Developing a Gradle plugin
· 8 min read

I've recently been learning how to write a Gradle plugin and want to document my insights.
Types of Plugins
There are three ways to implement a Gradle plugin: Script plugins, Precompiled script plugins, and Binary plugins.
Script plugins: The plugin logic is implemented directly in thebuild.gradlefile and can only be used in the current build.Precompiled script plugins: The plugin logic is implemented in a separate file (either.gradleor.gradle.kts) within the project and can be used across multiple builds in the project.Binary plugins: The plugin logic is implemented in a standalone project and packaged as a JAR file, which can be referenced and used in other projects by including the JAR file.
In this article, we'll create a Binary plugin using a separate project.
Environment Setup
You'll need to install Java and Gradle.
tip
For this guide, we are using Java 21.0.3 and Gradle 8.10.2.
Initializing the Project
Run the following command:
gradle init --type java-gradle-plugin
Welcome to Gradle 8.10.2!
Here are the highlights of this release:
- Support for Java 23
- Faster configuration cache
- Better configuration cache reports
For more details see https://docs.gradle.org/8.10.2/release-notes.html
Starting a Gradle Daemon (subsequent builds will be faster)
Project name (default: gradle-plugin-demo):
Select build script DSL:
1: Kotlin
2: Groovy
Enter selection (default: Kotlin) [1..2]
Generate build using new APIs and behavior (some features may change in the next minor release)? (default: no) [yes, no]
> Task :init
For more information, please refer to https://docs.gradle.org/8.10.2/userguide/custom_plugins.html in the Gradle documentation.
BUILD SUCCESSFUL in 1m 5s
1 actionable task: 1 executed
After running the init command, you'll be prompted to answer three questions:
Project name: Set the project name (default is the current directory name).Build script DSL: Choose the language for the build script (default isKotlin).New APIs and behavior: Whether to enable new APIs and features (default isno).
tip
Every Gradle project has a build file (build.gradle). The file extension differs depending on the scripting language: Groovy uses build.gradle, while Kotlin uses build.gradle.kts.
After initialization, the project structure will look like this:
.
├── .gitattributes
├── .gitignore
├── gradle
│ ├── libs.versions.toml
│ └── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── plugin
│ ├── build.gradle.kts
│ └── src
│ ├── functionalTest
│ │ └── java
│ │ └── org
│ │ └── example
│ │ └── GradlePluginDemoPluginFunctionalTest.java
│ ├── main
│ │ ├── java
│ │ │ └── org
│ │ │ └── example
│ │ │ └── GradlePluginDemoPlugin.java
│ │ └── resources
│ └── test
│ ├── java
│ │ └── org
│ │ └── example
│ │ └── GradlePluginDemoPluginTest.java
│ └── resources
└── settings.gradle.kts
19 directories, 12 files
gradle: Contains configuration for thegradle-wrapperand defines the version of Gradle used in the project.libs.versions.tomlis a version management file for dependencies, another way of using the Gradle Version Catalog.plugin: Directory for the plugin's code, including core and test code.settings.gradle.kts: Configuration file for theGradleproject.
