Leon S.
Leon S.

Reputation: 3687

Gradle: Run subproject task also on dependency projects

I am stuck on the following Problem: I have a main project with multiple sub-projects, each dependent on each other. For example:

+---Main
    +- Shared
       +- Variant1
          +- Variant1A

If I run the build task on project 3 with the Variant1A:build command, all other projects get built too.

I now added a deploy task to every sub-project, and if I deploy one of my projects, I want to deploy every project it depends upon as well, like so:

+---Main:deploy
    +- Shared:deploy
       +- Variant1:deploy
          +- Variant1A:deploy

However running Variant1A:deploy only runs the task for the Variant1A project:

+---Main:build
    +- Shared:build
       +- Variant1:build
          +- Variant1A:deploy

As I have a lot of subprojects and many complicated dependencies, I don't want to specify each project with dependsOn.
So I am searching for a way to run the task on all local projects like build does, however I don't want to add the logic to the build task, as it should not be deployed in the normal building process...

Upvotes: 0

Views: 575

Answers (1)

JBirdVegas
JBirdVegas

Reputation: 11403

from your root project's build.gradle you might be able to do something like this

// look for deploy as a task argument sent via command line
def isDeploy
// could be many tasks supplied via command line
for (def taskExecutionRequest : gradle.startParameter.taskRequests) {
    // we only want to make sure deploy was specified as a task request
    if (taskExecutionRequest.args.contains('deploy')) {
        isDeploy = true
    }
}
// if deploy task was specified then setup dependant tasks
if (isDeploy) {
    // add the task to all subprojects of the `rootProject`
    subprojects.each {
        // get all projects that depend on this subproject
        def projectDependencies = project.configurations.runtime.getAllDependencies().withType(ProjectDependency)
        // the subproject's dependant projects may also have project dependencies
        def dependentProjects = projectDependencies*.dependencyProject
        // now we have a list of all dependency projects of this subproject
        // start setting up the dependsOn task logic
        dependentProjects.each {
            // find the deploy task then make our dependant
            // project's deploy task a dependant of the deploy task
            tasks.findByName('deploy').dependsOn project(it.name).tasks.findByName('deploy')
        }
    }
}

Upvotes: 1

Related Questions