Whether taskB mustRunAfter taskA, or taskB dependsOn taskA, it seems that taskA runs first, then taskB runs. What's the difference?
For example:
tasks.create('a')
tasks.create('b').dependsOn('a')
tasks.create('c')
tasks.create('d').mustRunAfter('c')
dependsOn - sets task dependencies. Executing b here would require that a be executed first.
mustRunAfter - sets task ordering. Executing d does not require c. But, when both c and d are included, c will execute before d.
Where possible, declare task inputs instead of task dependencies. That said, expanding on mkobit's answer:
Ordering and dependencies involving two tasks
mustRunAfter
mustRunAfter controls the execution order of tasks explicitly specified on the command line, but doesn't create implicit dependencies on other tasks. Given taskA and taskB:
build.gradle.kts
val taskA by tasks.registering {
doLast {
println("taskA")
}
}
val taskB by tasks.registering {
doLast {
println("taskB")
}
}
taskB {
mustRunAfter(taskA)
}
then
gradle taskB taskA executes taskA followed by taskB;
gradle taskA executes taskA only;
gradle taskB executes taskB only.
dependsOn
dependsOn creates implicit dependencies on other tasks. Given the same two tasks:
taskB {
dependsOn(taskA)
}
then
gradle taskB executes taskA followed by taskB;
gradle taskB taskA executes taskA followed by taskB;
gradle taskA executes taskA only.
Ordering and dependencies involving three tasks
mustRunAfter and dependsOn accept an unordered collection of tasks. Given:
val taskC by tasks.registering {
doLast {
println("taskC")
}
}
taskC {
dependsOn(taskA, taskB)
}
then gradle taskC executes taskA and taskB in no guaranteed order, followed by taskC (assuming no other dependsOn or mustRunAfter declarations).
mustRunAfter also controls the execution order between interdependent tasks that would otherwise have no guaranteed order. Given:
taskB {
mustRunAfter(taskA)
}
then gradle taskC executes taskA followed by taskB followed by taskC, but gradle taskB only runs taskA because taskB does not dependOn(taskA).
Other types of task dependency
taskA { finalizedBy(taskB) } ensures gradle taskA executes taskA followed by taskB;
shouldRunAfter provides weaker ordering guarantees than mustRunAfter, useful for breaking cyclic dependencies and parallel task execution.
Further reading
Consult the Gradle documentation for more information on:
adding dependencies to tasks
ordering tasks
skipping tasks
Sometimes they have the same effect. For example, if taskC dependsOn taskA and taskB, then it doesn't matter whether taskB dependsOn taskA or mustRunAfter it - when you run taskC, the order will be taskA, taskB, taskC.
But if taskC dependsOn taskB only, then there's a difference. If taskB dependsOn taskA, then it's the same as above - taskA, taskB, taskC. If taskB merely mustRunAfter taskA, then taskA doesn't run, and running taskC will run taskB, then taskC.
mustRunAfter really means if taskA runs at all, then taskB must run after it.
Generally, if order is important, it appears that you need both dependsOn and mustRunAfter. Here is an example of running two tasks, one (custom registered "importUnicodeFiles" task) that is in "this" project and one (predefined "run" task) that is in a sibling project named ":unicode":
tasks.register("rebuildUnicodeFiles") {
description = "Force the rebuild of the `./src/main/resources/text` data"
val make = project(":unicode").tasks["run"]
val copy = tasks["importUnicodeFiles"]
dependsOn(make)
dependsOn(copy)
copy.mustRunAfter(make)
}
Related
If taskB depends on taskA, when I execute task A and then taskB...
Question 1: Is taskA executed just once?
Question 2: Is it executed when I run taskA or when I run taskB?
Each Gradle task will only be executed once in a build (Gradle invocation).
Also, the task order you pass to Gradle will only be maintained, if there are no dependencies between them:
If you call gradle taskA taskB, but taskA.dependsOn taskB, taskB will be executed before taskA.
I'm having trouble figuring out how to force a task to always run after another one. I'm aware of dependsOn, but taskA.dependsOn taskB will cause taskB to execute first. I'm also aware of mustRunAfter and shouldRunAfter, but taskB.mustRunAfter taskA does not force taskB to run, it only ensures that IF both are called, taskA runs first. IF I use both, I get a circular dependency error.
I want gradle taskA to cause taskA to run, then taskB to run.
I was looking for finalizer tasks. Use the method finalizedBy():
taskA.finalizedBy taskB
I have a simple project with subprojects and I want to generate aggregate report for all tests when I execute gradle test command.
I have followed the gradle documentation and added following:
task testReport(type: TestReport) {
// make sure this task is run after all subproject test tasks
mustRunAfter subprojects*.test
destinationDir = file("$buildDir/reports/allTests")
// Include the results from the `test` task in all subprojects
reportOn subprojects*.test
}
This works when I execute gradle test testReport, but when I execute gradle test or gradle build in the root project - the task testReport is not run.
How do make gradle to run the task without specifying it every time?
Add: test.finalizedBy 'testReport' to your build.gradle; just at the root level, doesn't have to be inside any closure.
taskX.finalizedBy taskY
Will run taskY everytime taskX completes execution successfully.
I've got a gradle build with source and javadoc jars and I'd like these tasks to be executed before distZip and distTar, is there a dependency that captures both of those for use with shouldRunAfter.
Right now I've got:
task javadocJar(type: Jar) {
classifier 'javadoc'
from javadoc
}
task sourcesJar(type: Jar) {
classifier 'sources'
from sourceSets.main.allSource
}
tasks.distZip.shouldRunAfter tasks.javadocJar
tasks.distTar.shouldRunAfter tasks.javadocJar
tasks.distZip.shouldRunAfter tasks.sourcesJar
tasks.distTar.shouldRunAfter tasks.sourcesJar
I'd like to condense down those four shouldRunAfter to two which captures both distZip and distTar.
You can use groovy syntax to make this shorter
[distZip, distTar]*.shouldRunAfter javadocJar, sourcesJar
Probably you also want dependsOn instead of shouldRunAfter so that the jars are built whenever one of the dist tasks is enabled.
I have 2 gradle projects. Lets say projectA and projectB. I need to create a task in projectA, which can execute a task in projectB. I tried with many ways, but i couldn't find a way to do that. Consider following code,
//ProjectA build.gradle
task taskA(dependsOn: ProjectB.taskB) << { println "executed taskB" }
//ProjectB build.gradle
task taskB() << { println "executing taskB"}
when I run taskA output should be,
>> gradle taskA
executing taskB
executed taskB
Can I anyhow achieve this?
thanks.
task taskA(dependsOn: project(":ProjectB").taskB)
We don't say that taskA executes taskB but that it depends on taskB.