Simple internal (located in buildSrc
) Gradle plugin example, written in Kotlin using Gradle Kotlin DSL
- Travis CI
- GitHub Pages documentation
- GitHub daggerok/gradle-kotlin-dsl-plugin-buildSrc repository
gradle init --type=basic --dsl=kotlin --project-name=gradle-kotlin-dsl-plugin-buildSrc
mkdir -p buildSrc/src/main/kotlin/com/github/daggerok/plugin
create buildSrc/build.gradle.kts
file:
plugins {
`kotlin-dsl`
}
repositories { // required by kotlin-dsl plugin dependencies
jcenter()
}
gradlePlugin {
plugins {
register("hello-plugin") {
id = "hello"
implementationClass = "com.github.daggerok.plugin.HelloPlugin"
}
}
}
configure<KotlinDslPluginOptions> {
experimentalWarning.set(false)
}
create ./buildSrc/src/main/kotlin/com/github/daggerok/plugin/HelloPlugin.kt
file:
open class HelloExtension {
var greeting: String = "Hello"
var name: String = "buddy"
}
class HelloPlugin : Plugin<Project> {
override fun apply(project: Project): Unit = project.run {
val hello = project.extensions.create<HelloExtension>("HelloExtension")
project.extensions.add("hello", hello)
tasks.register("hello") {
doLast {
println("${hello.greeting.capitalize()}, ${hello.name.capitalize()}!")
}
}
}
}
plugins {
hello
}
hello {
greeting = "hola"
name = "pablo"
}
./gradlew hello
# output
Hola, Pablo!