Skip to content

Commit 7035c0f

Browse files
snicollwilkinsona
authored andcommitted
Upgrade to Kotlin 2.1.0
This commit upgrades to Kotlin 2.1.0. Two related dependencies have been updated as well: Kotlin Coroutines to 1.10, and Kotlin Serialization to 1.8. As of Kotlin 2, it is no longer possible to have a Java type and a Kotlin type with the same name. As our code samples follow that unfortunate pattern, this commit makes sure that the Kotlin sample code does not depend on any of the Java counterpart and configure the kotlin compilation plugin to ignore Java sources. The minimum version of Gradle is 7.6.4. It bundles a version of Kotlin that cannot compile a Kotlin build script when spring-core, compiled with Kotlin 2.1, is on the classpath. Using Gradle 8.12 to run the DSL tests avoids the problem. Closes gh-45486 Co-authored-by: Andy Wilkinson <andy.wilkinson@broadcom.com>
1 parent 5ce080f commit 7035c0f

File tree

30 files changed

+374
-69
lines changed

30 files changed

+374
-69
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,4 @@ secrets.yml
4141
.sts4-cache
4242
.git-hooks/
4343
node_modules
44+
/.kotlin/

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-artifact:${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/KotlinConventions.java

Lines changed: 10 additions & 12 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.
@@ -17,15 +17,15 @@
1717
package org.springframework.boot.build;
1818

1919
import java.net.URI;
20-
import java.util.ArrayList;
21-
import java.util.List;
2220

2321
import dev.adamko.dokkatoo.DokkatooExtension;
2422
import dev.adamko.dokkatoo.formats.DokkatooHtmlPlugin;
2523
import org.gradle.api.Project;
2624
import org.gradle.api.tasks.SourceSet;
2725
import org.gradle.api.tasks.SourceSetContainer;
28-
import org.jetbrains.kotlin.gradle.dsl.KotlinJvmOptions;
26+
import org.jetbrains.kotlin.gradle.dsl.JvmTarget;
27+
import org.jetbrains.kotlin.gradle.dsl.KotlinJvmCompilerOptions;
28+
import org.jetbrains.kotlin.gradle.dsl.KotlinVersion;
2929
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile;
3030

3131
/**
@@ -56,14 +56,12 @@ void apply(Project project) {
5656
}
5757

5858
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());
65-
freeCompilerArgs.add("-Xsuppress-version-warnings");
66-
kotlinOptions.setFreeCompilerArgs(freeCompilerArgs);
59+
KotlinJvmCompilerOptions compilerOptions = compile.getCompilerOptions();
60+
compilerOptions.getApiVersion().set(KotlinVersion.KOTLIN_2_1);
61+
compilerOptions.getLanguageVersion().set(KotlinVersion.KOTLIN_2_1);
62+
compilerOptions.getJvmTarget().set(JvmTarget.JVM_17);
63+
compilerOptions.getAllWarningsAsErrors().set(true);
64+
compilerOptions.getFreeCompilerArgs().addAll("-Xsuppress-version-warnings");
6765
}
6866

6967
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.4
1515
javaFormatVersion=0.0.43
1616
junitJupiterVersion=5.12.2
17-
kotlinVersion=1.9.25
17+
kotlinVersion=2.1.0
1818
mavenVersion=3.9.4
1919
mockitoVersion=5.17.0
2020
nativeBuildToolsVersion=0.10.6

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

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1174,10 +1174,6 @@ bom {
11741174
}
11751175
}
11761176
library("Kotlin", "${kotlinVersion}") {
1177-
prohibit {
1178-
versionRange "[2.0.0-Beta1,)"
1179-
because "it exceeds our baseline"
1180-
}
11811177
group("org.jetbrains.kotlin") {
11821178
bom("kotlin-bom")
11831179
plugins = [
@@ -1190,11 +1186,7 @@ bom {
11901186
releaseNotes("https://github.com/JetBrains/kotlin/releases/tag/v{version}")
11911187
}
11921188
}
1193-
library("Kotlin Coroutines", "1.8.1") {
1194-
prohibit {
1195-
versionRange "[1.9.0-RC,)"
1196-
because "it requires Kotlin 2"
1197-
}
1189+
library("Kotlin Coroutines", "1.10.1") {
11981190
group("org.jetbrains.kotlinx") {
11991191
bom("kotlinx-coroutines-bom")
12001192
}
@@ -1203,11 +1195,7 @@ bom {
12031195
releaseNotes("https://github.com/Kotlin/kotlinx.coroutines/releases/tag/{version}")
12041196
}
12051197
}
1206-
library("Kotlin Serialization", "1.6.3") {
1207-
prohibit {
1208-
versionRange "[1.7.0-RC,)"
1209-
because "it requires Kotlin 2"
1210-
}
1198+
library("Kotlin Serialization", "1.8.0") {
12111199
group("org.jetbrains.kotlinx") {
12121200
bom("kotlinx-serialization-bom")
12131201
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import org.jetbrains.kotlin.gradle.tasks.KotlinCompilationTask
12
import org.springframework.boot.build.docs.ConfigureJavadocLinks
23

34
plugins {
@@ -46,6 +47,11 @@ sourcesJar {
4647
enabled = false
4748
}
4849

50+
// To avoid a redeclaration error with Kotlin compiler
51+
tasks.named('compileKotlin', KotlinCompilationTask.class) {
52+
javaSources.from = []
53+
}
54+
4955
plugins.withType(EclipsePlugin) {
5056
eclipse.classpath { classpath ->
5157
classpath.plusConfigurations.add(configurations.getByName(sourceSets.main.runtimeClasspathConfigurationName))

spring-boot-project/spring-boot-docs/src/docs/antora/modules/reference/pages/features/kotlin.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Feel free to join the #spring channel of https://slack.kotlinlang.org/[Kotlin Sl
1515
[[features.kotlin.requirements]]
1616
== Requirements
1717

18-
Spring Boot requires at least Kotlin 1.7.x and manages a suitable Kotlin version through dependency management.
18+
Spring Boot requires at least Kotlin 2.1.x and manages a suitable Kotlin version through dependency management.
1919
To use Kotlin, `org.jetbrains.kotlin:kotlin-stdlib` and `org.jetbrains.kotlin:kotlin-reflect` must be present on the classpath.
2020
The `kotlin-stdlib` variants `kotlin-stdlib-jdk7` and `kotlin-stdlib-jdk8` can also be used.
2121

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* Copyright 2012-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.docs.data.nosql.cassandra.connecting
18+
19+
class User
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright 2012-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.docs.features.externalconfig.typesafeconfigurationproperties.usingannotatedtypes
18+
19+
class Server(remoteAddress: Any?) {
20+
fun start() {
21+
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* Copyright 2012-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.docs.features.springapplication.applicationavailability.managing
18+
19+
class CacheCompletelyBrokenException: RuntimeException()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright 2012-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.docs.features.testcontainers.atdevelopmenttime.importingcontainerdeclarations
18+
19+
class MyContainers {
20+
}

spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/features/testcontainers/atdevelopmenttime/importingcontainerdeclarations/MyContainersConfiguration.kt

Lines changed: 1 addition & 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-2024 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.
@@ -16,7 +16,6 @@
1616

1717
package org.springframework.boot.docs.features.testcontainers.atdevelopmenttime.importingcontainerdeclarations
1818

19-
import org.springframework.boot.docs.features.devservices.testcontainers.atdevelopmenttime.importingcontainerdeclarations.MyContainers
2019
import org.springframework.boot.test.context.TestConfiguration
2120
import org.springframework.boot.testcontainers.context.ImportTestcontainers
2221

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright 2012-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.docs.packaging.nativeimage.advanced.nestedconfigurationproperties
18+
19+
class Nested {
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright 2012-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.docs.using.springbeansanddependencyinjection.multipleconstructors
18+
19+
interface AccountService {
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright 2012-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.docs.using.springbeansanddependencyinjection.multipleconstructors
18+
19+
interface RiskAssessor {
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright 2012-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.docs.using.springbeansanddependencyinjection.singleconstructor
18+
19+
interface AccountService {
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright 2012-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.docs.using.springbeansanddependencyinjection.singleconstructor
18+
19+
interface RiskAssessor {
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* Copyright 2012-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.docs.using.usingthespringbootapplicationannotation.individualannotations
18+
19+
class AnotherConfiguration
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* Copyright 2012-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.docs.using.usingthespringbootapplicationannotation.individualannotations
18+
19+
class SomeConfiguration

0 commit comments

Comments
 (0)