Skip to content

Commit 691c2b8

Browse files
committed
Introduce Toolchain plugin
Nullability support requires us to compile with Java 23 or later. We still need to be able to test against earlier versions down to our Java 17 baseline. This commit introduces a Toolchain plugin that is apply by convention to all Java project. When the toolchainVersion property is configured, the plugin configures test tasks to use that Java version.
1 parent 1d2b7b4 commit 691c2b8

File tree

7 files changed

+85
-2
lines changed

7 files changed

+85
-2
lines changed

gradle/plugins/conventions/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ gradlePlugin {
1717
}
1818

1919
dependencies {
20+
implementation(project(":toolchain"))
2021
implementation("io.spring.javaformat:spring-javaformat-gradle-plugin:$javaFormatVersion")
2122
implementation("io.spring.nohttp:nohttp-gradle:0.0.11")
2223
}

gradle/plugins/conventions/src/main/java/org/springframework/restdocs/build/conventions/JavaBasePluginConventions.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
import org.gradle.api.tasks.compile.JavaCompile;
3535
import org.gradle.api.tasks.testing.Test;
3636

37+
import org.springframework.restdocs.build.toolchain.ToolchainPlugin;
38+
3739
/**
3840
* Conventions for the {@link JavaBasePlugin}.
3941
*
@@ -47,6 +49,7 @@ class JavaBasePluginConventions extends Conventions<JavaBasePlugin> {
4749

4850
@Override
4951
void apply(JavaBasePlugin plugin) {
52+
configureToolchains();
5053
configureCheckstyle();
5154
configureJavaFormat();
5255
configureSourceAndTargetCompatibility();
@@ -55,6 +58,10 @@ void apply(JavaBasePlugin plugin) {
5558
configureDependencyManagement();
5659
}
5760

61+
private void configureToolchains() {
62+
getProject().getPlugins().apply(ToolchainPlugin.class);
63+
}
64+
5865
private void configureDependencyManagement() {
5966
Configuration internal = getProject().getConfigurations().create("internal", (configuration) -> {
6067
configuration.setCanBeConsumed(false);
@@ -91,6 +98,7 @@ private void configureJavaCompileTasks() {
9198
options.setCompilerArgs(List.of("-Werror", "-Xlint:unchecked", "-Xlint:deprecation", "-Xlint:rawtypes",
9299
"-Xlint:varargs", "-Xlint:options"));
93100
options.setEncoding("UTF-8");
101+
options.getRelease().set(17);
94102
});
95103
}
96104

gradle/plugins/conventions/src/main/java/org/springframework/restdocs/build/conventions/MavenPublishPluginConventions.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,9 @@ private void configureContents(MavenPublication maven) {
8383
"https://jakarta.ee/specifications/bean-validation/3.1/apidocs/");
8484
});
8585
});
86-
getProject().getPlugins().withType(JavaPlatformPlugin.class).configureEach((javaPlatformPlugin) ->
87-
maven.from(getProject().getComponents().getByName("javaPlatform")));
86+
getProject().getPlugins()
87+
.withType(JavaPlatformPlugin.class)
88+
.configureEach((javaPlatformPlugin) -> maven.from(getProject().getComponents().getByName("javaPlatform")));
8889
}
8990

9091
private void configureDeploymentRepository(PublishingExtension publishing) {

gradle/plugins/settings.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@ pluginManagement {
1515

1616
include "conventions"
1717
include "optional-dependencies"
18+
include "toolchain"

gradle/plugins/toolchain/build.gradle

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
plugins {
2+
id "java-gradle-plugin"
3+
}
4+
5+
gradlePlugin {
6+
plugins {
7+
optionalDependencies {
8+
id = "org.springframework.restdocs.toolchain"
9+
implementationClass = "org.springframework.restdocs.build.toolchain.ToolchainPlugin"
10+
}
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2014-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.restdocs.build.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+
public class ToolchainPlugin implements Plugin<Project> {
26+
27+
@Override
28+
public void apply(Project project) {
29+
String toolchainVersion = (String) project.findProperty("toolchainVersion");
30+
if (toolchainVersion != null) {
31+
JavaToolchainService toolchainService = project.getExtensions().getByType(JavaToolchainService.class);
32+
project.getTasks()
33+
.withType(Test.class)
34+
.configureEach((test) -> test.getJavaLauncher()
35+
.set(toolchainService.launcherFor(
36+
(spec) -> spec.getLanguageVersion().set(JavaLanguageVersion.of(toolchainVersion)))));
37+
}
38+
}
39+
40+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright 2014-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+
/**
18+
* Toolchain plugin.
19+
*/
20+
package org.springframework.restdocs.build.toolchain;

0 commit comments

Comments
 (0)