# Plugin - setting Task "X" to depend on all subprojects finishing Task "Y"?

**URL:** <https://discuss.gradle.org/t/plugin-setting-task-x-to-depend-on-all-subprojects-finishing-task-y/13691>\
**Category:** Help/Discuss\
**Created:** [January 14, 2016, 5:49pm UTC](https://discuss.gradle.org/t/plugin-setting-task-x-to-depend-on-all-subprojects-finishing-task-y/13691 "2016-01-14T17:49:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![PerryAJ](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/perryaj/32/4147_2.png) [@PerryAJ](https://discuss.gradle.org/u/PerryAJ)\
**Post date:** [January 14, 2016, 5:49pm UTC](https://discuss.gradle.org/t/plugin-setting-task-x-to-depend-on-all-subprojects-finishing-task-y/13691/1 "2016-01-14T17:49:59Z")

</div>

Hi All,

New to Gradle Plugin development, can’t seem to find the right solution here.

Background:

I’m creating a plugin to support building and aggregating projects, including a ‘manifest’ of all jars produced and subprojects they were produced in. I want to define this task in the plugin itself to minimize the need of plugin users to do task configuration in build scripts (most will be coming from Ant and/Maven and I want to them to buy-in to the change minimizing their initial learning curve).

**Problem** : I don’t know how to configure a plugin TaskX to depend on completion of all subprojects TaskY. Currently my ‘manifest’ is created on completion of each subproject and _almost_ works through hacky use of static variables, but it’s definitely not the right solution.

**Goal** : Create a TaskX dependency in the Plugin implementation that runs only after TaskY has completed on all subprojects.

I have established some dependencies using dependsOn like this

```gradle

void apply(Project target){
...
    target.tasks.findByName("myTaskName").dependsOn(project.subprojects.jar)
...
}

```

and

```gradle
target.tasks.findByName("myTaskName").dependsOn(["earlierTask1", "earlierTask2"])

```

Which work fine for most of my tasks. But for this specific task, I want to wait until all subprojects are complete and then just run it once.

I want something like this fictional method:

```gradle
project.tasks.findByName("TaskX").dependsOnCompletionOfAllSubprojects("TaskY")

```

I am guessing I completely overlooked something, look forward to the likely “Doh!” moment.

Thanks!

---

<div class="post-metadata">

**Author:** ![Gary\_Hale](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/gary_hale/32/7647_2.png) [@Gary\_Hale](https://discuss.gradle.org/u/Gary_Hale)\
**Post date:** [January 14, 2016, 9:51pm UTC](https://discuss.gradle.org/t/plugin-setting-task-x-to-depend-on-all-subprojects-finishing-task-y/13691/2 "2016-01-14T21:51:31Z")

</div>

What you likely want to do is use a rule for this. Something like:

```
project.subprojects.each { subproject ->
    subproject.tasks.matching { it.name == "TaskY" }.all { task ->
        project.tasks.findByName("TaskX").dependsOn task
    }
}

```

`matching` and `all` are _configuration rules_, which means they apply not only to tasks that have already been created, but also to tasks that are created at some point in the future. What this does is says for each subproject, set up a rule for all tasks that have a given name (TaskY) so that TaskX in this project is made to depend on that task. As tasks are added, this rule will fire and set up the dependency. Using a rule like this allows you to avoid dealing with ordering issues where say, your plugin is applied first and then some other plugin that adds TaskY is added afterwards.

---

<div class="post-metadata">

**Author:** ![PerryAJ](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/perryaj/32/4147_2.png) [@PerryAJ](https://discuss.gradle.org/u/PerryAJ)\
**Post date:** [January 19, 2016, 6:29pm UTC](https://discuss.gradle.org/t/plugin-setting-task-x-to-depend-on-all-subprojects-finishing-task-y/13691/3 "2016-01-19T18:29:29Z")

</div>

The rule did exactly what I was looking for. Don’t know how I overlooked that in the docs. Thank you!
