2
2
* Apply the JVM Toolchain conventions
3
3
* See https://docs.gradle.org/current/userguide/toolchains.html
4
4
*
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.
13
9
*
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}.
15
13
*
16
14
* Gradle will automatically detect JDK distributions in well-known locations.
17
15
* The following command will list the detected JDKs on the host.
23
21
* {@code
24
22
* $ echo JDK17
25
23
* /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
29
27
* }
30
28
*
31
29
* @author Brian Clozel
32
30
* @author Sam Brannen
33
31
*/
34
32
35
- def mainToolchainConfigured () {
36
- return project. hasProperty(' mainToolchain' ) && project. mainToolchain
37
- }
38
-
39
33
def testToolchainConfigured () {
40
34
return project. hasProperty(' testToolchain' ) && project. testToolchain
41
35
}
42
36
43
- def mainToolchainLanguageVersion () {
44
- if (mainToolchainConfigured()) {
45
- return JavaLanguageVersion . of(project. mainToolchain. toString())
46
- }
47
- return JavaLanguageVersion . of(17 )
48
- }
49
-
50
37
def testToolchainLanguageVersion () {
51
38
if (testToolchainConfigured()) {
52
39
return JavaLanguageVersion . of(project. testToolchain. toString())
53
40
}
54
- return mainToolchainLanguageVersion( )
41
+ return JavaLanguageVersion . of( 17 )
55
42
}
56
43
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 {
72
45
// Configure a specific Java Toolchain for compiling and running tests if the 'testToolchain' property is defined
73
46
if (testToolchainConfigured()) {
74
47
def testLanguageVersion = testToolchainLanguageVersion()
@@ -81,37 +54,13 @@ plugins.withType(JavaPlugin) {
81
54
javaLauncher = javaToolchains. launcherFor {
82
55
languageVersion = testLanguageVersion
83
56
}
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'
108
57
}
109
58
}
110
59
}
111
60
112
61
// Configure the JMH plugin to use the toolchain for generating and running JMH bytecode
113
62
pluginManager. withPlugin(" me.champeau.jmh" ) {
114
- if (mainToolchainConfigured() || testToolchainConfigured()) {
63
+ if (testToolchainConfigured()) {
115
64
tasks. matching { it. name. contains(' jmh' ) && it. hasProperty(' javaLauncher' ) }. configureEach {
116
65
javaLauncher. set(javaToolchains. launcherFor {
117
66
languageVersion. set(testToolchainLanguageVersion())
@@ -131,7 +80,7 @@ rootProject.ext {
131
80
resolvedTestToolchain = false
132
81
}
133
82
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()) {
135
84
def metadata = task. javaCompiler. get(). metadata
136
85
task. project. buildScan. value(' Main toolchain' , " $metadata . vendor $metadata . languageVersion ($metadata . installationPath )" )
137
86
resolvedMainToolchain = true
0 commit comments