Skip to content

Commit d757a21

Browse files
committed
Upgrade to Kotlin 2.1.10
- Upgrade kotlinVersion to 2.1.10 - Remove org.jetbrains.kotlin:kotlin-compiler-embeddable from dependencies - Replace deprecated kotlinOptions with compilerOptions - Update KotlinConventions of ConventionsPlugin to set Kotlin apiVersion and languageVersion to 2.1 - Fix redeclaration error with Kotlin compiler on spring-boot-doc - Upgrade PluginClasspathGradleBuild to inject missed Kotlin compiler related jars See gh-38539 Signed-off-by: Dmitry Sulman <[email protected]>
1 parent 7b9da78 commit d757a21

File tree

12 files changed

+72
-25
lines changed

12 files changed

+72
-25
lines changed

buildSrc/build.gradle

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ dependencies {
4242
implementation("org.apache.maven:maven-embedder:${mavenVersion}")
4343
implementation("org.antora:gradle-antora-plugin:1.0.0")
4444
implementation("org.jetbrains.kotlin:kotlin-gradle-plugin:${kotlinVersion}")
45-
implementation("org.jetbrains.kotlin:kotlin-compiler-embeddable:${kotlinVersion}")
4645
implementation("org.springframework:spring-context")
4746
implementation("org.springframework:spring-core")
4847
implementation("org.springframework:spring-web")

buildSrc/src/main/java/org/springframework/boot/build/ConventionsPlugin.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2024 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -35,6 +35,9 @@
3535
* When the {@link AntoraPlugin} is applied, the conventions in {@link AntoraConventions}
3636
* are applied.
3737
*
38+
* When the {@code org.jetbrains.kotlin.jvm} plugin is applied, the conventions in
39+
* {@link KotlinConventions} are applied.
40+
*
3841
* @author Andy Wilkinson
3942
* @author Christoph Dreis
4043
* @author Mike Smithson

buildSrc/src/main/java/org/springframework/boot/build/KotlinConventions.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2024 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -25,7 +25,9 @@
2525
import org.gradle.api.Project;
2626
import org.gradle.api.tasks.SourceSet;
2727
import org.gradle.api.tasks.SourceSetContainer;
28-
import org.jetbrains.kotlin.gradle.dsl.KotlinJvmOptions;
28+
import org.jetbrains.kotlin.gradle.dsl.JvmTarget;
29+
import org.jetbrains.kotlin.gradle.dsl.KotlinJvmCompilerOptions;
30+
import org.jetbrains.kotlin.gradle.dsl.KotlinVersion;
2931
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile;
3032

3133
/**
@@ -35,7 +37,7 @@
3537
* <ul>
3638
* <li>{@link KotlinCompile} tasks are configured to:
3739
* <ul>
38-
* <li>Use {@code apiVersion} and {@code languageVersion} 1.7.
40+
* <li>Use {@code apiVersion} and {@code languageVersion} 2.1.
3941
* <li>Use {@code jvmTarget} 17.
4042
* <li>Treat all warnings as errors
4143
* <li>Suppress version warnings
@@ -56,14 +58,14 @@ void apply(Project project) {
5658
}
5759

5860
private void configure(KotlinCompile compile) {
59-
KotlinJvmOptions kotlinOptions = compile.getKotlinOptions();
60-
kotlinOptions.setApiVersion("1.7");
61-
kotlinOptions.setLanguageVersion("1.7");
62-
kotlinOptions.setJvmTarget("17");
63-
kotlinOptions.setAllWarningsAsErrors(true);
64-
List<String> freeCompilerArgs = new ArrayList<>(kotlinOptions.getFreeCompilerArgs());
61+
KotlinJvmCompilerOptions kotlinOptions = compile.getCompilerOptions();
62+
kotlinOptions.getApiVersion().set(KotlinVersion.KOTLIN_2_1);
63+
kotlinOptions.getLanguageVersion().set(KotlinVersion.KOTLIN_2_1);
64+
kotlinOptions.getJvmTarget().set(JvmTarget.JVM_17);
65+
kotlinOptions.getAllWarningsAsErrors().set(true);
66+
List<String> freeCompilerArgs = new ArrayList<>(kotlinOptions.getFreeCompilerArgs().get());
6567
freeCompilerArgs.add("-Xsuppress-version-warnings");
66-
kotlinOptions.setFreeCompilerArgs(freeCompilerArgs);
68+
kotlinOptions.getFreeCompilerArgs().set(freeCompilerArgs);
6769
}
6870

6971
private void configureDokkatoo(Project project) {

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ hamcrestVersion=3.0
1414
jacksonVersion=2.18.2
1515
javaFormatVersion=0.0.43
1616
junitJupiterVersion=5.11.4
17-
kotlinVersion=1.9.25
17+
kotlinVersion=2.1.10
1818
mavenVersion=3.9.4
1919
mockitoVersion=5.15.2
2020
nativeBuildToolsVersion=0.10.5

spring-boot-project/spring-boot-dependencies/build.gradle

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1193,8 +1193,9 @@ bom {
11931193
}
11941194
library("Kotlin", "${kotlinVersion}") {
11951195
prohibit {
1196-
versionRange "[2.0.0-Beta1,)"
1197-
because "it exceeds our baseline"
1196+
contains "-Beta"
1197+
contains "-RC"
1198+
because "we don't want betas or release candidates"
11981199
}
11991200
group("org.jetbrains.kotlin") {
12001201
imports = [

spring-boot-project/spring-boot-docs/build.gradle

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
2+
13
plugins {
24
id "dev.adamko.dokkatoo-html"
35
id "java"
@@ -304,6 +306,26 @@ task runLoggingFormatExample(type: org.springframework.boot.build.docs.Applicati
304306
normalizeTomcatPort()
305307
}
306308

309+
// To avoid a redeclaration error with Kotlin compiler
310+
tasks.withType(KotlinCompile) {
311+
javaSources.from = [
312+
"src/main/java/org/springframework/boot/docs/data/nosql/cassandra/connecting/User.java",
313+
"src/main/java/org/springframework/boot/docs/features/devservices/testcontainers/atdevelopmenttime/importingcontainerdeclarations/MyContainers.java",
314+
"src/main/java/org/springframework/boot/docs/features/externalconfig/typesafeconfigurationproperties/usingannotatedtypes/Server.java",
315+
"src/main/java/org/springframework/boot/docs/features/springapplication/applicationavailability/managing/CacheCompletelyBrokenException.java",
316+
"src/main/java/org/springframework/boot/docs/packaging/nativeimage/advanced/nestedconfigurationproperties/Nested.java",
317+
"src/main/java/org/springframework/boot/docs/using/springbeansanddependencyinjection/multipleconstructors/AccountService.java",
318+
"src/main/java/org/springframework/boot/docs/using/springbeansanddependencyinjection/multipleconstructors/RiskAssessor.java",
319+
"src/main/java/org/springframework/boot/docs/using/springbeansanddependencyinjection/singleconstructor/AccountService.java",
320+
"src/main/java/org/springframework/boot/docs/using/springbeansanddependencyinjection/singleconstructor/RiskAssessor.java",
321+
"src/main/java/org/springframework/boot/docs/using/usingthespringbootapplicationannotation/individualannotations/AnotherConfiguration.java",
322+
"src/main/java/org/springframework/boot/docs/using/usingthespringbootapplicationannotation/individualannotations/SomeConfiguration.java",
323+
"src/main/java/org/springframework/boot/docs/web/servlet/springmvc/errorhandling/MyErrorBody.java",
324+
"src/main/java/org/springframework/boot/docs/web/servlet/springmvc/errorhandling/MyException.java",
325+
"src/main/java/org/springframework/boot/docs/web/servlet/springmvc/errorhandling/SomeController.java",
326+
]
327+
}
328+
307329
def getRelativeExamplesPath(var outputs) {
308330
def fileName = outputs.files.singleFile.name
309331
'example$example-output/' + fileName

spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/build.gradle

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ dependencies {
5454
testImplementation("org.assertj:assertj-core")
5555
testImplementation("org.graalvm.buildtools:native-gradle-plugin")
5656
testImplementation("org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion")
57-
testImplementation("org.jetbrains.kotlin:kotlin-compiler-embeddable:$kotlinVersion")
5857
testImplementation("org.jetbrains.kotlin:kotlin-compiler-runner:$kotlinVersion")
5958
testImplementation("org.jetbrains.kotlin:kotlin-daemon-client:$kotlinVersion")
6059
testImplementation("org.junit.jupiter:junit-jupiter")

spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/plugin/KotlinPluginAction.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -49,7 +49,7 @@ private String getKotlinVersion(Project project) {
4949
private void enableJavaParametersOption(Project project) {
5050
project.getTasks()
5151
.withType(KotlinCompile.class)
52-
.configureEach((compile) -> compile.getKotlinOptions().setJavaParameters(true));
52+
.configureEach((compile) -> compile.getCompilerOptions().getJavaParameters().set(true));
5353
}
5454

5555
@Override

spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/plugin/KotlinPluginActionIntegrationTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2024 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -83,7 +83,7 @@ void taskConfigurationIsAvoided() throws IOException {
8383
configured.add(line.substring("Configuring :".length()));
8484
}
8585
}
86-
assertThat(configured).containsExactlyInAnyOrder("help", "compileJava", "clean");
86+
assertThat(configured).containsExactlyInAnyOrder("help", "clean");
8787
}
8888

8989
}

spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/testkit/PluginClasspathGradleBuild.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.io.File;
2020
import java.io.IOException;
21+
import java.util.ArrayList;
2122
import java.util.Arrays;
2223
import java.util.List;
2324

@@ -33,9 +34,12 @@
3334
import org.apache.hc.core5.http.HttpRequest;
3435
import org.apache.hc.core5.http2.HttpVersionPolicy;
3536
import org.gradle.testkit.runner.GradleRunner;
37+
import org.gradle.util.GradleVersion;
38+
import org.jetbrains.kotlin.buildtools.api.jvm.ClassSnapshotGranularity;
3639
import org.jetbrains.kotlin.gradle.model.KotlinProject;
3740
import org.jetbrains.kotlin.gradle.plugin.KotlinCompilerPluginSupportPlugin;
3841
import org.jetbrains.kotlin.gradle.plugin.KotlinPlatformJvmPlugin;
42+
import org.jetbrains.kotlin.konan.target.HostManager;
3943
import org.jetbrains.kotlin.project.model.LanguageSettings;
4044
import org.jetbrains.kotlin.tooling.core.KotlinToolingVersion;
4145
import org.tomlj.Toml;
@@ -55,6 +59,8 @@
5559
*/
5660
public class PluginClasspathGradleBuild extends GradleBuild {
5761

62+
private static final GradleVersion GRADLE_VERSION_8_10 = GradleVersion.version("8.10");
63+
5864
public PluginClasspathGradleBuild() {
5965
super();
6066
}
@@ -69,6 +75,15 @@ public GradleRunner prepareRunner(String... arguments) throws IOException {
6975
}
7076

7177
private List<File> pluginClasspath() {
78+
List<File> pluginClasspath = new ArrayList<>(basePluginClasspath());
79+
if (this.getGradleVersion() == null
80+
|| GradleVersion.version(this.getGradleVersion()).compareTo(GRADLE_VERSION_8_10) > 0) {
81+
pluginClasspath.addAll(classpathAdditionsForGradleVersionsWithEmbeddedKotlin2());
82+
}
83+
return pluginClasspath;
84+
}
85+
86+
private List<File> basePluginClasspath() {
7287
return Arrays.asList(new File("bin/main"), new File("build/classes/java/main"),
7388
new File("build/resources/main"), new File(pathOfJarContaining(LaunchScript.class)),
7489
new File(pathOfJarContaining(ClassVisitor.class)),
@@ -94,6 +109,12 @@ private List<File> pluginClasspath() {
94109
new File(pathOfJarContaining("org.graalvm.buildtools.utils.SharedConstants")));
95110
}
96111

112+
private List<File> classpathAdditionsForGradleVersionsWithEmbeddedKotlin2() {
113+
return Arrays.asList(new File(pathOfJarContaining(ClassSnapshotGranularity.class)),
114+
new File(pathOfJarContaining("org.jetbrains.kotlin.build.report.metrics.GradleBuildTime")),
115+
new File(pathOfJarContaining(HostManager.class)));
116+
}
117+
97118
private String pathOfJarContaining(String className) {
98119
try {
99120
return pathOfJarContaining(Class.forName(className));

spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/resources/org/springframework/boot/gradle/plugin/KotlinPluginActionIntegrationTests-kotlinCompileTasksCanOverrideDefaultJavaParametersFlag.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@ plugins {
44

55
apply plugin: 'org.jetbrains.kotlin.jvm'
66

7-
import org.jetbrains.kotlin.gradle.dsl.KotlinCompile
7+
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
88

99
tasks.withType(KotlinCompile) {
10-
kotlinOptions.javaParameters = false
10+
compilerOptions.javaParameters = false
1111
}
1212

1313
task('kotlinCompileTasksJavaParameters') {
1414
doFirst {
1515
tasks.withType(KotlinCompile) {
16-
println "${name} java parameters: ${kotlinOptions.javaParameters}"
16+
println "${name} java parameters: ${compilerOptions.javaParameters.get()}"
1717
}
1818
}
1919
}

spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/resources/org/springframework/boot/gradle/plugin/KotlinPluginActionIntegrationTests-kotlinCompileTasksUseJavaParametersFlagByDefault.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ plugins {
44

55
apply plugin: 'org.jetbrains.kotlin.jvm'
66

7-
import org.jetbrains.kotlin.gradle.dsl.KotlinCompile
7+
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
88

99
task('kotlinCompileTasksJavaParameters') {
1010
doFirst {
1111
tasks.withType(KotlinCompile) {
12-
println "${name} java parameters: ${kotlinOptions.javaParameters}"
12+
println "${name} java parameters: ${compilerOptions.javaParameters.get()}"
1313
}
1414
}
1515
}

0 commit comments

Comments
 (0)