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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With