# Depends on all existing tasks of given type in subprojects

**URL:** <https://discuss.gradle.org/t/depends-on-all-existing-tasks-of-given-type-in-subprojects/7395>\
**Category:** Old Forum Archive\
**Created:** [January 7, 2015, 11:15am UTC](https://discuss.gradle.org/t/depends-on-all-existing-tasks-of-given-type-in-subprojects/7395 "2015-01-07T11:15:00Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marcin](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/marcin/32/1870_2.png) [@Marcin](https://discuss.gradle.org/u/Marcin)\
**Post date:** [January 7, 2015, 11:15am UTC](https://discuss.gradle.org/t/depends-on-all-existing-tasks-of-given-type-in-subprojects/7395/1 "2015-01-07T11:15:00Z")

</div>

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:

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

```

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

```gradle
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

```gradle
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:

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

```

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

---

<div class="post-metadata">

**Author:** ![mark\_vieira](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/mark_vieira/32/5396_2.png) [@mark\_vieira](https://discuss.gradle.org/u/mark_vieira)\
**Post date:** [January 7, 2015, 5:06pm UTC](https://discuss.gradle.org/t/depends-on-all-existing-tasks-of-given-type-in-subprojects/7395/2 "2015-01-07T17:06:00Z")

</div>

Try this.

task foo {

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

}

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

---

<div class="post-metadata">

**Author:** ![Marcin](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/marcin/32/1870_2.png) [@Marcin](https://discuss.gradle.org/u/Marcin)\
**Post date:** [January 7, 2015, 8:32pm UTC](https://discuss.gradle.org/t/depends-on-all-existing-tasks-of-given-type-in-subprojects/7395/3 "2015-01-07T20:32:00Z")

</div>

Thanks Mark, it’s simpler.

---

<div class="post-metadata">

**Author:** ![Marcin](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/marcin/32/1870_2.png) [@Marcin](https://discuss.gradle.org/u/Marcin)\
**Post date:** [January 8, 2015, 12:49pm UTC](https://discuss.gradle.org/t/depends-on-all-existing-tasks-of-given-type-in-subprojects/7395/4 "2015-01-08T12:49:00Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![mark\_vieira](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/mark_vieira/32/5396_2.png) [@mark\_vieira](https://discuss.gradle.org/u/mark_vieira)\
**Post date:** [January 8, 2015, 4:24pm UTC](https://discuss.gradle.org/t/depends-on-all-existing-tasks-of-given-type-in-subprojects/7395/5 "2015-01-08T16:24:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Marcin](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/marcin/32/1870_2.png) [@Marcin](https://discuss.gradle.org/u/Marcin)\
**Post date:** [January 8, 2015, 4:29pm UTC](https://discuss.gradle.org/t/depends-on-all-existing-tasks-of-given-type-in-subprojects/7395/6 "2015-01-08T16:29:00Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![mark\_vieira](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/mark_vieira/32/5396_2.png) [@mark\_vieira](https://discuss.gradle.org/u/mark_vieira)\
**Post date:** [January 8, 2015, 4:32pm UTC](https://discuss.gradle.org/t/depends-on-all-existing-tasks-of-given-type-in-subprojects/7395/7 "2015-01-08T16:32:00Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Marcin](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/marcin/32/1870_2.png) [@Marcin](https://discuss.gradle.org/u/Marcin)\
**Post date:** [January 8, 2015, 4:42pm UTC](https://discuss.gradle.org/t/depends-on-all-existing-tasks-of-given-type-in-subprojects/7395/8 "2015-01-08T16:42:00Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![mark\_vieira](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/mark_vieira/32/5396_2.png) [@mark\_vieira](https://discuss.gradle.org/u/mark_vieira)\
**Post date:** [January 8, 2015, 4:44pm UTC](https://discuss.gradle.org/t/depends-on-all-existing-tasks-of-given-type-in-subprojects/7395/9 "2015-01-08T16:44:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Marcin](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/marcin/32/1870_2.png) [@Marcin](https://discuss.gradle.org/u/Marcin)\
**Post date:** [January 8, 2015, 4:58pm UTC](https://discuss.gradle.org/t/depends-on-all-existing-tasks-of-given-type-in-subprojects/7395/10 "2015-01-08T16:58:00Z")

</div>

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. ‘’’

---

<div class="post-metadata">

**Author:** ![mark\_vieira](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/mark_vieira/32/5396_2.png) [@mark\_vieira](https://discuss.gradle.org/u/mark_vieira)\
**Post date:** [January 8, 2015, 5:22pm UTC](https://discuss.gradle.org/t/depends-on-all-existing-tasks-of-given-type-in-subprojects/7395/11 "2015-01-08T17:22:00Z")

</div>

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.
