Skip to content

Commit 352d204

Browse files
committed
Configure toolchain to build using the current JVM
Previously, CI incorrectly used the target toolchain version to build the project. This has an impact on the NullAway checks that require Java22+. This commit configures the toolchain the same way Spring Boot does: only the tests are executed against the build produced by the current VM. Closes gh-1741
1 parent a8fd1c1 commit 352d204

File tree

3 files changed

+140
-10
lines changed

3 files changed

+140
-10
lines changed

gradle/plugins/conventions-plugin/src/main/java/org/springframework/ws/gradle/conventions/JavaPluginConventions.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
import org.gradle.api.tasks.testing.Test;
3636
import org.gradle.jvm.toolchain.JavaLanguageVersion;
3737

38+
import org.springframework.ws.gradle.conventions.toolchain.ToolchainPlugin;
39+
3840
/**
3941
* Conventions for the {@link JavaPlugin}.
4042
*
@@ -49,11 +51,11 @@ void apply(Project project) {
4951
configureJavaConventions(project);
5052
JavaPluginExtension java = project.getExtensions().getByType(JavaPluginExtension.class);
5153
enableSourceAndJavadocJars(java);
52-
configureSourceAndTargetCompatibility(java);
54+
configureSourceAndTargetCompatibility(project);
5355
configureDependencyManagement(project);
5456
configureVersionUpgradePolicy(project);
5557
configureJarManifest(project);
56-
configureToolchain(project, java);
58+
configureToolchain(project);
5759
configureJavadocClasspath(project, java);
5860
configureJUnitPlatform(project);
5961
}
@@ -78,9 +80,12 @@ private void enableSourceAndJavadocJars(JavaPluginExtension java) {
7880
java.withJavadocJar();
7981
}
8082

81-
private void configureSourceAndTargetCompatibility(JavaPluginExtension java) {
82-
java.setSourceCompatibility(JAVA_BASELINE);
83-
java.setTargetCompatibility(JAVA_BASELINE);
83+
private void configureSourceAndTargetCompatibility(Project project) {
84+
if (!project.hasProperty("toolchainVersion")) {
85+
JavaPluginExtension javaPluginExtension = project.getExtensions().getByType(JavaPluginExtension.class);
86+
javaPluginExtension.setSourceCompatibility(JAVA_BASELINE);
87+
javaPluginExtension.setTargetCompatibility(JAVA_BASELINE);
88+
}
8489
}
8590

8691
private void configureDependencyManagement(Project project) {
@@ -113,11 +118,8 @@ private void configureJarManifest(Project project) {
113118
}));
114119
}
115120

116-
private void configureToolchain(Project project, JavaPluginExtension java) {
117-
Object toolchainVersion = project.findProperty("toolchainVersion");
118-
if (toolchainVersion != null) {
119-
java.getToolchain().getLanguageVersion().set(JavaLanguageVersion.of(toolchainVersion.toString()));
120-
}
121+
private void configureToolchain(Project project) {
122+
project.getPlugins().apply(ToolchainPlugin.class);
121123
}
122124

123125
private void configureJavadocClasspath(Project project, JavaPluginExtension java) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright 2012-present 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.ws.gradle.conventions.toolchain;
18+
19+
import org.gradle.api.Project;
20+
import org.gradle.api.provider.ListProperty;
21+
import org.gradle.api.provider.Property;
22+
import org.gradle.jvm.toolchain.JavaLanguageVersion;
23+
24+
/**
25+
* DSL extension for {@link ToolchainPlugin}.
26+
*
27+
* @author Christoph Dreis
28+
*/
29+
public class ToolchainExtension {
30+
31+
private final Property<JavaLanguageVersion> maximumCompatibleJavaVersion;
32+
33+
private final ListProperty<String> testJvmArgs;
34+
35+
private final JavaLanguageVersion javaVersion;
36+
37+
public ToolchainExtension(Project project) {
38+
this.maximumCompatibleJavaVersion = project.getObjects().property(JavaLanguageVersion.class);
39+
this.testJvmArgs = project.getObjects().listProperty(String.class);
40+
String toolchainVersion = (String) project.findProperty("toolchainVersion");
41+
this.javaVersion = (toolchainVersion != null) ? JavaLanguageVersion.of(toolchainVersion) : null;
42+
}
43+
44+
public Property<JavaLanguageVersion> getMaximumCompatibleJavaVersion() {
45+
return this.maximumCompatibleJavaVersion;
46+
}
47+
48+
public ListProperty<String> getTestJvmArgs() {
49+
return this.testJvmArgs;
50+
}
51+
52+
JavaLanguageVersion getJavaVersion() {
53+
return this.javaVersion;
54+
}
55+
56+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright 2012-present 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.ws.gradle.conventions.toolchain;
18+
19+
import org.gradle.api.Plugin;
20+
import org.gradle.api.Project;
21+
import org.gradle.api.tasks.testing.Test;
22+
import org.gradle.jvm.toolchain.JavaLanguageVersion;
23+
import org.gradle.jvm.toolchain.JavaToolchainService;
24+
25+
/**
26+
* {@link Plugin} for customizing Gradle's toolchain support.
27+
*
28+
* @author Christoph Dreis
29+
* @author Andy Wilkinson
30+
*/
31+
public class ToolchainPlugin implements Plugin<Project> {
32+
33+
@Override
34+
public void apply(Project project) {
35+
configureToolchain(project);
36+
}
37+
38+
private void configureToolchain(Project project) {
39+
ToolchainExtension toolchain = project.getExtensions().create("toolchain", ToolchainExtension.class, project);
40+
JavaLanguageVersion toolchainVersion = toolchain.getJavaVersion();
41+
if (toolchainVersion != null) {
42+
project.afterEvaluate((evaluated) -> configure(evaluated, toolchain));
43+
}
44+
}
45+
46+
private void configure(Project project, ToolchainExtension toolchain) {
47+
if (!isJavaVersionSupported(toolchain, toolchain.getJavaVersion())) {
48+
disableToolchainTasks(project);
49+
}
50+
else {
51+
configureTestToolchain(project, toolchain.getJavaVersion());
52+
}
53+
}
54+
55+
private boolean isJavaVersionSupported(ToolchainExtension toolchain, JavaLanguageVersion toolchainVersion) {
56+
return toolchain.getMaximumCompatibleJavaVersion()
57+
.map((version) -> version.canCompileOrRun(toolchainVersion))
58+
.getOrElse(true);
59+
}
60+
61+
private void disableToolchainTasks(Project project) {
62+
project.getTasks().withType(Test.class, (task) -> task.setEnabled(false));
63+
}
64+
65+
private void configureTestToolchain(Project project, JavaLanguageVersion toolchainVersion) {
66+
JavaToolchainService javaToolchains = project.getExtensions().getByType(JavaToolchainService.class);
67+
project.getTasks()
68+
.withType(Test.class, (test) -> test.getJavaLauncher()
69+
.set(javaToolchains.launcherFor((spec) -> spec.getLanguageVersion().set(toolchainVersion))));
70+
}
71+
72+
}

0 commit comments

Comments
 (0)