{{announcement.body}}
{{announcement.title}}

Developing a Custom Gradle Plugin

DZone 's Guide to

Developing a Custom Gradle Plugin

In the following example, we will create a plugin with two tasks: task with command-line option and task with extension.

· Java Zone ·
Free Resource

Gradle allows you to implement your own plugins in any languages like Java, Groovy, or Kotlin, so you can reuse your build logic, and share it with others, which can be used across many different projects and builds. 

In our example, we are going to use Kotlin as the implementation language for a standalone plugin project and Kotlin in the build script plugin examples. In the following example, we will create a plugin with two tasks: task with command-line option and task with extension.

Plugin Setup

Setup build.gradle.kts

Let's create a plugin as a  standalone project so that we can publish it and share it with others, which we are going to package and publish a plugin is to use the Java Gradle Plugin Development Plugin.  It's a is simply a Java project that produces a JAR containing the plugin classes, it's will automatically apply the Java Plugin, add the gradleApi() dependency to the implementation configuration, generate the required plugin descriptors in the resulting JAR file, and configure the Plugin Marker Artifact to be used when publishing. 

Here is a simple build script for the project build.gradle.kts:

Groovy


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.  Handling user input from the script we will do through an extension and command option.  

Kotlin


Create src/resource/META-INF/plugin.properties  file and set reference to your plugin class:

Properties files


Extension

For using extension 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 a 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.

Kotlin


Task

Task classes from the Gradle API can be used in specific circumstances, extending DefaultTask is the most common scenario. Annotate the default task action as @TaskAction.  

Kotlin


Test

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

Kotlin


Plugin publishing

Register in Gradle Plugin Portal. See more: publishing tutorial 

Add the Plugin Publishing Plugin to the project

Add plugin from plugin-publishing (use the latest version):

Groovy


Configure the Plugin Publishing Plugin

Create a pluginBundle block in build.gradle.kts and specify global information regarding your plugin. This helps other people browsing the portal find more information about your plugin and learn how to contribute to its development.

Groovy


Publish your plugin

Run validate plugin command:

./gradlew validatePlugins

Run publishing, if you're not authorized use login command:

./gradlew publishPlugins 


Usage

Apply your plugin to project:

Java


Output of gradle hello --message = "My Gradle Plugin"

Plain Text


Add extension in your build.gradle:

Groovy


Output of  gradle hello:

Plain Text
Topics:
gradle, gradle plugins, java, kotlin, tutorial

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}