Task Depends on Subproject Tasks?

Hi,

I have a Root Project that is basically a container for multiple other projects and does not host any real code. I want to wrap Maven publishing like so:

task publishAndSayHi {
    dependsOn('publishToMavenLocal')
    dependsOn('sayHi')
}

This will not work however because ‘publishtoMavenLocal’ only exists on the sub projects, not the root (as it has no code as I mentioned). I can do like so:

task publishAndSayHi {
    dependsOn(':subProj1:publishToMavenLocal')
    dependsOn(':subProj2:publishToMavenLocal')
    dependsOn(':subProj3:publishToMavenLocal')
    dependsOn('sayHi')
}

But I was wondering if perhaps there is more elegant way to do so without manually defining the sub projects?

1 Like

Well you could use a little Groovy syntax sugar to make it prettier like so:

task publishAndSayHi {
    subprojects.each { dependsOn("${it.name}:publishToMavenLocal") }
    dependsOn('sayHi')
}

Alternatively, if you don’t want to do this for all subprojects you can just define an Array.

4 Likes

Excellent, thank you very much!

Thank you so much, this solution worked for me.