In case you didn’t know, Gradle build scripts can be written in Kotlin rather than Groovy. However, as far as I know, the Kotlin DSL has never been properly documented. The closest to a documentation is the set of examples in the kotlin-dsl project
This README hopefully constitutes the temporary missing guide to migrate from the Groovy DSL to the Kotlin DSL.
It assumes you already know the Groovy DSL, and that you’re familiar with the Kotlin language syntax.
To use the Kotlin DSL, simply name your files build.gradle.kts
instead of build.gradle
.
The settings.gradle
file can also be renamed settings.gradle.kts
.
In a multi-project build, you can have some modules using the Groovy DSL (and thus use build.gradle
files), and some other modules using the Kotlin DSL (and thus use build.gradle.kts
files). So you’re not forced to migrate everything at once.
Using the plugins
block:
Groovy | Kotlin |
---|---|
plugins {
id('java')
id('jacoco')
} |
plugins {
java
id("jacoco")
} |
As you can see with the jacoco
example, the same syntax can be used in Groovy and Kotlin (except for double quotes that must be used for Strings in Kotlin, of course).
But the Kotlin DSL also defines extension properties for all (AFAIK) built-in plugins, so you can use them, as shown above with the java
example.
You can also use the older apply
syntax:
Groovy | Kotlin |
---|---|
apply plugin: 'checkstyle' |
plugins.apply("checkstyle") |
Using the plugins
block:
Groovy | Kotlin |
---|---|
plugins {
id 'org.flywaydb.flyway' version '5.0.3'
} |
plugins {
id("org.flywaydb.flyway") version "5.0.3"
} |
You can also use the older apply
syntax, but then the plugin must be added to the classpath of the build script:
Groovy | Kotlin |
---|---|
buildscript {
dependencies {
classpath 'org.springframework.boot:spring-boot-gradle-plugin:2.0.1.RELEASE'
}
}
repositories {
mavenCentral()
}
apply plugin: 'io.spring.dependency-management' |
buildscript {
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:2.0.1.RELEASE")
}
}
repositories {
mavenCentral()
}
plugins.apply("io.spring.dependency-management") |