Adding parameters to a Gradle plugin

July 25, 2020


Last time I wrote about the basic setup of a Gradle plugin in Kotlin(Writing a Gradle plugin with Kotlin ).

This time we want to add some parameters to this plugin to make it more generic.


Gradle is a build automation tool from the java ecosystem. It has a lot of plugins for various use cases.

Buildfiles are describedin Groovy as default but Gradle recently added the possibility to also write build scripts in a Kotlin DSL.

The Kotlin support will hit version 1.0 with Gradle 5.0.


We created a small plugin to print a simple “Hello World” message on screen. It can be found here: kotlintestplugin .

Then we used this plugin in a small testproject to actually see it: kotlintestpluginproject .

We will copy the kotlintestplugin project and use at as a starting point to build a parameterized Gradle plugin.

Getting started

Parameters for Gradle plugins are implemented through extensions. To add parameters we have to add a custom extension to our plugin.


// We can create the extension as a simple Class with one field for the parameter
// This parameter has the default value "Hello World"
open class KotlinTestExtension(
        var message:String = "Hello World")

open class KotlinTestPlugin : Plugin<Project> {
    override fun apply(project: Project) {
        // We need to register the extension in order to use it in our task

        project.tasks.create<TestTask>("kotlinTestPlugin", {
            description = "Print test message"
            group = "TestPlugin"

open class TestTask : DefaultTask() {
    fun testAction() {
        // We search for the extension and then use it to print the message
        project.extensions.findByType(!!.run {

Packing the plugin

With this changes we are able to build a parameterized version of our test plugin.

gradle jar

The binary should now be available under: build/libs/kotlintestplugin-0.0.1-SNAPSHOT.jar

Applying the plugin

To apply the plugin we again need a test project. I simply copied the test project from the initial plugin: kotlintestpluginproject .

At first we need to copy the plugin binary into the libs dir in the project root.

At next we will extend our buildscript to configure the plugin.


// We need to add an import in order to be typesafe
import de.klg71.kotlintestplugin.KotlinTestExtension

buildscript {
    repositories {
        flatDir {
    dependencies {


// Configure the message to be print
configure<KotlinTestExtension> {
    message = "Hello Dose of Kotlin"

Now we can use the kotlinTestPlugin task of Gradle to see our new message.

gradle kotlinTestPlugin

> Task :kotlinTestPlugin
Hello Dose of Kotlin

Wrapping up

We extended our exiting gradle plugin and made it configurable in the target project.

The sources are available on Github below: