Browse Source

Support alternate JDK versions in Gradle build

This commit adds support for the following two JVM system properties
that control the Gradle 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. Setting this system
  property causes Groovy 3.0 RC3 to be used instead of 2.5.x. 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=/opt/java/jdk-14 test

./gradlew --no-build-cache -DcustomJavaHome=/opt/java/jdk-14 test

./gradlew -DcustomJavaHome=/opt/java/jdk-14 -DcustomJavaSourceVersion=14 test

See gh-24474
pull/24490/head
Sam Brannen 6 years ago
parent
commit
8f02e1088d
  1. 4
      build.gradle
  2. 16
      gradle/build-scan-user-data.gradle
  3. 87
      gradle/custom-java-home.gradle
  4. 5
      spring-oxm/spring-oxm.gradle

4
build.gradle

@ -60,7 +60,8 @@ configure(allprojects) { project -> @@ -60,7 +60,8 @@ configure(allprojects) { project ->
entry 'aspectjtools'
entry 'aspectjweaver'
}
dependencySet(group: 'org.codehaus.groovy', version: '2.5.9') {
// If customJavaHome has been set, we assume we need Groovy 3.0 for testing purposes.
dependencySet(group: 'org.codehaus.groovy', version: System.getProperty("customJavaHome") ? '3.0.0-rc-3' : '2.5.9') {
entry 'groovy'
entry 'groovy-jsr223'
entry 'groovy-templates'
@ -305,6 +306,7 @@ configure([rootProject] + javaProjects) { project -> @@ -305,6 +306,7 @@ configure([rootProject] + javaProjects) { project ->
apply plugin: "java-test-fixtures"
apply plugin: "checkstyle"
apply plugin: 'org.springframework.build.compile'
apply from: "${rootDir}/gradle/custom-java-home.gradle"
apply from: "${rootDir}/gradle/ide.gradle"
pluginManager.withPlugin("kotlin") {

16
gradle/build-scan-user-data.gradle

@ -4,6 +4,8 @@ tagCiOrLocal() @@ -4,6 +4,8 @@ tagCiOrLocal()
addCiMetadata()
addGitMetadata()
addTestTaskMetadata()
addCustomJavaHomeMetadata()
addCustomJavaSourceVersionMetadata()
void tagOs() {
buildScan.tag System.getProperty('os.name')
@ -60,6 +62,20 @@ void addTestTaskMetadata() { @@ -60,6 +62,20 @@ void addTestTaskMetadata() {
}
}
void addCustomJavaHomeMetadata() {
def customJavaHome = System.getProperty("customJavaHome")
if (customJavaHome) {
buildScan.value "Custom JAVA_HOME", customJavaHome
}
}
void addCustomJavaSourceVersionMetadata() {
def customJavaSourceVersion = System.getProperty("customJavaSourceVersion")
if (customJavaSourceVersion) {
buildScan.value "Custom Java Source Version", customJavaSourceVersion
}
}
boolean isCi() {
isBamboo()
}

87
gradle/custom-java-home.gradle

@ -0,0 +1,87 @@ @@ -0,0 +1,87 @@
// -----------------------------------------------------------------------------
//
// 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. Setting this system
// property causes Groovy 3.0 RC3 to be used instead of 2.5.x. 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
//
// -----------------------------------------------------------------------------
import org.gradle.internal.os.OperatingSystem
// import org.jetbrains.kotlin.gradle.dsl.KotlinJvmCompile
def customJavaHome = System.getProperty("customJavaHome")
if (customJavaHome) {
def javacExecutable = customJavaHome + "/bin/javac"
def javaExecutable = customJavaHome + "/bin/java"
if (OperatingSystem.current().isWindows()) {
javacExecutable += ".exe"
javaExecutable += ".exe"
}
def customJavaSourceVersion = System.getProperty("customJavaSourceVersion")
tasks.withType(JavaCompile) {
logger.info("Java compiler for " + it.name + " task in " + project.name + ": " + javacExecutable)
doFirst {
// Avoid compiler warnings for non-existing path entries
classpath = classpath.filter { it.exists() }
}
options.fork = true
options.forkOptions.executable = javacExecutable
options.compilerArgs -= "-Werror"
if (customJavaSourceVersion) {
options.compilerArgs += [ "--release", customJavaSourceVersion]
inputs.property("customJavaSourceVersion", customJavaSourceVersion)
}
inputs.property("customJavaHome", customJavaHome)
}
tasks.withType(GroovyCompile) {
logger.info("Java compiler for " + it.name + " task in " + project.name + ": " + javacExecutable)
options.fork = true
options.forkOptions.executable = javacExecutable
if (customJavaSourceVersion) {
options.compilerArgs += [ "--release", customJavaSourceVersion]
inputs.property("customJavaSourceVersion", customJavaSourceVersion)
}
inputs.property("customJavaHome", customJavaHome)
}
/*
tasks.withType(KotlinJvmCompile) {
logger.info("Java home for " + it.name + " task in " + project.name + ": " + customJavaHome)
kotlinOptions.jdkHome = customJavaHome
inputs.property("customJavaHome", customJavaHome)
}
*/
tasks.withType(Test) {
logger.info("Java executable for " + it.name + " task in " + project.name + ": " + javaExecutable)
executable = javaExecutable
inputs.property("customJavaHome", customJavaHome)
if (customJavaSourceVersion) {
inputs.property("customJavaSourceVersion", customJavaSourceVersion)
}
}
}

5
spring-oxm/spring-oxm.gradle

@ -74,8 +74,9 @@ dependencies { @@ -74,8 +74,9 @@ dependencies {
testRuntime("com.sun.xml.bind:jaxb-impl")
}
// JiBX compiler is currently not compatible with JDK 9+
if (JavaVersion.current() == JavaVersion.VERSION_1_8) {
// JiBX compiler is currently not compatible with JDK 9+.
// If customJavaHome has been set, we assume the custom JDK version is 9+.
if ((JavaVersion.current() == JavaVersion.VERSION_1_8) && !System.getProperty("customJavaSourceVersion")) {
compileTestJava {
def bindingXml = "${projectDir}/src/test/resources/org/springframework/oxm/jibx/binding.xml"

Loading…
Cancel
Save