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

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


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

and

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:

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

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

Thanks!

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.

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