From 8f02e1088d7d12918f9fa594837aa32252e6c55c Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Wed, 5 Feb 2020 15:11:59 +0100 Subject: [PATCH] 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 --- build.gradle | 4 +- gradle/build-scan-user-data.gradle | 16 ++++++ gradle/custom-java-home.gradle | 87 ++++++++++++++++++++++++++++++ spring-oxm/spring-oxm.gradle | 5 +- 4 files changed, 109 insertions(+), 3 deletions(-) create mode 100644 gradle/custom-java-home.gradle diff --git a/build.gradle b/build.gradle index 24be2186d01..e597ed582e0 100644 --- a/build.gradle +++ b/build.gradle @@ -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 -> 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") { diff --git a/gradle/build-scan-user-data.gradle b/gradle/build-scan-user-data.gradle index d4f483d66f2..c9463e7f33d 100644 --- a/gradle/build-scan-user-data.gradle +++ b/gradle/build-scan-user-data.gradle @@ -4,6 +4,8 @@ tagCiOrLocal() addCiMetadata() addGitMetadata() addTestTaskMetadata() +addCustomJavaHomeMetadata() +addCustomJavaSourceVersionMetadata() void tagOs() { buildScan.tag System.getProperty('os.name') @@ -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() } diff --git a/gradle/custom-java-home.gradle b/gradle/custom-java-home.gradle new file mode 100644 index 00000000000..39d2619e9a2 --- /dev/null +++ b/gradle/custom-java-home.gradle @@ -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) + } + } + +} diff --git a/spring-oxm/spring-oxm.gradle b/spring-oxm/spring-oxm.gradle index 530245c1262..9d23276d228 100644 --- a/spring-oxm/spring-oxm.gradle +++ b/spring-oxm/spring-oxm.gradle @@ -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"