ok, having a conversation with myself here. Seems that the perfect solution to this would have been the following syntax:
//root build.gradle
task foo(dependsOn: getTasksByName('bar', true)) << {
println "I am foo!"
}
(where I want the foo task to depend on ‘bar’ tasks in all suprojects if the task is present in the subproject)
The getTaskByName(x, true) call is supposed to return a set of Task objects for the current project and all sub projects. This is exactly what I was looking for.
Unfortunately there is still a hurdle. I have created two custom build files at one/three/build.gradle and two/build.gradle with the following code:
task bar << {
println "I am bar in ${project.path}"
}
i.e. the projects three and :two have the ‘bar’ task whereas the project :one doesn’t. However, executing the root project ‘:foo’ task gives the following result:
nadurra:gradletest mbjarland$ gradle -q foo
I am bar in :one:three
I am foo!
note the missing execution of bar! In fact using the given setup and following root build.gradle file:
project(':one').dependsOn(':one:three')
project(':two').dependsOn(':one:three')
task foo(dependsOn: getTasksByName('bar', true)) << {
println "I am foo!"
}
println "THREE: " + project(":one:three").getTasksByName('bar', false)
println "TWO:
" + project(":two").getTasksByName('bar', false)
gives the following execution log:
nadurra:gradletest mbjarland$ gradle -q foo
THREE: [task ':one:three:bar']
TWO:
[]
I am bar in :one:three
I am foo!
i.e. even an explicit call to getTasksByName on project ‘:two’ returns an empty set. A direct call “gradle bar” does execute the task correctly.
What gives? To me this looks like a bug in the getTasksByName recursive selection…but I might be missing something here.
Any input on how to best accomplish the intended functionality or what the issue with my call to getTasksByName is would be much appreciated.
[EDIT: Turns out the behavior above is caused by the build life cycle phases. If I add code into an execution time task closure ala:
task thisworks << {
allprojects.each { project ->
println "thisworks> ${project.path.padLeft(17)} ->
${project.getTasksByName('bar', false)}"
}
}
the output is as expected. This does however as far as I can see disqualify the getTasksByName call from the use case “make your root project task depend on all tasks with the same name in your subprojects”.
It also leaves us without a proper solution to the initial problem since modifying task dependencies does not seem to work once you are in the execution phase]