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.

Written by Jamie Tanna's profile image Jamie Tanna on , and last updated on .

Content for this article is shared under the terms of the Creative Commons Attribution Non Commercial Share Alike 4.0 International, and code is shared under the Apache License 2.0.

#blogumentation #gradle #java.

This post was filed under articles.

Related Posts

Other posts you may be interested in:

Interactions with this post

Interactions with this post

Below you can find the interactions that this page has had using WebMention.

Have you written a response to this post? Let me know the URL:

Do you not have a website set up with WebMention capabilities? You can use Comment Parade.