mustRunAfter() not a usable fix for the dependsOn ordering bug

I shall not repeat what has been extensively and eloquently discussed in GRADLE-427 and elsewhere. In the light of that the glaring disconnect between what Gradle currently does with respect to dependsOn and what many users need becomes rather obvious.

Gradle developers, please respect your users. I appreciate your opinion with regard to dependsOn and the perceived benefits of its current behaviour but here in the real world we need to do things which work, whether they follow some theoretical guidelines or not. dependsOn reordering tasks causes a lot of pain for actual users working on actual projects and we need a robust, concise, easy to use alternative which respects the specified order.

It appears mustRunAfter() was meant to be a solution but it leads to code like this:

someOtherTask.mustRunAfter(actualTask) anotherTask.mustRunAfter(actualTask) anotherTask.mustRunAfter(someOtherTask) task myTask {

dependsOn actualTask, someOtherTask, anotherTask }

While it does solve the issue it’s unreadable and error-prone to write. While clearly a hack, this still seems to be a dramatically easier to write and read workaround:

task myTaskDependency1(dependsOn: actualTask) task myTaskDependency2(dependsOn: someOtherTask) task myTaskDependency3(dependsOn: anotherTask) task myTask {

dependsOn myTaskDependency1, myTaskDependency2, myTaskDependency3 }

Please add an alternative which would be as easy as:

task myTask {

properlyDependsOn actualTask, someOtherTask, anotherTask }

1 Like

It’s just a helper method away:

void dependsOnOrdered(Task task, Task... others) {
    task.dependsOn(others)
    for (int i = 0; i < others.size() - 1; i++) {
        task[i + 1].mustRunAfter(task[i])
    }
}

Just in case someone wants to use this, a minor modification is required.

rather than

task[i+1].mustRunAfter(task[i])

it should be

others[i+1].mustRunAfter(others[i])

I had the same challenge, and initially tried the approach outlined above. After getting it into our build, I felt like it wasn’t going to scale well (when adding additional aliases), so I kept researching and found what felt like a better approach.

Details here :

https://caffeineinduced.wordpress.com/2015/01/25/run-a-list-of-gradle-tasks-in-specific-order/