Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Skipping Gradle task dependencies if that task is up to date

I am trying to migrate a build to gradle, and I have one issue hanging me up with the build cache. I have a set of dependent tasks that are used to:

  • setup an embedded MySQL server
  • migrate the schema in that server using Flyway
  • generate Jooq code from that schema
  • stop the embedded MySQL server
  • compile that generated code with our application code

This entire process works fine, but I want to understand the build cache and how it works. The relevant portion of my build.gradle looks something like this:

plugins {
    id 'java'
    id 'nu.studer.jooq' version '2.0.11'
    id "org.flywaydb.flyway" version "5.2.4"
    id 'com.github.michaelruocco.embedded-mysql-plugin' version '2.1.10'
}

embeddedMysql {
    url = 'jdbc:mysql://127.0.0.1:3308/jooqGeneration'
    username = 'user'
    password = 'password'
    version = 'v5_7_latest'
}
startEmbeddedMysql.inputs.files(project.fileTree(dir: "src", include: "**/*.sql"))
startEmbeddedMysql.outputs.dir('build/generated-src/jooq/')

flyway {
    url = 'jdbc:mysql://127.0.0.1:3308/jooqGeneration'
    user = 'user'
    password = 'password'
    schemas = ['jooqGeneration']
}
flywayMigrate.dependsOn 'startEmbeddedMysql'
flywayMigrate.outputs.upToDateWhen { !startEmbeddedMysql.didWork }

jooq {
    sample(sourceSets.jooqSchemaGeneration) {
        jdbc {
            driver = 'org.mariadb.jdbc.Driver'
            url = 'jdbc:mysql://127.0.0.1:3308'
            user = 'user'
            password = 'password'
        }
        generator {
            name = 'org.jooq.util.DefaultGenerator'
            database {
                name = 'org.jooq.util.mysql.MySQLDatabase'
                includes = '.*'
                excludes = ''
                inputSchema = 'jooqGeneration'
            }
        }
    }
}
generateSampleJooqSchemaSource.dependsOn 'flywayMigrate'
generateSampleJooqSchemaSource.finalizedBy 'stopEmbeddedMysql'
generateSampleJooqSchemaSource.outputs.upToDateWhen { !flywayMigrate.didWork }

compileJava.dependsOn 'generateSampleJooqSchemaSource'

My problem is that the entire build chain here is pretty highly coupled. For every step in the dependency chain, I have to manage whether or not that step is up to date.

This all seems to stem from the fact that even if a task is up to date, the tasks that task depends on are still run. If I add some inputs and outputs to the generateSampleJooqSchemaSource task, it will correctly determine if it is up to date, but even though the task is up to date, the dependsOn tasks run anyway. This means that I need to go to each dependent step and handle when we run. I solved that by seeing if each dependent step did any work, and then added the files being updated to the first task where I am starting up the embedded MySQL server.

Is there a way to move all of that decision making to the final generateSampleJooqSchemaSource such that the dependsOn tasks only run if generateSampleJooqSchemaSource is not up to date?

This would allow me to re-use some of these steps for other uses. For example, I could re-use the embedded MySQL server for some database integration tests without having to setup another separate task.

Mostly I am just looking to understand the Gradle build cache, and how it decides to run these tasks.

like image 917
Michael Davis Avatar asked Dec 01 '25 11:12

Michael Davis


1 Answers

Tasks relationships and ordering happens when Gradle determines what to run, there is no up-to-date concept at that point.

Once the task graph is computed, Gradle will execute all tasks in the appropriate order, now checking whether anything has to be done for them.

One way of solving your problem would be to make sure that whatever causes your generateSampleJooqSchemaSource task to be up-to-date is also causing its dependencies to be up-to-date as well.

Or you could add a task that all these tasks depend on which would then output some content to be used by the later tasks in the chain as their up-to-date check, causing them to be skipped.

like image 191
Louis Jacomet Avatar answered Dec 04 '25 06:12

Louis Jacomet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!