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:



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.  


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

Properties files


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.



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.  



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


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):


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.


Publish your plugin

Run validate plugin command:

./gradlew validatePlugins

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

./gradlew publishPlugins 


Apply your plugin to project:


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

Plain Text

Add extension in your build.gradle:


Output of  gradle hello:

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

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}