From 1b44713d0926d3603641d9adf90addc45f21a35f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yoann=20Rodi=C3=A8re?= Date: Mon, 21 Oct 2019 11:02:11 +0200 Subject: [PATCH] HHH-14323 Generate Java 13/14 bytecode for tests when building with JDK13/14 --- gradle/base-information.gradle | 15 +++++++++++++++ gradle/java-module.gradle | 18 +++++++++++++----- ...bernate-integrationtest-java-modules.gradle | 10 ++++++---- 3 files changed, 34 insertions(+), 9 deletions(-) diff --git a/gradle/base-information.gradle b/gradle/base-information.gradle index 3affd900a4..871fdc19b0 100644 --- a/gradle/base-information.gradle +++ b/gradle/base-information.gradle @@ -20,6 +20,21 @@ 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 a362ebe190..472fa50c50 100644 --- a/gradle/java-module.gradle +++ b/gradle/java-module.gradle @@ -38,10 +38,6 @@ ext { forbiddenAPITargetJDKCompatibility = '11' } - -sourceCompatibility = project.baselineJavaVersion -targetCompatibility = project.baselineJavaVersion - if ( !project.description ) { project.description = "The Hibernate ORM $project.name module" } @@ -114,8 +110,20 @@ dependencies { // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // Compilation -tasks.withType(JavaCompile) { +tasks.withType( JavaCompile ) { options.encoding = 'UTF-8' + sourceCompatibility = project.baselineJavaVersion + targetCompatibility = project.baselineJavaVersion +} + +if ( project.baselineJavaVersion != project.testedJavaVersion ) { + logger.info( "Forcing the target bytecode version for test classes to '$project.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 + } } task compile(dependsOn: [compileJava, processResources, compileTestJava, processTestResources] ) diff --git a/hibernate-integrationtest-java-modules/hibernate-integrationtest-java-modules.gradle b/hibernate-integrationtest-java-modules/hibernate-integrationtest-java-modules.gradle index 233ce94e94..bcd9bfac9a 100644 --- a/hibernate-integrationtest-java-modules/hibernate-integrationtest-java-modules.gradle +++ b/hibernate-integrationtest-java-modules/hibernate-integrationtest-java-modules.gradle @@ -24,10 +24,12 @@ apply from: rootProject.file( 'gradle/java-module.gradle' ) // so we have to use https://github.com/java9-modularity/gradle-modules-plugin apply plugin: "org.javamodularity.moduleplugin" -// Override -source and -target -ext.baselineJavaVersion = 11 -sourceCompatibility = project.baselineJavaVersion -targetCompatibility = project.baselineJavaVersion +// Override -source and -target to the version being tested (11+, since this module is enabled) +ext.baselineJavaVersion = project.testedJavaVersion +tasks.withType( JavaCompile ) { + sourceCompatibility = project.baselineJavaVersion + targetCompatibility = project.baselineJavaVersion +} // Checkstyle fails for module-info checkstyleMain.exclude '**/module-info.java'