Skip to content

Commit b85a76f

Browse files
committed
Configure the Gradle toolchain to use Java 23
In order to be able to fix spring-projectsgh-34140 which requires using at least a Java 22 compiler, this commit intends to change the configuration of the Gradle toolchain to use Java 23, while setting the Java release to Java 17 (or other versions when using MRJARs) when relevant in order to keep the current Java 17 baseline. See spring-projectsgh-34220
1 parent 63770ba commit b85a76f

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

buildSrc/src/main/java/org/springframework/build/JavaConventions.java

+17-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-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.
@@ -73,7 +73,7 @@ public void apply(Project project) {
7373
private void applyJavaCompileConventions(Project project) {
7474
project.getExtensions().getByType(JavaPluginExtension.class).toolchain(toolchain -> {
7575
toolchain.getVendor().set(JvmVendorSpec.BELLSOFT);
76-
toolchain.getLanguageVersion().set(JavaLanguageVersion.of(17));
76+
toolchain.getLanguageVersion().set(JavaLanguageVersion.of(23));
7777
});
7878
SpringFrameworkExtension frameworkExtension = project.getExtensions().getByType(SpringFrameworkExtension.class);
7979
project.afterEvaluate(p -> {
@@ -83,6 +83,7 @@ private void applyJavaCompileConventions(Project project) {
8383
compileTask.getOptions().setCompilerArgs(COMPILER_ARGS);
8484
compileTask.getOptions().getCompilerArgumentProviders().add(frameworkExtension.asArgumentProvider());
8585
compileTask.getOptions().setEncoding("UTF-8");
86+
setJavaRelease(compileTask);
8687
});
8788
p.getTasks().withType(JavaCompile.class)
8889
.matching(compileTask -> compileTask.getName().startsWith(JavaPlugin.COMPILE_TEST_JAVA_TASK_NAME)
@@ -91,9 +92,23 @@ private void applyJavaCompileConventions(Project project) {
9192
compileTask.getOptions().setCompilerArgs(TEST_COMPILER_ARGS);
9293
compileTask.getOptions().getCompilerArgumentProviders().add(frameworkExtension.asArgumentProvider());
9394
compileTask.getOptions().setEncoding("UTF-8");
95+
setJavaRelease(compileTask);
9496
});
9597

9698
});
9799
}
98100

101+
private void setJavaRelease(JavaCompile task) {
102+
int defaultVersion = 17;
103+
int releaseVersion = defaultVersion;
104+
int compilerVersion = task.getJavaCompiler().get().getMetadata().getLanguageVersion().asInt();
105+
for (int version = defaultVersion ; version <= compilerVersion ; version++) {
106+
if (task.getName().contains("Java" + version)) {
107+
releaseVersion = version;
108+
break;
109+
}
110+
}
111+
task.getOptions().getRelease().set(releaseVersion);
112+
}
113+
99114
}

0 commit comments

Comments
 (0)