Table of Contents
I summarized the way to write build.gradle
and settings.gradle
in Kotlin.
Environment
- Kotlin 1.2.0
- Gradle 4.4 RC 5
Required
Gradle 3.0 M1 or higher is required for writing build.gradle
in Kotlin, Gradle 4.4 RC1 or higher is required for writing settings.gradle.kts
in Kotlin.
build.gradle
Here is a sample in Kotlin.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
import org.gradle.api.* import org.gradle.kotlin.dsl.* import org.gradle.plugin.* import org.gradle.script.lang.kotlin.* import org.jetbrains.kotlin.gradle.tasks.KotlinCompile group = "com.improve_future" version = "1.0-SNAPSHOT" buildscript { val kotlinVersion = "1.2.0" extra["kotlin_version"] = kotlinVersion repositories { mavenCentral() } dependencies { classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion") } } apply { plugin("kotlin") } repositories { mavenCentral() jcenter() } dependencies { val kotlinVersion = extra["kotlin_version"] as String compile("org.jetbrains.kotlin:kotlin-stdlib-jre8:$kotlinVersion") } tasks.withType<KotlinCompile>(KotlinCompile::class.java).all { kotlinOptions { jvmTarget = "1.8" } } |
From Gradle 4.0 RC1, it is not needed because Kotlin DSL 0.9.0 is included, but when you use earlier Gradle, you have to write the following line into settings.gradle
.
1 |
rootProject.buildFileName = 'build.gradle.kts' |
filter example is in Spring Boot のアプリケーションを AWS EC2 にデプロイする手順.
settings.gradle
This is not so different from one in Groovy.
1 |
(rootProject as ProjectDescriptor).name = "backlog_board" |
Intellij IDEA consider rootProject
as the interface without getName
method, so I cast it to ProjectDescriptor
. It can be also built successfully without cast.