Adding parameters to a Gradle plugin


Introduction

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.

Basics

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.

Recap

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.

src/main/kotlin/de/klg71/kotlintestplugin/KotlinTestPlugin.kt

// 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.extensions.create<KotlinTestExtension>("kotlintestextension", KotlinTestExtension::class.java)

        project.tasks.create<TestTask>("kotlinTestPlugin", TestTask::class.java).run {
            description = "Print test message"
            group = "TestPlugin"
        }
    }
}

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

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.

build.gradle.kts

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

buildscript {
    repositories {
        flatDir {
            dirs("libs")
        }
    }
    dependencies {
        classpath("de.klg71:kotlintestplugin:0.0.1-SNAPSHOT")
    }
}

apply(plugin="de.klg71.kotlintestplugin")

// 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: - kotlin_gradle_plugin_parameterized – The plugin - kotlin_plugin_test_project_parameterized – The project