Malt
Malt

Reputation: 30335

Gradle - make "assemble" depend on "clean" for all projects

I have several unrelated Java projects built with gradle, some of which are assembled into a jar file, others, into a war.

I'd like the assemble task of all projects to depend on the clean task since we've had issues with various old classes getting into assembled jars/wars from the build folder cache. Is there a way of doing that without adding assemble.dependsOn clean to each and every build.gradle?

Upvotes: 4

Views: 5845

Answers (2)

tddmonkey
tddmonkey

Reputation: 21184

You can handle this with a global hook in your ./gradle/init.gradle script. Anything you put in there is executed on every build.

In order to avoid failures on projects that don't have an assemble task you need a filter as well, something like the following will work:

allprojects {
    tasks.whenTaskAdded { theTask ->
        if (theTask.name.equals('assemble')) {
            theTask.dependsOn clean
        }
    }
}

What this is doing is applying a block to all projects defined (allproject). When each task is added this will run, and when a task with the name assemble is added a dependency will be added to clean.

Upvotes: 10

JBirdVegas
JBirdVegas

Reputation: 11413

From your top level build.gradle

submodules {
   assemble.dependsOn clean
}

This will apply the dependsOn to each subproject

Upvotes: 0

Related Questions