From b32ff5cd9c8da14fc2b756ccca2fdc860403b113 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yoann=20Rodi=C3=A8re?= Date: Wed, 23 Oct 2019 08:45:47 +0200 Subject: [PATCH] HHH-13682 Allow forcing the tested Java version in the Gradle build ... just in case we need that for some cutting-edge JDK, for example 15, that would not be supported by Gradle yet. --- build.gradle | 2 +- gradle/base-information.gradle | 15 --------- gradle/java-module.gradle | 6 ++-- ...ernate-integrationtest-java-modules.gradle | 2 +- settings.gradle | 31 ++++++++++++++++++- 5 files changed, 35 insertions(+), 21 deletions(-) diff --git a/build.gradle b/build.gradle index 6e7f479bb1..a67fae0768 100644 --- a/build.gradle +++ b/build.gradle @@ -58,7 +58,7 @@ task release { // Force to release with JDK 8. Releasing with JDK 11 is not supported yet: // - the hibernate-orm-modules tests do not run due to an issue with the ASM version currently used by Gradle doFirst { - if ( !JavaVersion.current().isJava8() ) { + if ( !JavaVersion.current().isJava8() || !gradle.ext.testedJavaVersionAsEnum.isJava8() ) { throw new IllegalStateException( "Please use JDK 8 to perform the release." ) } } diff --git a/gradle/base-information.gradle b/gradle/base-information.gradle index 6972f7e189..d07aa5702c 100644 --- a/gradle/base-information.gradle +++ b/gradle/base-information.gradle @@ -13,21 +13,6 @@ ext { jpaVersion = new JpaVersion('2.2') } -if ( JavaVersion.current() == JavaVersion.VERSION_HIGHER ) { - // This JDK is not supported by Gradle. - // Use a hack to retrieve the major as a string. - - // This only works for Java 9+ (we're at least on Java 13 here). - def major = System.getProperty( 'java.specification.version' ) - logger.warn( "[WARN] The Java version '$major' is not supported by gradle." ) - - ext.testedJavaVersion = major -} -else { - // This JDK is supported by Gradle. - ext.testedJavaVersion = JavaVersion.current() -} - group = 'org.hibernate' version = project.ormVersion.fullName diff --git a/gradle/java-module.gradle b/gradle/java-module.gradle index 913b1327db..b3fc25c29a 100644 --- a/gradle/java-module.gradle +++ b/gradle/java-module.gradle @@ -117,13 +117,13 @@ tasks.withType( JavaCompile ) { targetCompatibility = project.baselineJavaVersion } -if ( project.baselineJavaVersion != project.testedJavaVersion ) { - logger.info( "Forcing the target bytecode version for test classes to '$project.testedJavaVersion'" ) +if ( project.baselineJavaVersion != gradle.ext.testedJavaVersion ) { + logger.info( "Forcing the target bytecode version for test classes to '$gradle.ext.testedJavaVersion'" ) tasks.compileTestJava { // For *tests only*, generate bytecode matching the Java version currently in use. // This allows testing bytecode enhancement on the latest Java versions (13, 14, ...). - targetCompatibility = project.testedJavaVersion + targetCompatibility = gradle.ext.testedJavaVersion } } diff --git a/hibernate-integrationtest-java-modules/hibernate-integrationtest-java-modules.gradle b/hibernate-integrationtest-java-modules/hibernate-integrationtest-java-modules.gradle index bcd9bfac9a..663e585f44 100644 --- a/hibernate-integrationtest-java-modules/hibernate-integrationtest-java-modules.gradle +++ b/hibernate-integrationtest-java-modules/hibernate-integrationtest-java-modules.gradle @@ -25,7 +25,7 @@ apply from: rootProject.file( 'gradle/java-module.gradle' ) apply plugin: "org.javamodularity.moduleplugin" // Override -source and -target to the version being tested (11+, since this module is enabled) -ext.baselineJavaVersion = project.testedJavaVersion +ext.baselineJavaVersion = gradle.ext.testedJavaVersion tasks.withType( JavaCompile ) { sourceCompatibility = project.baselineJavaVersion targetCompatibility = project.baselineJavaVersion diff --git a/settings.gradle b/settings.gradle index 1abab8f11e..aa16052c52 100644 --- a/settings.gradle +++ b/settings.gradle @@ -10,7 +10,36 @@ plugins { } if ( !JavaVersion.current().java8Compatible ) { - throw new GradleException( "Gradle must be run with Java 8" ) + throw new GradleException( "Gradle must be run with Java 8 or later" ) +} + +// Consume the property 'testedJavaVersion' here and +// set it on gradle.ext so that we can inspect the result in build.gradle. +// We wouldn't be able to do that if we consumed +// the property in base-information.gradle and set it on project.ext. + +if ( hasProperty( 'testedJavaVersion' ) ) { + logger.warn( "[WARN] Targeting Java version '$testedJavaVersion' in tests." ) + gradle.ext.testedJavaVersion = testedJavaVersion + gradle.ext.testedJavaVersionAsEnum = JavaVersion.toVersion( testedJavaVersion ) +} +else { + // We will simply use Gradle's JDK for compilation, tests and javadoc generation. + def major + if ( JavaVersion.current() == JavaVersion.VERSION_HIGHER) { + logger.warn( "Gradle does not support this JDK, because it is too recent; build is likely to fail." + + " To avoid failures, you should specify an older Java version in the 'testedJavaVersion' parameter." + + " Just append the following to your gradle command:" + + " '-PtestedJavaVersion='" ) + // Use a hack to retrieve the major as a string. + // This only works for Java 9+ (we're at least on Java 18 here). + gradle.ext.testedJavaVersion = System.getProperty( 'java.specification.version' ) + } + else { + gradle.ext.testedJavaVersion = JavaVersion.current().getMajorVersion() + } + + gradle.ext.testedJavaVersionAsEnum = JavaVersion.current() } include 'hibernate-core'