Gradle, dependsOn ordering

You're using Gradle 1.5, and mustRunAfter dates from 1.6. The current version is 1.8.

dependsOn doesn't fix any order for the tasks. This has been discussed ad nauseam in various bug reports. The workaround is to use depencencies between indifidual tasks, or mustRunAfter.


For whatever reason gradle does guarantee order for dependsOn, after time they added mustRunAfter, which however you have to chain yourself like a idiot, so here is a utility:

task buildAppRelease() {
    group = "build"
    dependsOn ordered(":allClean", ":allTestReleaseUnitTest", ":app:assembleRelease")
}

def ordered(String... dependencyPaths) {
    def dependencies = dependencyPaths.collect { tasks.getByPath(it) }
    for (int i = 0; i < dependencies.size() - 1; i++) {
        dependencies[i + 1].mustRunAfter(dependencies[i])
    }
    return dependencies
}

The problem with your build script isn't the task dependencies, but that the task definition for resignclientjars is incorrect. It's doing its work in the configuration phase (i.e. for every build invocation whatsoever) instead of the execution phase. A correct task definition would look as follows:

task resignclientjars(dependsOn: unwar) {
    doLast {
       ...
    }
} 

You can read up on configuration phase vs. execution phase in the Gradle User Guide or the Gradle forums.


The unpredictable dependency ordering in Gradle is really annoying.

Here is workaround for executing the dependent tasks in order:

task dist(type: Zip) {
    def tasks = [clean, jar, test, docs]
    for (int i = 0; i < tasks.size() - 1; i++) {
        tasks[i + 1].mustRunAfter(tasks[i])
    }
    dependsOn(tasks)

    //...other stuff
}

Probably this workaround could be extracted in reusable manner as strictDependsOn()...

Tags:

Gradle