Introduction

Target audience: Beginner to intermediate developer with Gradle.

Starting a new Gradle plugin project can seem intimidating. Fortunately, Gradle provides a powerful interactive assistant, the task`init`, which generates a complete, clean, and ready-to-use project structure. This guide will show you how to use this tool to create a healthy plugin base, explain its structure, and show you how to run the initial build tasks.

Step 1: Launch the project assistant

The command`gradle init`is the starting point. It launches a command-line assistant that asks you a series of questions to configure the project.

Open your terminal in an empty folder and run:

gradle init

The assistant will guide you. Here are the choices to make for a Gradle plugin written in Kotlin:

  1. Select type of project to generate:Choose`Gradle plugin`.

  2. Select implementation language:Choose`Kotlin`.

  3. Select build script DSL:Choose`Kotlin`.

  4. Project name:Enter a name for your project (ex:`site-baker`).

  5. Plugin id:Give a unique identifier to your plugin (e.g:`com.cheroliv.site-baker`).

  6. Plugin class:Spécifiez le nom de la classe d’implémentation (ex:`com.cheroliv.SiteBakerPlugin`).

Once finished, Gradle generates a complete file tree.

Diagram of the initialization process
@startuml
start
:Run `gradle init`;
:Choose "Gradle plugin;
:Choose "Kotlin" (language);
:Choose "Kotlin" (DSL);
:Enter name, ID, and class;
:Generated project;
stop
@enduml

Step 2: Understanding the generated structure

The assistant creates a multi-module project structure, separating the root project from the plugin source code itself.

.
├── gradle/
│   └── wrapper/
│       ├── gradle-wrapper.jar
│       └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── plugin/
│   ├── build.gradle.kts
│   └── src/
│       ├── main/
│       │   └── kotlin/
│       │       └── com/cheroliv/SiteBakerPlugin.kt
│       ├── test/
│       │   └── kotlin/
│       │       └── com/cheroliv/SiteBakerPluginTest.kt
│       └── functionalTest/
│           └── kotlin/
│               └── com/cheroliv/SiteBakerPluginFunctionalTest.kt
└── settings.gradle.kts

key files and folders

  • settings.gradle.kts`This file at the root defines the modules included in the build. Here, it includes the sub-project`plugin.

[source,kotlin] (No text provided to translate) rootProject.name = "site-baker include("plugin") ----


  • plugin/build.gradle.kts`That’s the heart of your plugin’s configuration. It applies the plugin`java-gradle-plugin, declares the dependencies and configures the plugin’s metadata.

[source,kotlin] ---- plugins { java-gradle-plugin alias(libs.plugins.kotlin.jvm) }

gradlePlugin { val siteBaker by plugins.creating { id = "com.cheroliv.site-baker implementationClass = "com.cheroliv.SiteBakerPlugin } } (empty)

  • `src/main/`Contains the source code of your plugin.

  • `src/test/`Contains the unit tests. They run quickly and in isolation.

  • src/functionalTest/: Contains functional tests. These tests use`GradleRunner`To run a full version of Gradle on a test project, thereby simulating real use of your plugin.

Step 3: Build and test the plugin

The generated project includes theGradle Wrapper(gradlew) This is the recommended way to run Gradle, as it ensures that all developers use the same version, guaranteeing reproducible builds.

Basic tasks

  • Build the project:

This command compiles your code, runs all tests (unit and functional), and assembles the JAR of your plugin. [source,bash] Could you please provide the French text you would like translated? ./gradlew build ----

  • Run all checks:

The task`check`is an alias that runs all verification tasks, including`test` et functionalTest. [source,bash] (I cannot provide a translation because no French text was supplied.) ./gradlew check ----

  • Run only the unit tests:

For quick feedback during development. [source,bash] ---- ./gradlew test (Empty)

The command`./gradlew :test`, which explicitly targets the root project, will fail because the latter contains no tests. On the other hand : * ./gradlew test`works because Gradle executes the task`test`on all sub-projects that possess it (here, the module`plugin). * `./gradlew :plugin:test`is the most explicit command to run the module’s tests)`plugin`Only.

  • Run only functional tests :

Slower, they are useful for validating overall behaviour. [source,bash] (blank) ./gradlew functionalTest (Empty)

Test results are generated in the folder.plugin/build/reports/tests/. You can open the file`index.html`in your browser for a detailed report

Conclusion

In a few minutes,`gradle init`You have been provided with a solid, modern, and complete project base for developing your plugin. You have a clear structure, preconfigured unit and functional tests, and a reproducible build system thanks to the Gradle Wrapper.

You are now ready to open the file`SiteBakerPlugin.kt`And start adding the business logic of your plugin!

Related articles