Chapter 38. Writing Custom Plugins

A Gradle plugin packages up reusable pieces of build logic, which can be used across many different projects and builds. Gradle allows you to implement your own custom plugins, so you can reuse your build logic, and potentially share it with others.

You can implement a custom plugin in any language you like, provided the implementation ends up compiled as bytecode. For the examples here, we are going to use Groovy as the implementation language. You could use Java or Scala instead, if you want.

38.1. Packaging a plugin

There are several places where you can put the source for the plugin. Two convenient options are to add the task implementation to your build script, or to put the source in the rootProjectDir/buildSrc/src/main/groovy directory. Gradle will take care of compiling the task and making it available on the classpath of the build script. See Chapter 39, Organizing Build Logic for more details, and some other options. In our examples, we will put the task implementation in the build script, to keep things simple.

38.2. Writing a simple plugin

To create a custom plugin, you need to write an implementation of Plugin. Gradle instantiates the plugin and calls the plugin instance's apply() method when the plugin is used with a project. The project object is passed as a parameter, which the plugin can use to configure the project however it needs to. The following sample contains a greeting plugin, which adds a hello task to the project.

Example 38.1. A custom plugin

build.gradle

apply plugin: GreetingPlugin

class GreetingPlugin implements Plugin<Project> {
    def void apply(Project project) {
        project.task('hello') << {
            println "Hello from the GreetingPlugin"
        }
    }
}

Output of gradle -q hello

> gradle -q hello
Hello from the GreetingPlugin

One thing to note is that only one instance of a given plugin is created for a given build. The same plugin instance is used for all projects in the build.

38.3. Getting input from the build

Most plugins need to read user defined input from the build script. Plugins read values using convention objects. The Gradle Project has a Convention object that helps keep track of all the settings and properties being passed to plugins. You can capture user input by telling the Project Convention about your plugin. To capture input, simply add a Java Bean compliant class into the Convention's list of plugins. Groovy is a good language choice for a plugin because plain old Groovy objects contain all the getter and setter methods that a Java Bean requires.

Let's add a simple convention object to the project. Here we add a greeting property to the project, which allows you to configure the greeting.

Example 38.2. A custom plugin convention

build.gradle

apply plugin: GreetingPlugin

greeting = 'Hi from Gradle'

class GreetingPlugin implements Plugin<Project> {
    def void apply(Project project) {
        project.convention.plugins.greet = new GreetingPluginConvention()
        project.task('hello') << {
            println project.convention.plugins.greet.greeting
        }
    }
}

class GreetingPluginConvention {
    def String greeting = 'Hello from GreetingPlugin'
}

Output of gradle -q hello

> gradle -q hello
Hi from Gradle

In this example, GreetingPluginConvention is a plain old Groovy object with a field called greeting. The convention object is added to the plugin list with the name greet. The name of the variable in the build needs to match the name of the field in the convention object. The name you choose for your plugin (greet) is arbitrary and can be whatever you choose.

Oftentimes, you have several related properties you need to specify on a single plugin. With Groovy plugins it is easy to offer a configuration closure block to group settings together. The following example shows you how to do this.

Example 38.3. A custom plugin with closure convention

build.gradle

apply plugin: GreetingPlugin

greet {
    message = 'Hi from Gradle' 
}

class GreetingPlugin implements Plugin<Project> {
    def void apply(Project project) {

        project.convention.plugins.greet = new GreetingPluginConvention()
        project.task('hello') << {
            println project.convention.plugins.greet.message
        }
    }
}

class GreetingPluginConvention {
    String message

    def greet(Closure closure) {
        closure.delegate = this
        closure() 
    }
}

Output of gradle -q hello

> gradle -q hello
Hi from Gradle

In this example, several convention settings can be grouped together within the greet closure. The name of the closure block in the build script (greet) needs a matching method on the convention object, and that method must take a closure as an argument. Then, when the closure is executed, the fields on the convention object will be mapped to the variables within the closure based on the standard Groovy closure delegate feature. This technique is possible in other JVM languages but may not be as convenient as in Groovy.