Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

gradle

jar

war

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?

like image 699
Malt Avatar asked Oct 28 '25 09:10

Malt


1 Answers

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.

like image 116
tddmonkey Avatar answered Oct 31 '25 09:10

tddmonkey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!