https://github.com/daggerok/gradle-kotlin-dsl-plugin-buildsrc
Simple internal (located inside buildSrc folder) Gradle plugin example, written in Kotlin using Gradle Kotlin DSL
https://github.com/daggerok/gradle-kotlin-dsl-plugin-buildsrc
gradle-kotlin-dsl gradle-plugin gradle-plugin-kotlin
Last synced: 20 days ago
JSON representation
Simple internal (located inside buildSrc folder) Gradle plugin example, written in Kotlin using Gradle Kotlin DSL
- Host: GitHub
- URL: https://github.com/daggerok/gradle-kotlin-dsl-plugin-buildsrc
- Owner: daggerok
- Created: 2019-03-01T00:18:47.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2019-03-01T01:20:00.000Z (about 6 years ago)
- Last Synced: 2025-04-05T10:23:11.705Z (about 1 month ago)
- Topics: gradle-kotlin-dsl, gradle-plugin, gradle-plugin-kotlin
- Language: Kotlin
- Size: 343 KB
- Stars: 6
- Watchers: 2
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# gradle-kotlin-dsl-plugin-buildSrc
Simple internal (located in `buildSrc`) Gradle plugin example, written in Kotlin using Gradle Kotlin DSL- Travis CI [](https://travis-ci.org/daggerok/gradle-kotlin-dsl-plugin-buildSrc)
- GitHub [Pages](https://daggerok.github.io/gradle-kotlin-dsl-plugin-buildSrc/) documentation
- GitHub [daggerok/gradle-kotlin-dsl-plugin-buildSrc](https://github.com/daggerok/gradle-kotlin-dsl-plugin-buildSrc) repository## generate a project
```bash
gradle init --type=basic --dsl=kotlin --project-name=gradle-kotlin-dsl-plugin-buildSrc
```## create buildSrc
```bash
mkdir -p buildSrc/src/main/kotlin/com/github/daggerok/plugin
```## configure plugin
create `buildSrc/build.gradle.kts` file:
```kotlin
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 {
experimentalWarning.set(false)
}
```## plugin implementation
### prepare structure for your configuration extension
create `./buildSrc/src/main/kotlin/com/github/daggerok/plugin/HelloPlugin.kt` file:
```kotlin
open class HelloExtension {
var greeting: String = "Hello"
var name: String = "buddy"
}
```### prepare plugin
```kotlin
class HelloPlugin : Plugin {
override fun apply(project: Project): Unit = project.run {
val hello = project.extensions.create("HelloExtension")
project.extensions.add("hello", hello)
tasks.register("hello") {
doLast {
println("${hello.greeting.capitalize()}, ${hello.name.capitalize()}!")
}
}
}
}
```## plugin usage
```kotlin
plugins {
hello
}hello {
greeting = "hola"
name = "pablo"
}
``````bash
./gradlew hello
# output
Hola, Pablo!
```## resources
- [Writing Custom Plugins](https://docs.gradle.org/current/userguide/custom_plugins.html)
- [Gradle Kotlin DSL samples](https://github.com/gradle/kotlin-dsl/tree/master/samples)
- [Gradle Kotlin DSL Primer](https://docs.gradle.org/current/userguide/kotlin_dsl.html#sec:kotlin-dsl_plugin)
- [Gradle Tutorials](https://gradle.org/guides/)