Skip to content

Commit a8d5532

Browse files
committed
Introduce Gradle Toolchain support in build
Prior to this commit, the Spring Framework build would rely on setting a custom Java HOME for building all sources and tests with that JDK. This approach is not flexible enough, since we would be testing the source compatibility against a recent JDK, but not a common case experienced by the community: compiling and running application code with a recent JDK and the official, JDK8-based Framework artifacts. This method is also limiting our choice of JDKs to the ones currently supported by Gradle itself. This commit introduces the support of Gradle JVM Toolchains in the Spring Framework build. We can now select a specific JDK for compiling the main SourceSets (Java, Groovy and Kotlin) and another one for compiling and running the test SourceSets: `./gradlew check -PmainToolChain=8 -PtestToolchain=15` Gradle will automatically find the JDKs present on the host or download one automcatically. You can find out about the ones installed on your host using: `./gradlew -q javaToolchains` Finally, this commit also refactors the CI infrastructure to: * only have a single CI image (with all the supported JDKs) * use this new feature to compile with JDK8 but test it against JDK11 and JDK15. Closes gh-25787
1 parent 7f422f2 commit a8d5532

File tree

15 files changed

+168
-166
lines changed

15 files changed

+168
-166
lines changed

build.gradle

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -310,14 +310,13 @@ configure([rootProject] + javaProjects) { project ->
310310
apply plugin: "java-test-fixtures"
311311
apply plugin: "checkstyle"
312312
apply plugin: 'org.springframework.build.compile'
313-
apply from: "${rootDir}/gradle/custom-java-home.gradle"
313+
apply from: "${rootDir}/gradle/toolchains.gradle"
314314
apply from: "${rootDir}/gradle/ide.gradle"
315315

316316
pluginManager.withPlugin("kotlin") {
317317
apply plugin: "org.jetbrains.dokka"
318318
compileKotlin {
319319
kotlinOptions {
320-
jvmTarget = "1.8"
321320
languageVersion = "1.3"
322321
apiVersion = "1.3"
323322
freeCompilerArgs = ["-Xjsr305=strict"]
@@ -326,7 +325,6 @@ configure([rootProject] + javaProjects) { project ->
326325
}
327326
compileTestKotlin {
328327
kotlinOptions {
329-
jvmTarget = "1.8"
330328
freeCompilerArgs = ["-Xjsr305=strict"]
331329
}
332330
}

buildSrc/README.md

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,18 @@ They are declared in the `build.gradle` file in this folder.
88
### Compiler conventions
99

1010
The `org.springframework.build.compile` plugin applies the Java compiler conventions to the build.
11-
By default, the build compiles sources with Java `1.8` source and target compatibility.
12-
You can test a different source compatibility version on the CLI with a project property like:
13-
14-
```
15-
./gradlew test -PjavaSourceVersion=11
16-
```
1711

1812
## Build Plugins
1913

20-
## Optional dependencies
14+
### Optional dependencies
2115

2216
The `org.springframework.build.optional-dependencies` plugin creates a new `optional`
2317
Gradle configuration - it adds the dependencies to the project's compile and runtime classpath
2418
but doesn't affect the classpath of dependent projects.
2519
This plugin does not provide a `provided` configuration, as the native `compileOnly` and `testCompileOnly`
2620
configurations are preferred.
2721

28-
## API Diff
22+
### API Diff
2923

3024
This plugin uses the [Gradle JApiCmp](https://github.com/melix/japicmp-gradle-plugin) plugin
3125
to generate API Diff reports for each Spring Framework module. This plugin is applied once on the root

buildSrc/src/main/java/org/springframework/build/compile/CompilerConventionsPlugin.java

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2021 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.
@@ -20,31 +20,21 @@
2020
import java.util.Arrays;
2121
import java.util.List;
2222

23-
import org.gradle.api.JavaVersion;
2423
import org.gradle.api.Plugin;
2524
import org.gradle.api.Project;
25+
import org.gradle.api.plugins.JavaLibraryPlugin;
2626
import org.gradle.api.plugins.JavaPlugin;
2727
import org.gradle.api.plugins.JavaPluginConvention;
2828
import org.gradle.api.tasks.compile.JavaCompile;
2929

3030
/**
3131
* {@link Plugin} that applies conventions for compiling Java sources in Spring Framework.
32-
* <p>One can override the default Java source compatibility version
33-
* with a dedicated property on the CLI: {@code "./gradlew test -PjavaSourceVersion=11"}.
3432
*
3533
* @author Brian Clozel
3634
* @author Sam Brannen
3735
*/
3836
public class CompilerConventionsPlugin implements Plugin<Project> {
3937

40-
/**
41-
* The project property that can be used to switch the Java source
42-
* compatibility version for building source and test classes.
43-
*/
44-
public static final String JAVA_SOURCE_VERSION_PROPERTY = "javaSourceVersion";
45-
46-
public static final JavaVersion DEFAULT_COMPILER_VERSION = JavaVersion.VERSION_1_8;
47-
4838
private static final List<String> COMPILER_ARGS;
4939

5040
private static final List<String> TEST_COMPILER_ARGS;
@@ -69,7 +59,7 @@ public class CompilerConventionsPlugin implements Plugin<Project> {
6959

7060
@Override
7161
public void apply(Project project) {
72-
project.getPlugins().withType(JavaPlugin.class, javaPlugin -> applyJavaCompileConventions(project));
62+
project.getPlugins().withType(JavaLibraryPlugin.class, javaPlugin -> applyJavaCompileConventions(project));
7363
}
7464

7565
/**
@@ -79,15 +69,6 @@ public void apply(Project project) {
7969
*/
8070
private void applyJavaCompileConventions(Project project) {
8171
JavaPluginConvention java = project.getConvention().getPlugin(JavaPluginConvention.class);
82-
if (project.hasProperty(JAVA_SOURCE_VERSION_PROPERTY)) {
83-
JavaVersion javaSourceVersion = JavaVersion.toVersion(project.property(JAVA_SOURCE_VERSION_PROPERTY));
84-
java.setSourceCompatibility(javaSourceVersion);
85-
}
86-
else {
87-
java.setSourceCompatibility(DEFAULT_COMPILER_VERSION);
88-
}
89-
java.setTargetCompatibility(DEFAULT_COMPILER_VERSION);
90-
9172
project.getTasks().withType(JavaCompile.class)
9273
.matching(compileTask -> compileTask.getName().equals(JavaPlugin.COMPILE_JAVA_TASK_NAME))
9374
.forEach(compileTask -> {

ci/images/ci-image-jdk11/Dockerfile

Lines changed: 0 additions & 8 deletions
This file was deleted.

ci/images/ci-image-jdk15/Dockerfile

Lines changed: 0 additions & 8 deletions
This file was deleted.

ci/images/ci-image/Dockerfile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,8 @@ ADD setup.sh /setup.sh
44
ADD get-jdk-url.sh /get-jdk-url.sh
55
RUN ./setup.sh java8
66

7-
ENV JAVA_HOME /opt/openjdk
7+
ENV JAVA_HOME /opt/openjdk/java8
8+
ENV JDK11 /opt/openjdk/java11
9+
ENV JDK15 /opt/openjdk/java15
10+
811
ENV PATH $JAVA_HOME/bin:$PATH

ci/images/setup.sh

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,20 @@ curl --output /opt/concourse-release-scripts.jar https://repo.spring.io/release/
1919
###########################################################
2020
# JAVA
2121
###########################################################
22-
JDK_URL=$( ./get-jdk-url.sh $1 )
2322

2423
mkdir -p /opt/openjdk
25-
cd /opt/openjdk
26-
curl -L ${JDK_URL} | tar zx --strip-components=1
27-
test -f /opt/openjdk/bin/java
28-
test -f /opt/openjdk/bin/javac
24+
pushd /opt/openjdk > /dev/null
25+
for jdk in java8 java11 java15
26+
do
27+
JDK_URL=$( /get-jdk-url.sh $jdk )
28+
mkdir $jdk
29+
pushd $jdk > /dev/null
30+
curl -L ${JDK_URL} | tar zx --strip-components=1
31+
test -f bin/java
32+
test -f bin/javac
33+
popd > /dev/null
34+
done
35+
popd
2936

3037
###########################################################
3138
# GRADLE ENTERPRISE

ci/pipeline.yml

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -85,18 +85,6 @@ resources:
8585
source:
8686
<<: *docker-resource-source
8787
repository: ((docker-hub-organization))/spring-framework-ci
88-
- name: ci-image-jdk11
89-
type: docker-image
90-
icon: docker
91-
source:
92-
<<: *docker-resource-source
93-
repository: ((docker-hub-organization))/spring-framework-ci-jdk11
94-
- name: ci-image-jdk15
95-
type: docker-image
96-
icon: docker
97-
source:
98-
<<: *docker-resource-source
99-
repository: ((docker-hub-organization))/spring-framework-ci-jdk15
10088
- name: artifactory-repo
10189
type: artifactory-resource
10290
icon: package-variant
@@ -161,14 +149,6 @@ jobs:
161149
params:
162150
build: ci-images-git-repo/ci/images
163151
dockerfile: ci-images-git-repo/ci/images/ci-image/Dockerfile
164-
- put: ci-image-jdk11
165-
params:
166-
build: ci-images-git-repo/ci/images
167-
dockerfile: ci-images-git-repo/ci/images/ci-image-jdk11/Dockerfile
168-
- put: ci-image-jdk15
169-
params:
170-
build: ci-images-git-repo/ci/images
171-
dockerfile: ci-images-git-repo/ci/images/ci-image-jdk15/Dockerfile
172152
- name: build
173153
serial: true
174154
public: true
@@ -227,16 +207,19 @@ jobs:
227207
serial: true
228208
public: true
229209
plan:
230-
- get: ci-image-jdk11
210+
- get: ci-image
231211
- get: git-repo
232212
- get: every-morning
233213
trigger: true
234214
- put: repo-status-jdk11-build
235215
params: { state: "pending", commit: "git-repo" }
236216
- do:
237217
- task: check-project
238-
image: ci-image-jdk11
218+
image: ci-image
239219
file: git-repo/ci/tasks/check-project.yml
220+
params:
221+
MAIN_TOOLCHAIN: 8
222+
TEST_TOOLCHAIN: 11
240223
<<: *build-project-task-params
241224
on_failure:
242225
do:
@@ -251,16 +234,19 @@ jobs:
251234
serial: true
252235
public: true
253236
plan:
254-
- get: ci-image-jdk15
237+
- get: ci-image
255238
- get: git-repo
256239
- get: every-morning
257240
trigger: true
258241
- put: repo-status-jdk15-build
259242
params: { state: "pending", commit: "git-repo" }
260243
- do:
261244
- task: check-project
262-
image: ci-image-jdk15
245+
image: ci-image
263246
file: git-repo/ci/tasks/check-project.yml
247+
params:
248+
MAIN_TOOLCHAIN: 8
249+
TEST_TOOLCHAIN: 15
264250
<<: *build-project-task-params
265251
on_failure:
266252
do:

ci/scripts/check-project.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ set -e
44
source $(dirname $0)/common.sh
55

66
pushd git-repo > /dev/null
7-
./gradlew -Dorg.gradle.internal.launcher.welcomeMessageEnabled=false --no-daemon --max-workers=4 check
7+
./gradlew -Dorg.gradle.internal.launcher.welcomeMessageEnabled=false -Dorg.gradle.java.installations.fromEnv=JDK11,JDK15 \
8+
-PmainToolchain=$MAIN_TOOLCHAIN -PtestToolchain=$TEST_TOOLCHAIN --no-daemon --max-workers=4 check
89
popd > /dev/null

ci/tasks/check-project.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ caches:
1010
params:
1111
BRANCH:
1212
CI: true
13+
MAIN_TOOLCHAIN:
14+
TEST_TOOLCHAIN:
1315
GRADLE_ENTERPRISE_ACCESS_KEY:
1416
GRADLE_ENTERPRISE_CACHE_USERNAME:
1517
GRADLE_ENTERPRISE_CACHE_PASSWORD:

gradle/custom-java-home.gradle

Lines changed: 0 additions & 80 deletions
This file was deleted.

gradle/spring-module.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
apply plugin: 'java-library'
12
apply plugin: 'org.springframework.build.compile'
23
apply plugin: 'org.springframework.build.optional-dependencies'
34
// Uncomment the following for Shadow support in the jmhJar block.

0 commit comments

Comments
 (0)