gradle

Ordering tasks

Remarks#

Please note that mustRunAfter and shouldRunAfter are marked as “incubating” (as of Gradle 3.0) which means that these are experimental features and their behavior can be changed in future releases.

There are two ordering rules available:

  • mustRunAfter
  • shouldRunAfter

When you use the mustRunAfter ordering rule you specify that taskB must always run after taskA, whenever both taskA and taskB will be run.

The shouldRunAfter ordering rule is similar but less strict as it will be ignored in two situations:

  • if using that rule introduces an ordering cycle.
  • when using parallel execution and all dependencies of a task have been satisfied apart from the shouldRunAfter task, then this task will be run regardless of whether its shouldRunAfter dependencies have been run or not.

Ordering with the mustRunAfter method

task A << {
    println 'Hello from A'
}
task B << {
    println 'Hello from B'
}

B.mustRunAfter A

The B.mustRunAfter A line tells Gradle to run task after task specified as an argument.

And the output is:

> gradle -q B A
Hello from A
Hello from B

The ordering rule doesn’t introduce dependency between the A and the B tasks, but has an effect only when both tasks are scheduled for execution.

It means that we can execute tasks A and B independently.

The output is:

> gradle -q B 
Hello from B

This modified text is an extract of the original Stack Overflow Documentation created by the contributors and released under CC BY-SA 3.0 This website is not affiliated with Stack Overflow