From b086e38bcb50c6b341f7d60a95a3942533f4986d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Deleuze?= Date: Tue, 1 Jul 2025 16:30:03 +0200 Subject: [PATCH] Upgrade to Kotlin 2.2.0 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit leverages Kotlin 2.2 which is expected to be the new baseline for Spring Boot 4.0, so API and language versions are upgraded to KOTLIN_2_2 as well. It also enables the -Xannotation-default-target=param-property compiler flag, mainly for test purpose, in order to avoid the related warnings and to use what will likely be the default in an upcoming Kotlin version short term. For more details see https://youtrack.jetbrains.com/issue/KT-73255. KotlinPlatformJvmPlugin was a class from an old KMP plugin, deprecated for a long time and now removed. So its usage in PluginClasspathGradleBuild has been removed as well. Signed-off-by: Sébastien Deleuze --- .../boot/build/KotlinConventions.java | 5 +++-- gradle.properties | 2 +- .../modules/reference/pages/features/kotlin.adoc | 13 ++++++------- .../gradle/testkit/PluginClasspathGradleBuild.java | 2 -- 4 files changed, 10 insertions(+), 12 deletions(-) diff --git a/buildSrc/src/main/java/org/springframework/boot/build/KotlinConventions.java b/buildSrc/src/main/java/org/springframework/boot/build/KotlinConventions.java index 06ab027d522f..decdb0e5ac4e 100644 --- a/buildSrc/src/main/java/org/springframework/boot/build/KotlinConventions.java +++ b/buildSrc/src/main/java/org/springframework/boot/build/KotlinConventions.java @@ -54,7 +54,7 @@ class KotlinConventions { private static final JvmTarget JVM_TARGET = JvmTarget.JVM_17; - private static final KotlinVersion KOTLIN_VERSION = KotlinVersion.KOTLIN_2_1; + private static final KotlinVersion KOTLIN_VERSION = KotlinVersion.KOTLIN_2_2; void apply(Project project) { project.getPlugins().withId("org.jetbrains.kotlin.jvm", (plugin) -> { @@ -70,7 +70,8 @@ private void configure(KotlinCompile compile) { compilerOptions.getLanguageVersion().set(KOTLIN_VERSION); compilerOptions.getJvmTarget().set(JVM_TARGET); compilerOptions.getAllWarningsAsErrors().set(true); - compilerOptions.getFreeCompilerArgs().addAll("-Xsuppress-version-warnings"); + compilerOptions.getFreeCompilerArgs() + .addAll("-Xsuppress-version-warnings", "-Xannotation-default-target=param-property"); } private void configureDokkatoo(Project project) { diff --git a/gradle.properties b/gradle.properties index 20dd09242fe8..883a87644627 100644 --- a/gradle.properties +++ b/gradle.properties @@ -14,7 +14,7 @@ hamcrestVersion=3.0 jacksonVersion=2.19.1 javaFormatVersion=0.0.47 junitJupiterVersion=5.13.1 -kotlinVersion=2.1.0 +kotlinVersion=2.2.0 mavenVersion=3.9.10 mockitoVersion=5.17.0 nativeBuildToolsVersion=0.10.6 diff --git a/spring-boot-project/spring-boot-docs/src/docs/antora/modules/reference/pages/features/kotlin.adoc b/spring-boot-project/spring-boot-docs/src/docs/antora/modules/reference/pages/features/kotlin.adoc index fdbf0c17487b..256237ddaabc 100644 --- a/spring-boot-project/spring-boot-docs/src/docs/antora/modules/reference/pages/features/kotlin.adoc +++ b/spring-boot-project/spring-boot-docs/src/docs/antora/modules/reference/pages/features/kotlin.adoc @@ -15,9 +15,10 @@ Feel free to join the #spring channel of https://slack.kotlinlang.org/[Kotlin Sl [[features.kotlin.requirements]] == Requirements -Spring Boot requires at least Kotlin 2.1.x and manages a suitable Kotlin version through dependency management. +Spring Boot requires at least Kotlin 2.2.x and manages a suitable Kotlin version through dependency management. To use Kotlin, `org.jetbrains.kotlin:kotlin-stdlib` and `org.jetbrains.kotlin:kotlin-reflect` must be present on the classpath. -The `kotlin-stdlib` variants `kotlin-stdlib-jdk7` and `kotlin-stdlib-jdk8` can also be used. + +Kotlin 2.2.x introduces https://kotlinlang.org/docs/whatsnew22.html#new-defaulting-rules-for-use-site-annotation-targets[new defaulting rules for propagating annotations to parameters, fields, and properties]. In order to avoid related warnings and use what will likely become the Kotlin default behavior in an upcoming version, it is recommended to configure the `-Xannotation-default-target=param-property` compiler flag. Since https://discuss.kotlinlang.org/t/classes-final-by-default/166[Kotlin classes are final by default], you are likely to want to configure {url-kotlin-docs}/compiler-plugins.html#spring-support[kotlin-spring] plugin in order to automatically open Spring-annotated classes so that they can be proxied. @@ -39,15 +40,13 @@ Kotlin also allows using functional constructs with nullable values as described Although Java does not allow one to express null-safety in its type system, Spring Framework, Spring Data, and Reactor now provide null-safety of their API through tooling-friendly annotations. By default, types from Java APIs used in Kotlin are recognized as {url-kotlin-docs}/java-interop.html#null-safety-and-platform-types[platform types] for which null-checks are relaxed. -{url-kotlin-docs}/java-interop.html#jsr-305-support[Kotlin's support for JSR 305 annotations] combined with nullability annotations provide null-safety for the related Spring API in Kotlin. +{url-kotlin-docs}/java-interop.html#nullability-annotations[Kotlin's support for nullability annotations] provides null-safety for the annotated APIs like Spring's one in Kotlin. -The JSR 305 checks can be configured by adding the `-Xjsr305` compiler flag with the following options: `-Xjsr305={strict|warn|ignore}`. +The https://jspecify.dev[JSpecify] strict checks are https://kotlinlang.org/docs/whatsnew21.html#change-of-jspecify-nullability-mismatch-diagnostics-severity-to-strict[enabled by default], while the JSR 305 checks can be configured by adding the `-Xjsr305` compiler flag with the following options: `-Xjsr305={strict|warn|ignore}`. The default behavior is the same as `-Xjsr305=warn`. The `strict` value is required to have null-safety taken in account in Kotlin types inferred from Spring API but should be used with the knowledge that Spring API nullability declaration could evolve even between minor releases and more checks may be added in the future). -WARNING: Generic type arguments, varargs and array elements nullability are not yet supported. -See https://jira.spring.io/browse/SPR-15942[SPR-15942] for up-to-date information. -Also be aware that Spring Boot's own API is {url-github-issues}/10712[not yet annotated]. +WARNING: Be aware that Spring Boot's own API is {url-github-issues}/10712[not yet annotated]. diff --git a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/testkit/PluginClasspathGradleBuild.java b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/testkit/PluginClasspathGradleBuild.java index a36563b4b269..94afafc35975 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/testkit/PluginClasspathGradleBuild.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/testkit/PluginClasspathGradleBuild.java @@ -35,7 +35,6 @@ import org.gradle.testkit.runner.GradleRunner; import org.jetbrains.kotlin.gradle.model.KotlinProject; import org.jetbrains.kotlin.gradle.plugin.KotlinCompilerPluginSupportPlugin; -import org.jetbrains.kotlin.gradle.plugin.KotlinPlatformJvmPlugin; import org.jetbrains.kotlin.project.model.LanguageSettings; import org.jetbrains.kotlin.tooling.core.KotlinToolingVersion; import org.tomlj.Toml; @@ -85,7 +84,6 @@ private List pluginClasspath() { classpath.add(new File(pathOfJarContaining(DependencyManagementPlugin.class))); if (this.kotlin) { classpath.add(new File(pathOfJarContaining("org.jetbrains.kotlin.cli.common.PropertiesKt"))); - classpath.add(new File(pathOfJarContaining(KotlinPlatformJvmPlugin.class))); classpath.add(new File(pathOfJarContaining(KotlinProject.class))); classpath.add(new File(pathOfJarContaining(KotlinToolingVersion.class))); classpath.add(new File(pathOfJarContaining("org.jetbrains.kotlin.build.report.metrics.BuildTime")));