Here, it continues from Kotlin 1.2.0 + Spring: create Web Application (3 of 4).
Write Gradle in Kotlin
Write build.gradle in Kotlin
build.gradle is also written in Kotlin. In the same directory with build.gradle, create build.gradle.kts and write as follows.
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 42 43 44 45 46 |
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile group = "com.task" 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") } } plugins { id("org.springframework.boot") version "1.5.8.RELEASE" } apply { plugin("kotlin") } repositories { mavenCentral() jcenter() } dependencies { val kotlinVersion = extra["kotlin_version"] as String compile("org.jetbrains.kotlin:kotlin-stdlib-jre8:$kotlinVersion") compile("org.springframework.boot:spring-boot-starter-web:1.5.8.RELEASE") compile("org.springframework.boot:spring-boot-starter-data-jpa") compile("org.postgresql:postgresql:42.1.4") val kotlinxHtmlVersion = "0.6.6" compile("org.jetbrains.kotlinx:kotlinx-html-jvm:$kotlinxHtmlVersion") } tasks.withType<KotlinCompile>(KotlinCompile::class.java).all { kotlinOptions { jvmTarget = "1.8" } } |
Then, delete build.gradle
. Execute ./gradlew clean && ./gradlew bootrun
, then the server will work.
In the earlier version Gradle than 4.0, we must add one line into settings.gradle
. From Kotlin DSL v0.9.0, it doesn’t need rootProject.buildFileName
for writing build.gradle.kts
. Kotlin DSL v0.9.0 is included in Gradle 4.0.
1 2 3 |
rootProject.name = 'myapp' // line required in the earlier Gradle than version 4.0 rootProject.buildFileName = 'build.gradle.kts' |
Write settings.gradle in Kotlin
Kotlin DSL v0.13.1 can also write settings.gradle.kts
. Gradle 4.4 RC1 includes it. In Intellij IDEA, Kotlin plugin of the version 1.1.60-eap-43 supports it.
Rename settings.gradle
to settings.gradle.kts
. In settings.gradle
, both single and double quotes represents string, but only double quotes represents string in Kotllin. Then, change rootProject.name
value.
Before | rootProject.name = 'myapp' |
---|---|
After | rootProject.name = “myapp” |
The project is publicized in GitHub: Kotlin 1.2.0 Spring Sample.