Configure Gradle to Configure Tasks Globally with an initscript
As part of Configure Gradle to Allow Listing All Subproject Dependencies, I wanted to utilise Gradle initscripts to provide a global task across all my projects.
Unfortunately our setup from Running Spotless Automagically with Gradle doesn't quite work, but I was able to adapt the general set up from Marcin's article My Gradle init script which allows us to do this.
We can create a file i.e. ~/.gradle/init.d/hello.gradle
:
projectsEvaluated {
rootProject.allprojects { project ->
if (!tasks.findByName('hello')) {
task hello() {
println('Hey from ' + project.name)
}
}
}
}
Which allows us to run gradle hello
and it'll tell us which project it's executing in.