Skip to content

Commit 5c574b9

Browse files
committed
Refactor toolchain support in Gradle build
This commit removes the `-PmainToolchain` option from our build, since it was not broadly used. Instead, the language level is now configured in the `JavaConventions` for JDK 17. The `-PtestToolchain` option is still available for testing Spring Framework with other JDKs (i.e., compiling and running tests with a JDK that's not the baseline). See gh-30339
1 parent 9562a1c commit 5c574b9

File tree

2 files changed

+18
-65
lines changed

2 files changed

+18
-65
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@
2424
import org.gradle.api.Project;
2525
import org.gradle.api.plugins.JavaBasePlugin;
2626
import org.gradle.api.plugins.JavaPlugin;
27+
import org.gradle.api.plugins.JavaPluginExtension;
2728
import org.gradle.api.tasks.compile.JavaCompile;
29+
import org.gradle.jvm.toolchain.JavaLanguageVersion;
2830

2931
/**
3032
* {@link Plugin} that applies conventions for compiling Java sources in Spring Framework.
@@ -68,6 +70,8 @@ public void apply(Project project) {
6870
* @param project the current project
6971
*/
7072
private void applyJavaCompileConventions(Project project) {
73+
project.getExtensions().getByType(JavaPluginExtension.class)
74+
.getToolchain().getLanguageVersion().set(JavaLanguageVersion.of(17));
7175
project.getTasks().withType(JavaCompile.class)
7276
.matching(compileTask -> compileTask.getName().equals(JavaPlugin.COMPILE_JAVA_TASK_NAME))
7377
.forEach(compileTask -> {

gradle/toolchains.gradle

Lines changed: 14 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,14 @@
22
* Apply the JVM Toolchain conventions
33
* See https://docs.gradle.org/current/userguide/toolchains.html
44
*
5-
* One can choose the toolchain to use for compiling the MAIN sources and/or compiling
6-
* and running the TEST sources. These options apply to Java, Kotlin and Groovy sources
7-
* when available.
8-
* {@code "./gradlew check -PmainToolchain=17 -PtestToolchain=20"} will use:
9-
* <ul>
10-
* <li>a JDK17 toolchain for compiling the main SourceSet
11-
* <li>a JDK20 toolchain for compiling and running the test SourceSet
12-
* </ul>
5+
* One can choose the toolchain to use for compiling and running the TEST sources.
6+
* These options apply to Java, Kotlin and Groovy test sources when available.
7+
* {@code "./gradlew check -PtestToolchain=22"} will use a JDK22
8+
* toolchain for compiling and running the test SourceSet.
139
*
14-
* By default, the build will fall back to using the current JDK and 17 language level for all sourceSets.
10+
* By default, the main build will fall back to using the a JDK 17
11+
* toolchain (and 17 language level) for all main sourceSets.
12+
* See {@link org.springframework.build.JavaConventions}.
1513
*
1614
* Gradle will automatically detect JDK distributions in well-known locations.
1715
* The following command will list the detected JDKs on the host.
@@ -23,52 +21,27 @@
2321
* {@code
2422
* $ echo JDK17
2523
* /opt/openjdk/java17
26-
* $ echo JDK20
27-
* /opt/openjdk/java20
28-
* $ ./gradlew -Porg.gradle.java.installations.fromEnv=JDK17,JDK20 check
24+
* $ echo JDK22
25+
* /opt/openjdk/java22
26+
* $ ./gradlew -Porg.gradle.java.installations.fromEnv=JDK17,JDK22 check
2927
* }
3028
*
3129
* @author Brian Clozel
3230
* @author Sam Brannen
3331
*/
3432

35-
def mainToolchainConfigured() {
36-
return project.hasProperty('mainToolchain') && project.mainToolchain
37-
}
38-
3933
def testToolchainConfigured() {
4034
return project.hasProperty('testToolchain') && project.testToolchain
4135
}
4236

43-
def mainToolchainLanguageVersion() {
44-
if (mainToolchainConfigured()) {
45-
return JavaLanguageVersion.of(project.mainToolchain.toString())
46-
}
47-
return JavaLanguageVersion.of(17)
48-
}
49-
5037
def testToolchainLanguageVersion() {
5138
if (testToolchainConfigured()) {
5239
return JavaLanguageVersion.of(project.testToolchain.toString())
5340
}
54-
return mainToolchainLanguageVersion()
41+
return JavaLanguageVersion.of(17)
5542
}
5643

57-
plugins.withType(JavaPlugin) {
58-
// Configure the Java Toolchain if the 'mainToolchain' is configured
59-
if (mainToolchainConfigured()) {
60-
java {
61-
toolchain {
62-
languageVersion = mainToolchainLanguageVersion()
63-
}
64-
}
65-
}
66-
else {
67-
// Fallback to JDK17
68-
java {
69-
sourceCompatibility = JavaVersion.VERSION_17
70-
}
71-
}
44+
plugins.withType(JavaPlugin).configureEach {
7245
// Configure a specific Java Toolchain for compiling and running tests if the 'testToolchain' property is defined
7346
if (testToolchainConfigured()) {
7447
def testLanguageVersion = testToolchainLanguageVersion()
@@ -81,37 +54,13 @@ plugins.withType(JavaPlugin) {
8154
javaLauncher = javaToolchains.launcherFor {
8255
languageVersion = testLanguageVersion
8356
}
84-
jvmArgs += ['-Djava.locale.providers=COMPAT']
85-
}
86-
}
87-
}
88-
89-
plugins.withType(GroovyPlugin) {
90-
// Fallback to JDK17
91-
if (!mainToolchainConfigured()) {
92-
compileGroovy {
93-
sourceCompatibility = JavaVersion.VERSION_17
94-
}
95-
}
96-
}
97-
98-
pluginManager.withPlugin("kotlin") {
99-
// Fallback to JDK17
100-
compileKotlin {
101-
kotlinOptions {
102-
jvmTarget = '17'
103-
}
104-
}
105-
compileTestKotlin {
106-
kotlinOptions {
107-
jvmTarget = '17'
10857
}
10958
}
11059
}
11160

11261
// Configure the JMH plugin to use the toolchain for generating and running JMH bytecode
11362
pluginManager.withPlugin("me.champeau.jmh") {
114-
if (mainToolchainConfigured() || testToolchainConfigured()) {
63+
if (testToolchainConfigured()) {
11564
tasks.matching { it.name.contains('jmh') && it.hasProperty('javaLauncher') }.configureEach {
11665
javaLauncher.set(javaToolchains.launcherFor {
11766
languageVersion.set(testToolchainLanguageVersion())
@@ -131,7 +80,7 @@ rootProject.ext {
13180
resolvedTestToolchain = false
13281
}
13382
gradle.taskGraph.afterTask { Task task, TaskState state ->
134-
if (mainToolchainConfigured() && !resolvedMainToolchain && task instanceof JavaCompile && task.javaCompiler.isPresent()) {
83+
if (!resolvedMainToolchain && task instanceof JavaCompile && task.javaCompiler.isPresent()) {
13584
def metadata = task.javaCompiler.get().metadata
13685
task.project.buildScan.value('Main toolchain', "$metadata.vendor $metadata.languageVersion ($metadata.installationPath)")
13786
resolvedMainToolchain = true

0 commit comments

Comments
 (0)