Browse Source
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-25787pull/26684/head
Brian Clozel
4 years ago
15 changed files with 168 additions and 166 deletions
@ -1,8 +0,0 @@
@@ -1,8 +0,0 @@
|
||||
FROM ubuntu:focal-20210119 |
||||
|
||||
ADD setup.sh /setup.sh |
||||
ADD get-jdk-url.sh /get-jdk-url.sh |
||||
RUN ./setup.sh java11 |
||||
|
||||
ENV JAVA_HOME /opt/openjdk |
||||
ENV PATH $JAVA_HOME/bin:$PATH |
@ -1,8 +0,0 @@
@@ -1,8 +0,0 @@
|
||||
FROM ubuntu:focal-20210119 |
||||
|
||||
ADD setup.sh /setup.sh |
||||
ADD get-jdk-url.sh /get-jdk-url.sh |
||||
RUN ./setup.sh java15 |
||||
|
||||
ENV JAVA_HOME /opt/openjdk |
||||
ENV PATH $JAVA_HOME/bin:$PATH |
@ -1,80 +0,0 @@
@@ -1,80 +0,0 @@
|
||||
// ----------------------------------------------------------------------------- |
||||
// |
||||
// This script adds support for the following two JVM system properties |
||||
// that control the build for alternative JDKs (i.e., a JDK other than |
||||
// the one used to launch the Gradle process). |
||||
// |
||||
// - customJavaHome: absolute path to the alternate JDK installation to |
||||
// use to compile Java code and execute tests. This system property |
||||
// is also used in spring-oxm.gradle to determine whether JiBX is |
||||
// supported. |
||||
// |
||||
// - customJavaSourceVersion: Java version supplied to the `--release` |
||||
// command line flag to control the Java source and target |
||||
// compatibility version. Supported versions include 9 or higher. |
||||
// Do not set this system property if Java 8 should be used. |
||||
// |
||||
// Examples: |
||||
// |
||||
// ./gradlew -DcustomJavaHome=/Library/Java/JavaVirtualMachines/jdk-14.jdk/Contents/Home test |
||||
// |
||||
// ./gradlew --no-build-cache -DcustomJavaHome=/Library/Java/JavaVirtualMachines/jdk-14.jdk/Contents/Home test |
||||
// |
||||
// ./gradlew -DcustomJavaHome=/Library/Java/JavaVirtualMachines/jdk-14.jdk/Contents/Home -DcustomJavaSourceVersion=14 test |
||||
// |
||||
// |
||||
// Credits: inspired by work from Marc Philipp and Stephane Nicoll |
||||
// |
||||
// ----------------------------------------------------------------------------- |
||||
|
||||
import org.gradle.internal.os.OperatingSystem |
||||
// import org.jetbrains.kotlin.gradle.dsl.KotlinJvmCompile |
||||
|
||||
def customJavaHome = System.getProperty("customJavaHome") |
||||
|
||||
if (customJavaHome) { |
||||
def customJavaHomeDir = new File(customJavaHome) |
||||
def customJavaSourceVersion = System.getProperty("customJavaSourceVersion") |
||||
|
||||
tasks.withType(JavaCompile) { |
||||
logger.info("Java home for " + it.name + " task in " + project.name + ": " + customJavaHomeDir) |
||||
options.forkOptions.javaHome = customJavaHomeDir |
||||
inputs.property("customJavaHome", customJavaHome) |
||||
if (customJavaSourceVersion) { |
||||
options.compilerArgs += [ "--release", customJavaSourceVersion] |
||||
inputs.property("customJavaSourceVersion", customJavaSourceVersion) |
||||
} |
||||
} |
||||
|
||||
tasks.withType(GroovyCompile) { |
||||
logger.info("Java home for " + it.name + " task in " + project.name + ": " + customJavaHomeDir) |
||||
options.forkOptions.javaHome = customJavaHomeDir |
||||
inputs.property("customJavaHome", customJavaHome) |
||||
if (customJavaSourceVersion) { |
||||
options.compilerArgs += [ "--release", customJavaSourceVersion] |
||||
inputs.property("customJavaSourceVersion", customJavaSourceVersion) |
||||
} |
||||
} |
||||
|
||||
/* |
||||
tasks.withType(KotlinJvmCompile) { |
||||
logger.info("Java home for " + it.name + " task in " + project.name + ": " + customJavaHome) |
||||
kotlinOptions.jdkHome = customJavaHomeDir |
||||
inputs.property("customJavaHome", customJavaHome) |
||||
} |
||||
*/ |
||||
|
||||
tasks.withType(Test) { |
||||
def javaExecutable = customJavaHome + "/bin/java" |
||||
if (OperatingSystem.current().isWindows()) { |
||||
javaExecutable += ".exe" |
||||
} |
||||
logger.info("Java executable for " + it.name + " task in " + project.name + ": " + javaExecutable) |
||||
executable = javaExecutable |
||||
inputs.property("customJavaHome", customJavaHome) |
||||
if (customJavaSourceVersion) { |
||||
inputs.property("customJavaSourceVersion", customJavaSourceVersion) |
||||
} |
||||
} |
||||
|
||||
} |
@ -0,0 +1,127 @@
@@ -0,0 +1,127 @@
|
||||
/** |
||||
* Apply the JVM Toolchain conventions |
||||
* See https://docs.gradle.org/current/userguide/toolchains.html |
||||
* |
||||
* One can choose the toolchain to use for compiling the MAIN sources and/or compiling |
||||
* and running the TEST sources. These options apply to Java, Kotlin and Groovy sources |
||||
* when available. |
||||
* {@code "./gradlew check -PmainToolchain=8 -PtestToolchain=11"} will use: |
||||
* <ul> |
||||
* <li>a JDK8 toolchain for compiling the main SourceSet |
||||
* <li>a JDK11 toolchain for compiling and running the test SourceSet |
||||
* </ul> |
||||
* |
||||
* Gradle will automatically detect JDK distributions in well-known locations. |
||||
* The following command will list the detected JDKs on the host. |
||||
* {@code |
||||
* $ ./gradlew -q javaToolchains |
||||
* } |
||||
* |
||||
* We can also configure ENV variables and let Gradle know about them: |
||||
* {@code |
||||
* $ echo JDK11 |
||||
* /opt/openjdk/java11 |
||||
* $ echo JDK15 |
||||
* /opt/openjdk/java15 |
||||
* $ ./gradlew -Dorg.gradle.java.installations.fromEnv=JDK11,JDK15 check |
||||
* } |
||||
* |
||||
* @author Brian Clozel |
||||
*/ |
||||
def mainToolchain = 'mainToolchain' |
||||
def testToolchain = 'testToolchain' |
||||
|
||||
plugins.withType(JavaPlugin) { |
||||
// Configure the Java Toolchain if the 'mainToolchain' property is defined |
||||
if (project.hasProperty(mainToolchain)) { |
||||
def mainLanguageVersion = JavaLanguageVersion.of(project.property(mainToolchain).toString()) |
||||
java { |
||||
toolchain { |
||||
languageVersion = mainLanguageVersion |
||||
} |
||||
} |
||||
} |
||||
else { |
||||
// Fallback to JDK8 |
||||
java { |
||||
sourceCompatibility = JavaVersion.VERSION_1_8 |
||||
} |
||||
} |
||||
// Configure a specific Java Toolchain for compiling and running tests if the 'testToolchain' property is defined |
||||
if (project.hasProperty(testToolchain)) { |
||||
def testLanguageVersion = JavaLanguageVersion.of(project.property(testToolchain).toString()); |
||||
tasks.withType(JavaCompile).matching { it.name.contains("Test") }.configureEach { |
||||
javaCompiler = javaToolchains.compilerFor { |
||||
languageVersion = testLanguageVersion |
||||
} |
||||
} |
||||
tasks.withType(Test).configureEach{ |
||||
javaLauncher = javaToolchains.launcherFor { |
||||
languageVersion = testLanguageVersion |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
plugins.withType(GroovyPlugin) { |
||||
// Fallback to JDK8 |
||||
if (!project.hasProperty(mainToolchain)) { |
||||
compileGroovy { |
||||
sourceCompatibility = JavaVersion.VERSION_1_8 |
||||
} |
||||
} |
||||
} |
||||
|
||||
// Configure the Kotlin compiler if the 'mainToolchain' property is defined |
||||
pluginManager.withPlugin("kotlin") { |
||||
if (project.hasProperty(mainToolchain)) { |
||||
def mainLanguageVersion = JavaLanguageVersion.of(project.property(mainToolchain).toString()); |
||||
def compiler = javaToolchains.compilerFor { |
||||
languageVersion = mainLanguageVersion |
||||
} |
||||
// See https://kotlinlang.org/docs/gradle.html#attributes-specific-for-jvm |
||||
def javaVersion = mainLanguageVersion.toString() == '8' ? '1.8' : mainLanguageVersion.toString() |
||||
compileKotlin { |
||||
kotlinOptions { |
||||
jvmTarget = javaVersion |
||||
jdkHome = compiler.get().metadata.installationPath.asFile.absolutePath |
||||
} |
||||
} |
||||
// Compile the test classes with the same version, 'testToolchain' will override if defined |
||||
compileTestKotlin { |
||||
kotlinOptions { |
||||
jvmTarget = javaVersion |
||||
jdkHome = compiler.get().metadata.installationPath.asFile.absolutePath |
||||
} |
||||
} |
||||
} |
||||
else { |
||||
compileKotlin { |
||||
kotlinOptions { |
||||
jvmTarget = '1.8' |
||||
} |
||||
} |
||||
} |
||||
|
||||
if (project.hasProperty(testToolchain)) { |
||||
def testLanguageVersion = JavaLanguageVersion.of(project.property(testToolchain).toString()); |
||||
def compiler = javaToolchains.compilerFor { |
||||
languageVersion = testLanguageVersion |
||||
} |
||||
// See https://kotlinlang.org/docs/gradle.html#attributes-specific-for-jvm |
||||
def javaVersion = testLanguageVersion.toString() == '8' ? '1.8' : testLanguageVersion.toString() |
||||
compileTestKotlin { |
||||
kotlinOptions { |
||||
jvmTarget = javaVersion |
||||
jdkHome = compiler.get().metadata.installationPath.asFile.absolutePath |
||||
} |
||||
} |
||||
} |
||||
else { |
||||
compileTestKotlin { |
||||
kotlinOptions { |
||||
jvmTarget = '1.8' |
||||
} |
||||
} |
||||
} |
||||
} |
Loading…
Reference in new issue