Skip to content

Simple internal (located inside buildSrc folder) Gradle plugin example, written in Kotlin using Gradle Kotlin DSL

Notifications You must be signed in to change notification settings

daggerok/gradle-kotlin-dsl-plugin-buildSrc

Repository files navigation

gradle-kotlin-dsl-plugin-buildSrc

Simple internal (located in buildSrc) Gradle plugin example, written in Kotlin using Gradle Kotlin DSL

generate a project

gradle init --type=basic --dsl=kotlin --project-name=gradle-kotlin-dsl-plugin-buildSrc

create buildSrc

mkdir -p buildSrc/src/main/kotlin/com/github/daggerok/plugin

configure 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)
}

plugin implementation

prepare structure for your configuration extension

create ./buildSrc/src/main/kotlin/com/github/daggerok/plugin/HelloPlugin.kt file:

open class HelloExtension {
  var greeting: String = "Hello"
  var name: String = "buddy"
}

prepare plugin

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()}!")
      }
    }
  }
}

plugin usage

plugins {
  hello
}

hello {
  greeting = "hola"
  name = "pablo"
}
./gradlew hello
# output
Hola, Pablo!

resources

About

Simple internal (located inside buildSrc folder) Gradle plugin example, written in Kotlin using Gradle Kotlin DSL

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published