Skip to content

Commit 85eb589

Browse files
committed
Fix Gradle Java Toolchain configuration
This commit fixes various issues with the configuration of the Gradle Java toolchain in the build. First, the configuration of build properties is fixed in the CI pipeline because it wasn't properly checked. The JMH plugin is also upgraded and we now configure its toolchain support. This commit also rewrites the XJC tasks in the spring-oxm module, leveraging a Gradle plugin that creates actual compile tasks we can configure. See gh-25787
1 parent b18cf3c commit 85eb589

File tree

10 files changed

+115
-66
lines changed

10 files changed

+115
-66
lines changed

build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ plugins {
99
id "io.freefair.aspectj" version '5.1.1' apply false
1010
id "com.github.ben-manes.versions" version '0.28.0'
1111
id "com.github.johnrengelman.shadow" version "6.1.0" apply false
12-
id "me.champeau.gradle.jmh" version "0.5.2" apply false
12+
id "me.champeau.jmh" version "0.6.4" apply false
1313
id "org.jetbrains.kotlin.plugin.serialization" version "1.4.32" apply false
14+
id "org.unbroken-dome.xjc" version '2.0.0' apply false
1415
}
1516

1617
ext {

ci/pipeline.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,6 @@ jobs:
231231
image: ci-image
232232
file: git-repo/ci/tasks/check-project.yml
233233
params:
234-
MAIN_TOOLCHAIN: 8
235234
TEST_TOOLCHAIN: 11
236235
<<: *build-project-task-params
237236
on_failure:
@@ -258,7 +257,6 @@ jobs:
258257
image: ci-image
259258
file: git-repo/ci/tasks/check-project.yml
260259
params:
261-
MAIN_TOOLCHAIN: 8
262260
TEST_TOOLCHAIN: 15
263261
<<: *build-project-task-params
264262
on_failure:

ci/scripts/check-project.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +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 -Dorg.gradle.java.installations.fromEnv=JDK11,JDK15 \
8-
-PmainToolchain=$MAIN_TOOLCHAIN -PtestToolchain=$TEST_TOOLCHAIN --no-daemon --max-workers=4 check
7+
./gradlew -Dorg.gradle.internal.launcher.welcomeMessageEnabled=false -Porg.gradle.java.installations.fromEnv=JDK11,JDK15 \
8+
-PmainToolchain=${MAIN_TOOLCHAIN} -PtestToolchain=${TEST_TOOLCHAIN} --no-daemon --max-workers=4 check
99
popd > /dev/null

gradle/spring-module.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ apply plugin: 'org.springframework.build.optional-dependencies'
44
// Uncomment the following for Shadow support in the jmhJar block.
55
// Currently commented out due to ZipException: archive is not a ZIP archive
66
// apply plugin: 'com.github.johnrengelman.shadow'
7-
apply plugin: 'me.champeau.gradle.jmh'
7+
apply plugin: 'me.champeau.jmh'
88
apply from: "$rootDir/gradle/publications.gradle"
99

1010
dependencies {
11-
jmh 'org.openjdk.jmh:jmh-core:1.25'
12-
jmh 'org.openjdk.jmh:jmh-generator-annprocess:1.25'
11+
jmh 'org.openjdk.jmh:jmh-core:1.28'
12+
jmh 'org.openjdk.jmh:jmh-generator-annprocess:1.28'
1313
jmh 'net.sf.jopt-simple:jopt-simple:4.6'
1414
}
1515

gradle/toolchains.gradle

Lines changed: 51 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
* <li>a JDK11 toolchain for compiling and running the test SourceSet
1212
* </ul>
1313
*
14+
* By default, the build will fall back to using the current JDK and 1.8 language level for all sourceSets.
15+
*
1416
* Gradle will automatically detect JDK distributions in well-known locations.
1517
* The following command will list the detected JDKs on the host.
1618
* {@code
@@ -29,13 +31,34 @@
2931
* @author Brian Clozel
3032
*/
3133

34+
def mainToolchainConfigured() {
35+
return project.hasProperty('mainToolchain') && project.mainToolchain
36+
}
37+
38+
def testToolchainConfigured() {
39+
return project.hasProperty('testToolchain') && project.testToolchain
40+
}
41+
42+
def mainToolchainLanguageVersion() {
43+
if (mainToolchainConfigured()) {
44+
return JavaLanguageVersion.of(project.mainToolchain.toString())
45+
}
46+
return JavaLanguageVersion.of(8)
47+
}
48+
49+
def testToolchainLanguageVersion() {
50+
if (testToolchainConfigured()) {
51+
return JavaLanguageVersion.of(project.testToolchain.toString())
52+
}
53+
return mainToolchainLanguageVersion()
54+
}
55+
3256
plugins.withType(JavaPlugin) {
33-
// Configure the Java Toolchain if the 'mainToolchain' property is defined
34-
if (project.hasProperty('mainToolchain') && project.mainToolchain) {
35-
def mainLanguageVersion = JavaLanguageVersion.of(project.mainToolchain.toString())
57+
// Configure the Java Toolchain if the 'mainToolchain' is configured
58+
if (mainToolchainConfigured()) {
3659
java {
3760
toolchain {
38-
languageVersion = mainLanguageVersion
61+
languageVersion = mainToolchainLanguageVersion()
3962
}
4063
}
4164
}
@@ -46,8 +69,8 @@ plugins.withType(JavaPlugin) {
4669
}
4770
}
4871
// Configure a specific Java Toolchain for compiling and running tests if the 'testToolchain' property is defined
49-
if (project.hasProperty('testToolchain') && project.testToolchain) {
50-
def testLanguageVersion = JavaLanguageVersion.of(project.testToolchain.toString());
72+
if (testToolchainConfigured()) {
73+
def testLanguageVersion = testToolchainLanguageVersion()
5174
tasks.withType(JavaCompile).matching { it.name.contains("Test") }.configureEach {
5275
javaCompiler = javaToolchains.compilerFor {
5376
languageVersion = testLanguageVersion
@@ -63,17 +86,17 @@ plugins.withType(JavaPlugin) {
6386

6487
plugins.withType(GroovyPlugin) {
6588
// Fallback to JDK8
66-
if (!project.hasProperty('mainToolchain')) {
89+
if (!mainToolchainConfigured()) {
6790
compileGroovy {
6891
sourceCompatibility = JavaVersion.VERSION_1_8
6992
}
7093
}
7194
}
7295

73-
// Configure the Kotlin compiler if the 'mainToolchain' property is defined
7496
pluginManager.withPlugin("kotlin") {
75-
if (project.hasProperty('mainToolchain') && project.mainToolchain) {
76-
def mainLanguageVersion = JavaLanguageVersion.of(project.mainToolchain.toString());
97+
// Configure the Kotlin compiler if the 'mainToolchain' property is defined
98+
if (mainToolchainConfigured()) {
99+
def mainLanguageVersion = mainToolchainLanguageVersion()
77100
def compiler = javaToolchains.compilerFor {
78101
languageVersion = mainLanguageVersion
79102
}
@@ -107,8 +130,8 @@ pluginManager.withPlugin("kotlin") {
107130
}
108131
}
109132

110-
if (project.hasProperty('testToolchain') && project.testToolchain) {
111-
def testLanguageVersion = JavaLanguageVersion.of(project.testToolchain.toString());
133+
if (testToolchainConfigured()) {
134+
def testLanguageVersion = testToolchainLanguageVersion()
112135
def compiler = javaToolchains.compilerFor {
113136
languageVersion = testLanguageVersion
114137
}
@@ -121,4 +144,20 @@ pluginManager.withPlugin("kotlin") {
121144
}
122145
}
123146
}
147+
}
148+
149+
// Configure the JMH plugin to use the toolchain for generating and running JMH bytecode
150+
pluginManager.withPlugin("me.champeau.jmh") {
151+
if (mainToolchainConfigured() || testToolchainConfigured()) {
152+
tasks.matching { it.name.contains('jmh') && it.hasProperty('javaLauncher') }.configureEach {
153+
javaLauncher.set(javaToolchains.launcherFor {
154+
languageVersion.set(testToolchainLanguageVersion())
155+
})
156+
}
157+
tasks.withType(JavaCompile).matching { it.name.contains("Jmh") }.configureEach {
158+
javaCompiler = javaToolchains.compilerFor {
159+
languageVersion = testToolchainLanguageVersion()
160+
}
161+
}
162+
}
124163
}

spring-messaging/src/test/java/org/springframework/messaging/handler/invocation/InvocableHandlerMethodTests.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,10 @@ private StubArgumentResolver getStubResolver(int index) {
166166

167167

168168
@SuppressWarnings("unused")
169-
private static class Handler {
169+
static class Handler {
170+
171+
public Handler() {
172+
}
170173

171174
public String handle(Integer intArg, String stringArg) {
172175
return intArg + "-" + stringArg;
@@ -181,7 +184,7 @@ public void handleWithException(Throwable ex) throws Throwable {
181184
}
182185

183186

184-
private static class ExceptionRaisingArgumentResolver implements HandlerMethodArgumentResolver {
187+
static class ExceptionRaisingArgumentResolver implements HandlerMethodArgumentResolver {
185188

186189
@Override
187190
public boolean supportsParameter(MethodParameter parameter) {

spring-messaging/src/test/java/org/springframework/messaging/handler/invocation/reactive/InvocableHandlerMethodTests.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,8 @@ private static class Handler {
183183

184184
private AtomicReference<String> result = new AtomicReference<>();
185185

186+
public Handler() {
187+
}
186188

187189
public String getResult() {
188190
return this.result.get();

spring-oxm/spring-oxm.gradle

Lines changed: 12 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,24 @@
1+
plugins {
2+
id "org.unbroken-dome.xjc"
3+
}
4+
15
description = "Spring Object/XML Marshalling"
26

37
configurations {
48
jibx
5-
xjc
69
}
710

811
dependencies {
912
jibx "org.jibx:jibx-bind:1.3.3"
1013
jibx "org.apache.bcel:bcel:6.0"
11-
xjc "javax.xml.bind:jaxb-api:2.3.1"
12-
xjc "com.sun.xml.bind:jaxb-core:2.3.0.1"
13-
xjc "com.sun.xml.bind:jaxb-impl:2.3.0.1"
14-
xjc "com.sun.xml.bind:jaxb-xjc:2.3.1"
15-
xjc "com.sun.activation:javax.activation:1.2.0"
1614
}
1715

18-
ext.genSourcesDir = "${buildDir}/generated-sources"
19-
ext.flightSchema = "${projectDir}/src/test/resources/org/springframework/oxm/flight.xsd"
20-
21-
task genJaxb {
22-
ext.sourcesDir = "${genSourcesDir}/jaxb"
23-
ext.classesDir = "${buildDir}/classes/jaxb"
24-
25-
inputs.files(flightSchema).withPathSensitivity(PathSensitivity.RELATIVE)
26-
outputs.dir classesDir
27-
28-
doLast() {
29-
project.ant {
30-
taskdef name: "xjc", classname: "com.sun.tools.xjc.XJCTask",
31-
classpath: configurations.xjc.asPath
32-
mkdir(dir: sourcesDir)
33-
mkdir(dir: classesDir)
34-
35-
xjc(destdir: sourcesDir, schema: flightSchema,
36-
package: "org.springframework.oxm.jaxb.test") {
37-
produces(dir: sourcesDir, includes: "**/*.java")
38-
}
39-
40-
javac(destdir: classesDir, source: 1.8, target: 1.8, debug: true,
41-
debugLevel: "lines,vars,source",
42-
classpath: configurations.xjc.asPath) {
43-
src(path: sourcesDir)
44-
include(name: "**/*.java")
45-
include(name: "*.java")
46-
}
47-
48-
copy(todir: classesDir) {
49-
fileset(dir: sourcesDir, erroronmissingdir: false) {
50-
exclude(name: "**/*.java")
51-
}
52-
}
53-
}
16+
xjc {
17+
xjcVersion = '2.2'
18+
}
19+
sourceSets {
20+
test {
21+
xjcTargetPackage = 'org.springframework.oxm.jaxb.test'
5422
}
5523
}
5624

@@ -67,7 +35,7 @@ dependencies {
6735
testCompile("org.codehaus.jettison:jettison") {
6836
exclude group: "stax", module: "stax-api"
6937
}
70-
testCompile(files(genJaxb.classesDir).builtBy(genJaxb))
38+
//testCompile(files(genJaxb.classesDir).builtBy(genJaxb))
7139
testCompile("org.xmlunit:xmlunit-assertj")
7240
testCompile("org.xmlunit:xmlunit-matchers")
7341
testRuntime("com.sun.xml.bind:jaxb-core")
@@ -76,7 +44,7 @@ dependencies {
7644

7745
// JiBX compiler is currently not compatible with JDK 9+.
7846
// If customJavaHome has been set, we assume the custom JDK version is 9+.
79-
if ((JavaVersion.current() == JavaVersion.VERSION_1_8) && !System.getProperty("customJavaSourceVersion")) {
47+
if ((JavaVersion.current() == JavaVersion.VERSION_1_8) && !project.hasProperty("testToolchain")) {
8048
compileTestJava {
8149
def bindingXml = "${projectDir}/src/test/resources/org/springframework/oxm/jibx/binding.xml"
8250

spring-oxm/src/test/schema/flight.xsd

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Copyright 2002-2021 the original author or authors.
4+
~
5+
~ Licensed under the Apache License, Version 2.0 (the "License");
6+
~ you may not use this file except in compliance with the License.
7+
~ You may obtain a copy of the License at
8+
~
9+
~ https://www.apache.org/licenses/LICENSE-2.0
10+
~
11+
~ Unless required by applicable law or agreed to in writing, software
12+
~ distributed under the License is distributed on an "AS IS" BASIS,
13+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
~ See the License for the specific language governing permissions and
15+
~ limitations under the License.
16+
-->
17+
18+
<schema xmlns="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"
19+
targetNamespace="http://samples.springframework.org/flight"
20+
xmlns:tns="http://samples.springframework.org/flight">
21+
<element name="flights">
22+
<complexType>
23+
<sequence>
24+
<element name="flight" type="tns:flightType"
25+
maxOccurs="unbounded">
26+
</element>
27+
</sequence>
28+
</complexType>
29+
</element>
30+
<element name="flight" type="tns:flightType"/>
31+
<complexType name="flightType">
32+
<sequence>
33+
<element name="number" type="long"/>
34+
</sequence>
35+
</complexType>
36+
</schema>

src/checkstyle/checkstyle-suppressions.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
<suppress files="[\\/]src[\\/](test|testFixtures)[\\/]java[\\/]" checks="AnnotationLocation|AnnotationUseStyle|AtclauseOrder|AvoidNestedBlocks|FinalClass|HideUtilityClassConstructor|InnerTypeLast|JavadocStyle|JavadocType|JavadocVariable|LeftCurly|MultipleVariableDeclarations|NeedBraces|OneTopLevelClass|OuterTypeFilename|RequireThis|SpringCatch|SpringJavadoc|SpringNoThis" />
77
<suppress files="[\\/]src[\\/](test|testFixtures)[\\/]java[\\/]org[\\/]springframework[\\/].+(Tests|Suite)" checks="IllegalImport" id="bannedJUnitJupiterImports" />
88
<suppress files="[\\/]src[\\/](test|testFixtures)[\\/]java[\\/]" checks="SpringJUnit5" message="should not be public" />
9+
<!-- generated sources -->
10+
<suppress files="[\\/]build[\\/]generated[\\/]sources[\\/]" checks=".*" />
911

1012
<!-- JMH benchmarks -->
1113
<suppress files="[\\/]src[\\/]jmh[\\/]java[\\/]org[\\/]springframework[\\/]" checks="JavadocVariable|JavadocStyle|InnerTypeLast" />

0 commit comments

Comments
 (0)