# Developing Custom Gradle Plugins

version 6.2.1

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 plugins, so you can reuse your build logic, and share it with others.

You can implement a Gradle plugin in any language you like, provided the implementation ends up compiled as JVM bytecode. In our examples, we are going to use Groovy as the implementation language. Groovy, Java or Kotlin are all good choices as the language to use to implement a plugin, as the Gradle API has been designed to work well with these languages. In general, a plugin implemented using Java or Kotlin, which are statically typed, will perform better than the same plugin implemented using Groovy.

## Packaging a plugin

There are several places where you can put the source for the plugin.

Build script

  You can include the source for the plugin directly in the build script. This has the benefit that the plugin is automatically compiled and included in the classpath of the build script without you having to do anything. However, the plugin is not visible outside the build script, and so you cannot reuse the plugin outside the build script it is defined in.

`buildSrc` project

  You can put the source for the plugin in the `rootProjectDir/buildSrc/src/main/groovy` directory (or `rootProjectDir/buildSrc/src/main/java` or `rootProjectDir/buildSrc/src/main/kotlin` depending on which language you prefer). Gradle will take care of compiling and testing the plugin and making it available on the classpath of the build script. The plugin is visible to every build script used by the build. However, it is not visible outside the build, and so you cannot reuse the plugin outside the build it is defined in.

  See Organizing Gradle Projects for more details about the `buildSrc` project.

Standalone project

  You can create a separate project for your plugin. This project produces and publishes a JAR which you can then use in multiple builds and share with others. Generally, this JAR might include some plugins, or bundle several related task classes into a single library. Or some combination of the two.

In our examples, we will start with the plugin in the build script, to keep things simple. Then we will look at creating a standalone project.

## Writing a simple plugin

To create a Gradle plugin, you need to write a class that implements the Plugin interface. When the plugin is applied to a project, Gradle creates an instance of the plugin class and calls the instance’s Plugin.apply() method. 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 1. A custom plugin
build.gradle

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

    // Apply the plugin
    apply plugin: GreetingPlugin

build.gradle.kts

    class GreetingPlugin : Plugin<Project> {
        override fun apply(project: Project) {
            project.task("hello") {
                doLast {
                    println("Hello from the GreetingPlugin")
                }
            }
        }
    }

    // Apply the plugin
    apply<GreetingPlugin>()

Output of `gradle -q hello`

    > gradle -q hello
    Hello from the GreetingPlugin

One thing to note is that a new instance of a plugin is created for each project it is applied to. Also note that the Plugin class is a generic type. This example has it receiving the Project type as a type parameter. A plugin can instead receive a parameter of type Settings, in which case the plugin can be applied in a settings script, or a parameter of type Gradle, in which case the plugin can be applied in an initialization script.

## Making the plugin configurable

Most plugins offer some configuration options for build scripts and other plugins to use to customize how the plugin works. Plugins do this using extension objects. The Gradle Project has an associated ExtensionContainer object that contains all the settings and properties for the plugins that have been applied to the project. You can provide configuration for your plugin by adding an extension object to this container. An extension object is simply an object with Java Bean properties that represent the configuration.

Let’s add a simple extension object to the project. Here we add a `greeting` extension object to the project, which allows you to configure the greeting.

Example 2. A custom plugin extension
build.gradle

    class GreetingPluginExtension {
        String message = 'Hello from GreetingPlugin'
    }

    class GreetingPlugin implements Plugin<Project> {
        void apply(Project project) {
            // Add the 'greeting' extension object
            def extension = project.extensions.create('greeting', GreetingPluginExtension)
            // Add a task that uses configuration from the extension object
            project.task('hello') {
                doLast {
                    println extension.message
                }
            }
        }
    }

    apply plugin: GreetingPlugin

    // Configure the extension
    greeting.message = 'Hi from Gradle'

build.gradle.kts

    open class GreetingPluginExtension {
        var message = "Hello from GreetingPlugin"
    }

    class GreetingPlugin : Plugin<Project> {
        override fun apply(project: Project) {
            // Add the 'greeting' extension object
            val extension = project.extensions.create<GreetingPluginExtension>("greeting")
            // Add a task that uses configuration from the extension object
            project.task("hello") {
                doLast {
                    println(extension.message)
                }
            }
        }
    }

    apply<GreetingPlugin>()

    // Configure the extension
    the<GreetingPluginExtension>().message = "Hi from Gradle"

Output of `gradle -q hello`

    > gradle -q hello
    Hi from Gradle

In this example, `GreetingPluginExtension` is a object with a property called `message`. The extension object is added to the project with the name `greeting`. This object then becomes available as a project property with the same name as the extension object.

Oftentimes, you have several related properties you need to specify on a single plugin. Gradle adds a configuration block for each extension object, so you can group settings together. The following example shows you how this works.

Example 3. A custom plugin with configuration block
build.gradle

    class GreetingPluginExtension {
        String message
        String greeter
    }

    class GreetingPlugin implements Plugin<Project> {
        void apply(Project project) {
            def extension = project.extensions.create('greeting', GreetingPluginExtension)
            project.task('hello') {
                doLast {
                    println "${extension.message} from ${extension.greeter}"
                }
            }
        }
    }

    apply plugin: GreetingPlugin

    // Configure the extension using a DSL block
    greeting {
        message = 'Hi'
        greeter = 'Gradle'
    }

build.gradle.kts

    open class GreetingPluginExtension {
        var message: String? = null
        var greeter: String? = null
    }

    class GreetingPlugin : Plugin<Project> {
        override fun apply(project: Project) {
            val extension = project.extensions.create<GreetingPluginExtension>("greeting")
            project.task("hello") {
                doLast {
                    println("${extension.message} from ${extension.greeter}")
                }
            }
        }
    }

    apply<GreetingPlugin>()

    // Configure the extension using a DSL block
    configure<GreetingPluginExtension> {
        message = "Hi"
        greeter = "Gradle"
    }

Output of `gradle -q hello`

    > gradle -q hello
    Hi from Gradle

In this example, several settings can be grouped together within the `greeting` closure. The name of the closure block in the build script (`greeting`) needs to match the extension object name. Then, when the closure is executed, the fields on the extension object will be mapped to the variables within the closure based on the standard Groovy closure delegate feature.

In this example, several settings can be grouped together within the `configure<GreetingPluginExtension>` block. The type used on the `configure` function in the build script (`GreetingPluginExtension`) needs to match the extension type. Then, when the block is executed, the receiver of the block is the extension.

In this way, using an extension object extends the Gradle DSL to add a project property and DSL block for the plugin. And because an extension object is simply a regular object, you can provide your own DSL nested inside the plugin block by adding properties and methods to the extension object.

### Developing project extensions

You can find out more about implementing project extensions in Developing Custom Gradle Types.

## Working with files in custom tasks and plugins

When developing custom tasks and plugins, it’s a good idea to be very flexible when accepting input configuration for file locations. To do this, you can leverage the Project.file(java.lang.Object) method to resolve values to files as late as possible.

Example 4. Evaluating file properties lazily
build.gradle

    class GreetingToFileTask extends DefaultTask {

        def destination

        @OutputFile
        File getDestination() {
            project.file(destination)
        }

        @TaskAction
        def greet() {
            def file = getDestination()
            file.parentFile.mkdirs()
            file.write 'Hello!'
        }
    }

    task greet(type: GreetingToFileTask) {
        destination = { project.greetingFile }
    }

    task sayGreeting(dependsOn: greet) {
        doLast {
            println file(greetingFile).text
        }
    }

    ext.greetingFile = "$buildDir/hello.txt"

build.gradle.kts

    open class GreetingToFileTask : DefaultTask() {

        var destination: Any? = null

        @OutputFile
        fun getDestination(): File {
            return project.file(destination!!)
        }

        @TaskAction
        fun greet() {
            val file = getDestination()
            file.parentFile.mkdirs()
            file.writeText("Hello!")
        }
    }

    tasks.register<GreetingToFileTask>("greet") {
        destination = { project.extra["greetingFile"]!! }
    }

    tasks.register("sayGreeting") {
        dependsOn("greet")
        doLast {
            println(file(project.extra["greetingFile"]!!).readText())
        }
    }

    extra["greetingFile"] = "$buildDir/hello.txt"

Output of `gradle -q sayGreeting`

    > gradle -q sayGreeting
    Hello!

In this example, we configure the `greet` task `destination` property as a closure/provider, which is evaluated with the Project.file(java.lang.Object) method to turn the return value of the closure/provider into a `File` object at the last minute. You will notice that in the example above we specify the `greetingFile` property value after we have configured to use it for the task. This kind of lazy evaluation is a key benefit of accepting any value when setting a file property, then resolving that value when reading the property.

## Mapping extension properties to task properties

Capturing user input from the build script through an extension and mapping it to input/output properties of a custom task is a useful pattern. The build script author interacts only with the DSL defined by the extension. The imperative logic is hidden in the plugin implementation.

Gradle provides some types that you can use in task implementations and extensions to help you with this. Refer to Lazy Configuration for more information.

## A standalone project

Now we will move our plugin to a standalone project, so we can publish it and share it with others. This project is simply a Groovy project that produces a JAR containing the plugin classes. Here is a simple build script for the project. It applies the Groovy plugin, and adds the Gradle API as a compile-time dependency.

Example 5. A build for a custom plugin
build.gradle

    plugins {
        id 'groovy'
    }

    dependencies {
        implementation gradleApi()
        implementation localGroovy()
    }

build.gradle.kts

    plugins {
        groovy
    }

    dependencies {
        implementation(gradleApi())
        implementation(localGroovy())
    }

| The code for this example can be found at `samples/customPlugin` in the ‘-all’ distribution of Gradle.

So how does Gradle find the Plugin implementation? The answer is you need to provide a properties file in the jar’s `META-INF/gradle-plugins` directory that matches the id of your plugin.

### Example: Wiring for a custom plugin

src/main/resources/META-INF/gradle-plugins/org.samples.greeting.properties

    implementation-class=org.gradle.GreetingPlugin

Notice that the properties filename matches the plugin id and is placed in the resources folder, and that the `implementation-class` property identifies the Plugin implementation class.

### Creating a plugin id

Plugin ids are fully qualified in a manner similar to Java packages (i.e. a reverse domain name). This helps to avoid collisions and provides a way to group plugins with similar ownership.

Your plugin id should be a combination of components that reflect namespace (a reasonable pointer to you or your organization) and the name of the plugin it provides. For example if you had a Github account named "foo" and your plugin was named "bar", a suitable plugin id might be `com.github.foo.bar`. Similarly, if the plugin was developed at the baz organization, the plugin id might be `org.baz.bar`.

Plugin ids should conform to the following:

- May contain any alphanumeric character, '.', and '-'.

- Must contain at least one '.' character separating the namespace from the name of the plugin.

- Conventionally use a lowercase reverse domain name convention for the namespace.

- Conventionally use only lowercase characters in the name.

- `org.gradle` and `com.gradleware` namespaces may not be used.

- Cannot start or end with a '.' character.

- Cannot contain consecutive '.' characters (i.e. '..').

Although there are conventional similarities between plugin ids and package names, package names are generally more detailed than is necessary for a plugin id. For instance, it might seem reasonable to add "gradle" as a component of your plugin id, but since plugin ids are only used for Gradle plugins, this would be superfluous. Generally, a namespace that identifies ownership and a name are all that are needed for a good plugin id.

### Publishing your plugin

If you are publishing your plugin internally for use within your organization, you can publish it like any other code artifact. See the Ivy and Maven chapters on publishing artifacts.

If you are interested in publishing your plugin to be used by the wider Gradle community, you can publish it to the Gradle Plugin Portal. This site provides the ability to search for and gather information about plugins contributed by the Gradle community. Please refer to the corresponding guide on how to make your plugin available on this site.

### Using your plugin in another project

To use a plugin in a build script, you need to add the plugin classes to the build script’s classpath. To do this, you use a “buildscript { }” block, as described in see Applying plugins using the buildscript block. The following example shows how you might do this when the JAR containing the plugin has been published to a local repository:

Example 6. Using a custom plugin in another project
build.gradle

    buildscript {
        repositories {
            maven {
                url = uri(repoLocation)
            }
        }
        dependencies {
            classpath 'org.gradle:customPlugin:1.0-SNAPSHOT'
        }
    }
    apply plugin: 'org.samples.greeting'

build.gradle.kts

    buildscript {
        repositories {
            maven {
                url = uri(repoLocation)
            }
        }
        dependencies {
            classpath("org.gradle:customPlugin:1.0-SNAPSHOT")
        }
    }
    apply(plugin = "org.samples.greeting")

Alternatively, you can use the plugins DSL (see Applying plugins using the plugins DSL) to apply the plugin:

Example 7. Applying a community plugin with the plugins DSL
build.gradle

    plugins {
        id 'com.jfrog.bintray' version '0.4.1'
    }

build.gradle.kts

    plugins {
        id("com.jfrog.bintray") version "0.4.1"
    }

### Writing tests for your plugin

You can use the ProjectBuilder class to create Project instances to use when you test your plugin implementation.

### Example: Testing a custom plugin

src/test/groovy/org/gradle/GreetingPluginTest.groovy

    class GreetingPluginTest {
        @Test
        public void greeterPluginAddsGreetingTaskToProject() {
            Project project = ProjectBuilder.builder().build()
            project.pluginManager.apply 'org.samples.greeting'

            assertTrue(project.tasks.hello instanceof GreetingTask)
        }
    }

### Using the Java Gradle Plugin Development Plugin

You can use the Java Gradle Plugin Development Plugin to eliminate some of the boilerplate declarations in your build script and provide some basic validations of plugin metadata. This plugin will automatically apply the Java Plugin, add the `gradleApi()` dependency to the compile configuration, and perform plugin metadata validations as part of the `jar` task execution, and generate plugin descriptors in the resulting JAR’s `META-INF` directory.

Example 8. Using the Java Gradle Plugin Development plugin
build.gradle

    plugins {
        id 'java-gradle-plugin'
        id 'groovy'
    }

    gradlePlugin {
        plugins {
            simplePlugin {
                id = 'org.samples.greeting'
                implementationClass = 'org.gradle.GreetingPlugin'
            }
        }
    }

build.gradle.kts

    plugins {
        `java-gradle-plugin`
        groovy
    }

    gradlePlugin {
        plugins {
            create("simplePlugin") {
                id = "org.samples.greeting"
                implementationClass = "org.gradle.GreetingPlugin"
            }
        }
    }

When publishing plugins to custom plugin repositories using the Ivy or Maven publish plugins, the Java Gradle Plugin Development Plugin will also generate plugin marker artifacts named based on the plugin id which depend on the plugin’s implementation artifact.

## More details

Plugins often also provide custom task types. Please see Developing Custom Gradle Task Types for more details.

Gradle provides a number of features that are helpful when developing Gradle types, including plugins. Please see Developing Custom Gradle Types for more details.