HHH-14323 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.
This commit is contained in:
Yoann Rodière 2019-10-23 08:45:47 +02:00
parent ddb474f7c3
commit 4bf0a96c22
No known key found for this signature in database
GPG Key ID: B8D049359DC35ABC
5 changed files with 35 additions and 21 deletions

View File

@ -58,7 +58,7 @@ task release {
// Force to release with JDK 8. Releasing with JDK 11 is not supported yet: // 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 // - the hibernate-orm-modules tests do not run due to an issue with the ASM version currently used by Gradle
doFirst { doFirst {
if ( !JavaVersion.current().isJava8() ) { if ( !JavaVersion.current().isJava8() || !gradle.ext.testedJavaVersionAsEnum.isJava8() ) {
throw new IllegalStateException( "Please use JDK 8 to perform the release." ) throw new IllegalStateException( "Please use JDK 8 to perform the release." )
} }
} }

View File

@ -20,21 +20,6 @@ ext {
jpaVersion = new JpaVersion('2.2') 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' group = 'org.hibernate'
version = project.ormVersion.fullName version = project.ormVersion.fullName

View File

@ -116,13 +116,13 @@ tasks.withType( JavaCompile ) {
targetCompatibility = project.baselineJavaVersion targetCompatibility = project.baselineJavaVersion
} }
if ( project.baselineJavaVersion != project.testedJavaVersion ) { if ( project.baselineJavaVersion != gradle.ext.testedJavaVersion ) {
logger.info( "Forcing the target bytecode version for test classes to '$project.testedJavaVersion'" ) logger.info( "Forcing the target bytecode version for test classes to '$gradle.ext.testedJavaVersion'" )
tasks.compileTestJava { tasks.compileTestJava {
// For *tests only*, generate bytecode matching the Java version currently in use. // For *tests only*, generate bytecode matching the Java version currently in use.
// This allows testing bytecode enhancement on the latest Java versions (13, 14, ...). // This allows testing bytecode enhancement on the latest Java versions (13, 14, ...).
targetCompatibility = project.testedJavaVersion targetCompatibility = gradle.ext.testedJavaVersion
} }
} }

View File

@ -25,7 +25,7 @@ apply from: rootProject.file( 'gradle/java-module.gradle' )
apply plugin: "org.javamodularity.moduleplugin" apply plugin: "org.javamodularity.moduleplugin"
// Override -source and -target to the version being tested (11+, since this module is enabled) // 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 ) { tasks.withType( JavaCompile ) {
sourceCompatibility = project.baselineJavaVersion sourceCompatibility = project.baselineJavaVersion
targetCompatibility = project.baselineJavaVersion targetCompatibility = project.baselineJavaVersion

View File

@ -10,7 +10,36 @@ plugins {
} }
if ( !JavaVersion.current().java8Compatible ) { 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=<major version of your newer JDK, e.g. 14>'" )
// 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' include 'hibernate-core'