Depends on all existing tasks of given type in subprojects

My ‘release’ task applied on a root project from a common/shared ‘.gradle’ file has to depend on all existing ‘bintrayUpload’ tasks in subprojects. I tried:

task foo {
    dependsOn subprojects.bintrayUpload
    (...)
}

but it fails on empty subprojects (having their own subrojects). I tried with:

task foo {
    dependsOn project.getTasksByName("bintrayUpload", true)
    (...)
}

and it works, but a drawback is: > Cannot configure the ‘publishing’ extension after it has been accessed.

when I try to do anything with publishing later.

I cannot do

task foo {
    dependsOn ([project(":proj1"), project(":proj2")].bintrayUpload)
}

as I don’t know which projects should be used by name (it is a common/shared script).

Question. How can I depend on all existing ‘bintrayUpload’ in subprojects, but not fail on subprojects which don’t have that task?

Update I have used a workaround with depending on:

Set uploadableProjects = subprojects.findAll { sProject ->
     sProject.tasks.matching { it.name == "bintrayUpload" }
}

but I wonder if it can be done clearly in Gradle API?

1 Like

Try this.

task foo {

dependsOn subprojects.collect { it.tasks.withType(BintrayUploadTask) }

}

Edit I updated my example to include upload tasks from all subprojects.

Thanks Mark, it’s simpler.

Btw, there is one issue with your solution. Gradle requires to have a buildscript dependency definition in every ‘.gradle’ file imported into the main ‘.gradle’ file to be able to use classes from those plugins directly (also to import them). It duplicates places where the current dependency version number has to be kept.

Btw2, I wonder if there is a feature request to remove that limitation?

When you say “imported” do you mean like a script plugin? Ie. using the ‘apply from: …’ syntax?

I mean “import com.jfrog.bintray.gradle.BintrayUploadTask” to be able to use it in the script. It fails when done in other file even if it is applied from the ‘.gradle’ which has Bintray plugin as a buildscript dependency.

If you add the buildscript dependency to your root project it should be available to all subprojects. What file are you defining the dependency in and what file is throwing the error?

That’s another store.

build.gradle in my project with BintrayPlugin as a dependency. I apply releasing configuration from another file ‘apply from: ‘release.gradle’’. And in ‘release.gradle’ I cannot reference Bintrayplugin class directly unless I add there (to ‘release.gradle’ file) the whole ‘buildscript { repositories {}, dependencies {} }’ with dependency to Bintray plugin.

Got it. What if you put the ‘buildscript {…}’ block only in the ‘release.gradle’ file?

I doesn’t work. Having ‘buildscript {}’ with Bintray plugin dependency in ‘release.gradle’ and ‘apply plugin: ‘com.jfrog.bintray’’ in the same file I have got:

‘’’ > Failed to apply plugin [id ‘com.jfrog.bintray’]

Plugin with id ‘com.jfrog.bintray’ not found. ‘’’

I believe this is due to the fact that the ‘buildscript’ block is being evaluated before the plugin is being applied. Therefore there is no way to avoid defining the classpath dependency in both files. If you want to avoid hard-coding the dependency version in both places you can always define it as a project property in a ‘gradle.properties’ file.