From 6c5f47a49fde548212184f6687219be6097f7ae2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Deleuze?= Date: Mon, 9 Jan 2023 11:35:54 +0100 Subject: [PATCH] Upgrade to Spring Boot 3 and Kotlin 1.8 --- README.adoc | 55 +++++++------------ build.gradle.kts | 27 +++++---- gradle/wrapper/gradle-wrapper.properties | 2 +- pom.xml | 26 +++------ .../kotlin/com/example/blog/BlogProperties.kt | 2 - src/main/kotlin/com/example/blog/Entities.kt | 2 +- 6 files changed, 43 insertions(+), 71 deletions(-) diff --git a/README.adoc b/README.adoc index 51cfbfe..83e7921 100644 --- a/README.adoc +++ b/README.adoc @@ -82,11 +82,11 @@ In order to be able to use Kotlin non-nullable properties with JPA, https://kotl import org.jetbrains.kotlin.gradle.tasks.KotlinCompile plugins { - kotlin("plugin.jpa") version "1.6.21" - id("org.springframework.boot") version "2.7.1" - id("io.spring.dependency-management") version "1.0.11.RELEASE" - kotlin("jvm") version "1.6.21" - kotlin("plugin.spring") version "1.6.21" + id("org.springframework.boot") version "3.0.1" + id("io.spring.dependency-management") version "1.1.0" + kotlin("jvm") version "1.8.0" + kotlin("plugin.spring") version "1.8.0" + kotlin("plugin.jpa") version "1.8.0" } ---- @@ -110,9 +110,8 @@ tasks.withType { === Dependencies -3 Kotlin specific libraries are required for such Spring Boot web application and configured by default: +2 Kotlin specific libraries are required for such Spring Boot web application and configured by default: - - `kotlin-stdlib-jdk8` is the Java 8 variant of Kotlin standard library - `kotlin-reflect` is Kotlin reflection library - `jackson-module-kotlin` adds support for serialization/deserialization of Kotlin classes and data classes (single constructor classes can be used automatically, and those with secondary constructors or static factories are also supported) @@ -125,7 +124,6 @@ dependencies { implementation("org.springframework.boot:spring-boot-starter-web") implementation("com.fasterxml.jackson.module:jackson-module-kotlin") implementation("org.jetbrains.kotlin:kotlin-reflect") - implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8") runtimeOnly("com.h2database:h2") runtimeOnly("org.springframework.boot:spring-boot-devtools") testImplementation("org.springframework.boot:spring-boot-starter-test") @@ -206,8 +204,8 @@ Notice also that Kotlin compiler is configured to generate Java 8 bytecode (Java 3 Kotlin specific libraries are required for such Spring Boot web application and configured by default: - - `kotlin-stdlib-jdk8` is the Java 8 variant of Kotlin standard library - - `kotlin-reflect` is Kotlin reflection library (mandatory as of Spring Framework 5) + - `kotlin-stdlib` is the Kotlin standard library + - `kotlin-reflect` is Kotlin reflection library - `jackson-module-kotlin` adds support for serialization/deserialization of Kotlin classes and data classes (single constructor classes can be used automatically, and those with secondary constructors or static factories are also supported) `pom.xml` @@ -236,7 +234,7 @@ Notice also that Kotlin compiler is configured to generate Java 8 bytecode (Java org.jetbrains.kotlin - kotlin-stdlib-jdk8 + kotlin-stdlib @@ -470,13 +468,13 @@ With Gradle: ---- plugins { ... - kotlin("plugin.allopen") version "1.6.21" + kotlin("plugin.allopen") version "1.8.0" } allOpen { - annotation("javax.persistence.Entity") - annotation("javax.persistence.Embeddable") - annotation("javax.persistence.MappedSuperclass") + annotation("jakarta.persistence.Entity") + annotation("jakarta.persistence.Embeddable") + annotation("jakarta.persistence.MappedSuperclass") } ---- @@ -495,9 +493,9 @@ Or with Maven: all-open - - - + + + @@ -796,12 +794,11 @@ With Gradle: [source,kotlin] ---- testImplementation("org.springframework.boot:spring-boot-starter-test") { - exclude(module = "junit") exclude(module = "mockito-core") } testImplementation("org.junit.jupiter:junit-jupiter-api") testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine") -testImplementation("com.ninja-squad:springmockk:3.0.1") +testImplementation("com.ninja-squad:springmockk:4.0.0") ---- Or with Maven: @@ -813,16 +810,6 @@ Or with Maven: org.springframework.boot spring-boot-starter-test test - - - junit - junit - - - org.mockito - mockito-core - - org.junit.jupiter @@ -832,7 +819,7 @@ Or with Maven: com.ninja-squad springmockk - 3.0.1 + 4.0.0 test ---- @@ -882,13 +869,11 @@ NOTE: `$` needs to be escaped in strings as it is used for string interpolation. == Configuration properties -In Kotlin, the recommended way to manage your application properties is to leverage `@ConfigurationProperties` with -`@ConstructorBinding` in order to be able to use read-only properties. +In Kotlin, the recommended way to manage your application properties is to use read-only properties. `src/main/kotlin/com/example/blog/BlogProperties.kt` [source,kotlin] ---- -@ConstructorBinding @ConfigurationProperties("blog") data class BlogProperties(var title: String, val banner: Banner) { data class Banner(val title: String? = null, val content: String) @@ -914,7 +899,7 @@ To generate https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle ---- plugins { ... - kotlin("kapt") version "1.6.21" + kotlin("kapt") version "1.8.0" } dependencies { diff --git a/build.gradle.kts b/build.gradle.kts index 7641ae7..b9452b4 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,18 +1,18 @@ import org.jetbrains.kotlin.gradle.tasks.KotlinCompile plugins { - id("org.springframework.boot") version "2.7.1" - id("io.spring.dependency-management") version "1.0.11.RELEASE" - kotlin("jvm") version "1.6.21" - kotlin("plugin.spring") version "1.6.21" - kotlin("plugin.allopen") version "1.6.21" - kotlin("plugin.jpa") version "1.6.21" - kotlin("kapt") version "1.6.21" + id("org.springframework.boot") version "3.0.1" + id("io.spring.dependency-management") version "1.1.0" + kotlin("jvm") version "1.8.0" + kotlin("plugin.spring") version "1.8.0" + kotlin("plugin.allopen") version "1.8.0" + kotlin("plugin.jpa") version "1.8.0" + kotlin("kapt") version "1.8.0" } group = "com.example" version = "0.0.1-SNAPSHOT" -java.sourceCompatibility = JavaVersion.VERSION_1_8 +java.sourceCompatibility = JavaVersion.VERSION_17 repositories { mavenCentral() @@ -24,29 +24,28 @@ dependencies { implementation("org.springframework.boot:spring-boot-starter-web") implementation("com.fasterxml.jackson.module:jackson-module-kotlin") implementation("org.jetbrains.kotlin:kotlin-reflect") - implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8") runtimeOnly("com.h2database:h2") runtimeOnly("org.springframework.boot:spring-boot-devtools") kapt("org.springframework.boot:spring-boot-configuration-processor") testImplementation("org.springframework.boot:spring-boot-starter-test") { - exclude(group = "org.junit.vintage", module = "junit-vintage-engine") exclude(module = "mockito-core") } testImplementation("org.junit.jupiter:junit-jupiter-api") testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine") - testImplementation("com.ninja-squad:springmockk:3.1.1") + testImplementation("com.ninja-squad:springmockk:4.0.0") } tasks.withType { kotlinOptions { + jvmTarget = "17" freeCompilerArgs += "-Xjsr305=strict" } } allOpen { - annotation("javax.persistence.Entity") - annotation("javax.persistence.Embeddable") - annotation("javax.persistence.MappedSuperclass") + annotation("jakarta.persistence.Entity") + annotation("jakarta.persistence.Embeddable") + annotation("jakarta.persistence.MappedSuperclass") } tasks.withType { diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 00e33ed..070cb70 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,5 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-7.4.1-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-7.6-bin.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists diff --git a/pom.xml b/pom.xml index 5fe9e5f..d812d40 100644 --- a/pom.xml +++ b/pom.xml @@ -14,15 +14,15 @@ org.springframework.boot spring-boot-starter-parent - 2.7.1 + 3.0.1 UTF-8 UTF-8 - 1.8 - 1.6.21 + 17 + 1.8.0 @@ -44,7 +44,7 @@ org.jetbrains.kotlin - kotlin-stdlib-jdk8 + kotlin-stdlib org.jetbrains.kotlin @@ -65,16 +65,6 @@ org.springframework.boot spring-boot-starter-test test - - - org.junit.vintage - junit-vintage-engine - - - org.mockito - mockito-core - - org.junit.jupiter @@ -84,7 +74,7 @@ com.ninja-squad springmockk - 3.1.1 + 4.0.0 test @@ -110,9 +100,9 @@ all-open - - - + + + diff --git a/src/main/kotlin/com/example/blog/BlogProperties.kt b/src/main/kotlin/com/example/blog/BlogProperties.kt index 2e29585..97eedfd 100644 --- a/src/main/kotlin/com/example/blog/BlogProperties.kt +++ b/src/main/kotlin/com/example/blog/BlogProperties.kt @@ -1,9 +1,7 @@ package com.example.blog import org.springframework.boot.context.properties.ConfigurationProperties -import org.springframework.boot.context.properties.ConstructorBinding -@ConstructorBinding @ConfigurationProperties("blog") data class BlogProperties(var title: String, val banner: Banner) { data class Banner(val title: String? = null, val content: String) diff --git a/src/main/kotlin/com/example/blog/Entities.kt b/src/main/kotlin/com/example/blog/Entities.kt index e32543e..308f930 100644 --- a/src/main/kotlin/com/example/blog/Entities.kt +++ b/src/main/kotlin/com/example/blog/Entities.kt @@ -1,7 +1,7 @@ package com.example.blog import java.time.LocalDateTime -import javax.persistence.* +import jakarta.persistence.* @Entity class Article(