Skip to content

Commit

Permalink
Upgrade to Kotlin 2.1.10
Browse files Browse the repository at this point in the history
- 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]>
  • Loading branch information
dmitrysulman committed Mar 1, 2025
1 parent 7b9da78 commit d757a21
Show file tree
Hide file tree
Showing 12 changed files with 72 additions and 25 deletions.
1 change: 0 additions & 1 deletion buildSrc/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ dependencies {
implementation("org.apache.maven:maven-embedder:${mavenVersion}")
implementation("org.antora:gradle-antora-plugin:1.0.0")
implementation("org.jetbrains.kotlin:kotlin-gradle-plugin:${kotlinVersion}")
implementation("org.jetbrains.kotlin:kotlin-compiler-embeddable:${kotlinVersion}")
implementation("org.springframework:spring-context")
implementation("org.springframework:spring-core")
implementation("org.springframework:spring-web")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2024 the original author or authors.
* Copyright 2012-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -35,6 +35,9 @@
* When the {@link AntoraPlugin} is applied, the conventions in {@link AntoraConventions}
* are applied.
*
* When the {@code org.jetbrains.kotlin.jvm} plugin is applied, the conventions in
* {@link KotlinConventions} are applied.
*
* @author Andy Wilkinson
* @author Christoph Dreis
* @author Mike Smithson
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2024 the original author or authors.
* Copyright 2012-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -25,7 +25,9 @@
import org.gradle.api.Project;
import org.gradle.api.tasks.SourceSet;
import org.gradle.api.tasks.SourceSetContainer;
import org.jetbrains.kotlin.gradle.dsl.KotlinJvmOptions;
import org.jetbrains.kotlin.gradle.dsl.JvmTarget;
import org.jetbrains.kotlin.gradle.dsl.KotlinJvmCompilerOptions;
import org.jetbrains.kotlin.gradle.dsl.KotlinVersion;
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile;

/**
Expand All @@ -35,7 +37,7 @@
* <ul>
* <li>{@link KotlinCompile} tasks are configured to:
* <ul>
* <li>Use {@code apiVersion} and {@code languageVersion} 1.7.
* <li>Use {@code apiVersion} and {@code languageVersion} 2.1.
* <li>Use {@code jvmTarget} 17.
* <li>Treat all warnings as errors
* <li>Suppress version warnings
Expand All @@ -56,14 +58,14 @@ void apply(Project project) {
}

private void configure(KotlinCompile compile) {
KotlinJvmOptions kotlinOptions = compile.getKotlinOptions();
kotlinOptions.setApiVersion("1.7");
kotlinOptions.setLanguageVersion("1.7");
kotlinOptions.setJvmTarget("17");
kotlinOptions.setAllWarningsAsErrors(true);
List<String> freeCompilerArgs = new ArrayList<>(kotlinOptions.getFreeCompilerArgs());
KotlinJvmCompilerOptions kotlinOptions = compile.getCompilerOptions();
kotlinOptions.getApiVersion().set(KotlinVersion.KOTLIN_2_1);
kotlinOptions.getLanguageVersion().set(KotlinVersion.KOTLIN_2_1);
kotlinOptions.getJvmTarget().set(JvmTarget.JVM_17);
kotlinOptions.getAllWarningsAsErrors().set(true);
List<String> freeCompilerArgs = new ArrayList<>(kotlinOptions.getFreeCompilerArgs().get());
freeCompilerArgs.add("-Xsuppress-version-warnings");
kotlinOptions.setFreeCompilerArgs(freeCompilerArgs);
kotlinOptions.getFreeCompilerArgs().set(freeCompilerArgs);
}

private void configureDokkatoo(Project project) {
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ hamcrestVersion=3.0
jacksonVersion=2.18.2
javaFormatVersion=0.0.43
junitJupiterVersion=5.11.4
kotlinVersion=1.9.25
kotlinVersion=2.1.10
mavenVersion=3.9.4
mockitoVersion=5.15.2
nativeBuildToolsVersion=0.10.5
Expand Down
5 changes: 3 additions & 2 deletions spring-boot-project/spring-boot-dependencies/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -1193,8 +1193,9 @@ bom {
}
library("Kotlin", "${kotlinVersion}") {
prohibit {
versionRange "[2.0.0-Beta1,)"
because "it exceeds our baseline"
contains "-Beta"
contains "-RC"
because "we don't want betas or release candidates"
}
group("org.jetbrains.kotlin") {
imports = [
Expand Down
22 changes: 22 additions & 0 deletions spring-boot-project/spring-boot-docs/build.gradle
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
id "dev.adamko.dokkatoo-html"
id "java"
Expand Down Expand Up @@ -304,6 +306,26 @@ task runLoggingFormatExample(type: org.springframework.boot.build.docs.Applicati
normalizeTomcatPort()
}

// To avoid a redeclaration error with Kotlin compiler
tasks.withType(KotlinCompile) {
javaSources.from = [
"src/main/java/org/springframework/boot/docs/data/nosql/cassandra/connecting/User.java",
"src/main/java/org/springframework/boot/docs/features/devservices/testcontainers/atdevelopmenttime/importingcontainerdeclarations/MyContainers.java",
"src/main/java/org/springframework/boot/docs/features/externalconfig/typesafeconfigurationproperties/usingannotatedtypes/Server.java",
"src/main/java/org/springframework/boot/docs/features/springapplication/applicationavailability/managing/CacheCompletelyBrokenException.java",
"src/main/java/org/springframework/boot/docs/packaging/nativeimage/advanced/nestedconfigurationproperties/Nested.java",
"src/main/java/org/springframework/boot/docs/using/springbeansanddependencyinjection/multipleconstructors/AccountService.java",
"src/main/java/org/springframework/boot/docs/using/springbeansanddependencyinjection/multipleconstructors/RiskAssessor.java",
"src/main/java/org/springframework/boot/docs/using/springbeansanddependencyinjection/singleconstructor/AccountService.java",
"src/main/java/org/springframework/boot/docs/using/springbeansanddependencyinjection/singleconstructor/RiskAssessor.java",
"src/main/java/org/springframework/boot/docs/using/usingthespringbootapplicationannotation/individualannotations/AnotherConfiguration.java",
"src/main/java/org/springframework/boot/docs/using/usingthespringbootapplicationannotation/individualannotations/SomeConfiguration.java",
"src/main/java/org/springframework/boot/docs/web/servlet/springmvc/errorhandling/MyErrorBody.java",
"src/main/java/org/springframework/boot/docs/web/servlet/springmvc/errorhandling/MyException.java",
"src/main/java/org/springframework/boot/docs/web/servlet/springmvc/errorhandling/SomeController.java",
]
}

def getRelativeExamplesPath(var outputs) {
def fileName = outputs.files.singleFile.name
'example$example-output/' + fileName
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ dependencies {
testImplementation("org.assertj:assertj-core")
testImplementation("org.graalvm.buildtools:native-gradle-plugin")
testImplementation("org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion")
testImplementation("org.jetbrains.kotlin:kotlin-compiler-embeddable:$kotlinVersion")
testImplementation("org.jetbrains.kotlin:kotlin-compiler-runner:$kotlinVersion")
testImplementation("org.jetbrains.kotlin:kotlin-daemon-client:$kotlinVersion")
testImplementation("org.junit.jupiter:junit-jupiter")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -49,7 +49,7 @@ private String getKotlinVersion(Project project) {
private void enableJavaParametersOption(Project project) {
project.getTasks()
.withType(KotlinCompile.class)
.configureEach((compile) -> compile.getKotlinOptions().setJavaParameters(true));
.configureEach((compile) -> compile.getCompilerOptions().getJavaParameters().set(true));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2024 the original author or authors.
* Copyright 2012-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -83,7 +83,7 @@ void taskConfigurationIsAvoided() throws IOException {
configured.add(line.substring("Configuring :".length()));
}
}
assertThat(configured).containsExactlyInAnyOrder("help", "compileJava", "clean");
assertThat(configured).containsExactlyInAnyOrder("help", "clean");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

Expand All @@ -33,9 +34,12 @@
import org.apache.hc.core5.http.HttpRequest;
import org.apache.hc.core5.http2.HttpVersionPolicy;
import org.gradle.testkit.runner.GradleRunner;
import org.gradle.util.GradleVersion;
import org.jetbrains.kotlin.buildtools.api.jvm.ClassSnapshotGranularity;
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.konan.target.HostManager;
import org.jetbrains.kotlin.project.model.LanguageSettings;
import org.jetbrains.kotlin.tooling.core.KotlinToolingVersion;
import org.tomlj.Toml;
Expand All @@ -55,6 +59,8 @@
*/
public class PluginClasspathGradleBuild extends GradleBuild {

private static final GradleVersion GRADLE_VERSION_8_10 = GradleVersion.version("8.10");

public PluginClasspathGradleBuild() {
super();
}
Expand All @@ -69,6 +75,15 @@ public GradleRunner prepareRunner(String... arguments) throws IOException {
}

private List<File> pluginClasspath() {
List<File> pluginClasspath = new ArrayList<>(basePluginClasspath());
if (this.getGradleVersion() == null
|| GradleVersion.version(this.getGradleVersion()).compareTo(GRADLE_VERSION_8_10) > 0) {
pluginClasspath.addAll(classpathAdditionsForGradleVersionsWithEmbeddedKotlin2());
}
return pluginClasspath;
}

private List<File> basePluginClasspath() {
return Arrays.asList(new File("bin/main"), new File("build/classes/java/main"),
new File("build/resources/main"), new File(pathOfJarContaining(LaunchScript.class)),
new File(pathOfJarContaining(ClassVisitor.class)),
Expand All @@ -94,6 +109,12 @@ private List<File> pluginClasspath() {
new File(pathOfJarContaining("org.graalvm.buildtools.utils.SharedConstants")));
}

private List<File> classpathAdditionsForGradleVersionsWithEmbeddedKotlin2() {
return Arrays.asList(new File(pathOfJarContaining(ClassSnapshotGranularity.class)),
new File(pathOfJarContaining("org.jetbrains.kotlin.build.report.metrics.GradleBuildTime")),
new File(pathOfJarContaining(HostManager.class)));
}

private String pathOfJarContaining(String className) {
try {
return pathOfJarContaining(Class.forName(className));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ plugins {

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

import org.jetbrains.kotlin.gradle.dsl.KotlinCompile
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

tasks.withType(KotlinCompile) {
kotlinOptions.javaParameters = false
compilerOptions.javaParameters = false
}

task('kotlinCompileTasksJavaParameters') {
doFirst {
tasks.withType(KotlinCompile) {
println "${name} java parameters: ${kotlinOptions.javaParameters}"
println "${name} java parameters: ${compilerOptions.javaParameters.get()}"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ plugins {

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

import org.jetbrains.kotlin.gradle.dsl.KotlinCompile
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

task('kotlinCompileTasksJavaParameters') {
doFirst {
tasks.withType(KotlinCompile) {
println "${name} java parameters: ${kotlinOptions.javaParameters}"
println "${name} java parameters: ${compilerOptions.javaParameters.get()}"
}
}
}

0 comments on commit d757a21

Please sign in to comment.