From 6edcde1667b32428239c42951b97fd990927f8e4 Mon Sep 17 00:00:00 2001 From: Christian Beikov Date: Tue, 15 Aug 2023 19:35:08 +0200 Subject: [PATCH] Fix Gradle toolchain issues --- gradle/java-module.gradle | 20 ------- hibernate-core/hibernate-core.gradle | 6 +- .../hibernate/orm/post/LoggingReportTask.java | 2 +- .../orm/toolchains/JavaModulePlugin.java | 57 ++++++++++++++++++- .../orm/toolchains/JdkVersionConfig.java | 8 +-- .../hibernate-gradle-plugin.gradle | 4 +- .../hibernate-jpamodelgen.gradle | 6 +- 7 files changed, 68 insertions(+), 35 deletions(-) diff --git a/gradle/java-module.gradle b/gradle/java-module.gradle index 0258834cb0..63b65cac8c 100644 --- a/gradle/java-module.gradle +++ b/gradle/java-module.gradle @@ -177,26 +177,6 @@ if ( toolsJar.exists() ) { // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // Testing - -if ( jdkVersions.explicitlyConfigured ) { - tasks.test { - // Configure version of Java tools - javaLauncher = javaToolchains.launcherFor { - languageVersion = gradle.ext.javaVersions.test.launcher - } - - // Configure JVM Options - jvmArgs( getProperty( 'toolchain.launcher.jvmargs' ).toString().split( ' ' ) ) - if ( project.hasProperty( 'test.jdk.launcher.args' ) ) { - jvmArgs( project.getProperty( 'test.jdk.launcher.args' ).toString().split( ' ' ) ) - } - - // Display version of Java tools - doFirst { - logger.lifecycle "Testing with '${javaLauncher.get().metadata.installationPath}'" - } - } -} class HeapDumpPathProvider implements CommandLineArgumentProvider { @OutputDirectory Provider path diff --git a/hibernate-core/hibernate-core.gradle b/hibernate-core/hibernate-core.gradle index 11d3f18cd3..62449edc21 100644 --- a/hibernate-core/hibernate-core.gradle +++ b/hibernate-core/hibernate-core.gradle @@ -271,7 +271,7 @@ if ( jdkVersions.test.release.asInt() >= 17 && jdkVersions.explicit ) { // For the new source set, we need to configure the source and target version to 17 compileTestJava17Java { javaCompiler = javaToolchains.compilerFor { - languageVersion = gradle.ext.javaVersions.test.compiler + languageVersion = jdkVersions.test.compiler } sourceCompatibility = 17 targetCompatibility = 17 @@ -295,13 +295,13 @@ if ( jdkVersions.test.release.asInt() >= 17 && jdkVersions.explicit ) { // We execute the Java 17 tests in a custom test task task testJava17(type: Test) { javaLauncher = javaToolchains.launcherFor { - languageVersion = gradle.ext.javaVersions.test.launcher + languageVersion = jdkVersions.test.launcher } useJUnitPlatform() testClassesDirs = sourceSets.testJava17.output.classesDirs classpath = sourceSets.testJava17.runtimeClasspath - if ( gradle.ext.javaVersions.test.launcher.asInt() >= 19 ) { + if ( jdkVersions.test.launcher.asInt() >= 19 ) { logger.warn( "The version of Java bytecode that will be tested is not supported by Bytebuddy by default. " + " Setting 'net.bytebuddy.experimental=true'." ) systemProperty 'net.bytebuddy.experimental', true diff --git a/local-build-plugins/src/main/java/org/hibernate/orm/post/LoggingReportTask.java b/local-build-plugins/src/main/java/org/hibernate/orm/post/LoggingReportTask.java index 1cd0324943..f5eb413bac 100644 --- a/local-build-plugins/src/main/java/org/hibernate/orm/post/LoggingReportTask.java +++ b/local-build-plugins/src/main/java/org/hibernate/orm/post/LoggingReportTask.java @@ -122,7 +122,7 @@ public abstract class LoggingReportTask extends AbstractJandexAwareTask { private IdRange calculateIdRange(AnnotationInstance msgLoggerAnnUsage, SubSystem subSystem) { final ClassInfo loggerClassInfo = msgLoggerAnnUsage.target().asClass(); - getProject().getLogger().lifecycle( "MessageLogger (`%s`) missing id-range", loggerClassInfo.simpleName() ); + getProject().getLogger().lifecycle( "MessageLogger (`{}`) missing id-range", loggerClassInfo.simpleName() ); final List messageAnnUsages = loggerClassInfo.annotations( MSG_ANN_NAME ); if ( messageAnnUsages.isEmpty() ) { diff --git a/local-build-plugins/src/main/java/org/hibernate/orm/toolchains/JavaModulePlugin.java b/local-build-plugins/src/main/java/org/hibernate/orm/toolchains/JavaModulePlugin.java index 8d871b9ce4..22dde0ae6a 100644 --- a/local-build-plugins/src/main/java/org/hibernate/orm/toolchains/JavaModulePlugin.java +++ b/local-build-plugins/src/main/java/org/hibernate/orm/toolchains/JavaModulePlugin.java @@ -22,6 +22,7 @@ import org.gradle.api.tasks.SourceSetContainer; import org.gradle.api.tasks.compile.CompileOptions; import org.gradle.api.tasks.compile.JavaCompile; import org.gradle.api.tasks.javadoc.Javadoc; +import org.gradle.api.tasks.testing.Test; import org.gradle.jvm.toolchain.JavaLanguageVersion; import org.gradle.jvm.toolchain.JavaToolchainService; @@ -50,6 +51,7 @@ public class JavaModulePlugin implements Plugin { final JavaCompile mainCompileTask = (JavaCompile) project.getTasks().getByName( mainSourceSet.getCompileJavaTaskName() ); final JavaCompile testCompileTask = (JavaCompile) project.getTasks().getByName( testSourceSet.getCompileJavaTaskName() ); + final Test testTask = (Test) project.getTasks().findByName( testSourceSet.getName() ); if ( !jdkVersionsConfig.isExplicitlyConfigured() ) { mainCompileTask.setSourceCompatibility( jdkVersionsConfig.getMainReleaseVersion().toString() ); @@ -63,6 +65,7 @@ public class JavaModulePlugin implements Plugin { configureCompileTasks( project ); configureJavadocTasks( project ); + configureTestTasks( project ); configureCompileTask( mainCompileTask, jdkVersionsConfig.getMainReleaseVersion() ); configureCompileTask( testCompileTask, jdkVersionsConfig.getTestReleaseVersion() ); @@ -72,6 +75,13 @@ public class JavaModulePlugin implements Plugin { javaToolchainSpec.getLanguageVersion().set( jdkVersionsConfig.getTestCompileVersion() ); } ) ); + if ( testTask != null ) { + testTask.getJavaLauncher().set( + toolchainService.launcherFor( javaToolchainSpec -> { + javaToolchainSpec.getLanguageVersion().set( jdkVersionsConfig.getTestLauncherVersion() ); + } ) + ); + } } } @@ -97,7 +107,7 @@ public class JavaModulePlugin implements Plugin { @Override public void execute(Task task) { project.getLogger().lifecycle( - "Compiling with '%s'", + "Compiling with '{}'", compileTask.getJavaCompiler().get().getMetadata().getInstallationPath() ); } @@ -116,7 +126,7 @@ public class JavaModulePlugin implements Plugin { @Override public void execute(Task task) { project.getLogger().lifecycle( - "Generating javadoc with '%s'", + "Generating javadoc with '{}}'", javadocTask.getJavadocTool().get().getMetadata().getInstallationPath() ); } @@ -125,6 +135,37 @@ public class JavaModulePlugin implements Plugin { } ); } + private void configureTestTasks(Project project) { + project.getTasks().withType( Test.class ).configureEach( new Action() { + @Override + public void execute(Test testTask) { + getJvmArgs( testTask ).addAll( + Arrays.asList( + project.property( "toolchain.launcher.jvmargs" ).toString().split( " " ) + ) + ); + if ( project.hasProperty( "test.jdk.launcher.args" ) ) { + getJvmArgs( testTask ).addAll( + Arrays.asList( + project.getProperties().get( "test.jdk.launcher.args" ).toString().split( " " ) + ) + ); + } + testTask.doFirst( + new Action() { + @Override + public void execute(Task task) { + project.getLogger().lifecycle( + "Testing with '{}'", + testTask.getJavaLauncher().get().getMetadata().getInstallationPath() + ); + } + } + ); + } + } ); + } + private static List javadocFlags(Project project) { final String jvmArgs = project.property( "toolchain.javadoc.jvmargs" ).toString(); final String[] splits = jvmArgs.split( " " ); @@ -145,4 +186,16 @@ public class JavaModulePlugin implements Plugin { return existing; } } + + public static List getJvmArgs(Test testTask) { + final List existing = testTask.getJvmArgs(); + if ( existing == null || !( existing instanceof ArrayList ) ) { + final List target = new ArrayList<>(); + testTask.setJvmArgs( target ); + return target; + } + else { + return existing; + } + } } diff --git a/local-build-plugins/src/main/java/org/hibernate/orm/toolchains/JdkVersionConfig.java b/local-build-plugins/src/main/java/org/hibernate/orm/toolchains/JdkVersionConfig.java index 490440f27f..18020922f8 100644 --- a/local-build-plugins/src/main/java/org/hibernate/orm/toolchains/JdkVersionConfig.java +++ b/local-build-plugins/src/main/java/org/hibernate/orm/toolchains/JdkVersionConfig.java @@ -124,10 +124,10 @@ public class JdkVersionConfig { testCompileVersion = requireNonNullElse( explicitTestVersion, baselineJdkVersion ); mainReleaseVersion = baselineJdkVersion; - if ( testCompileVersion.asInt() > gradleJdkVersion.asInt() ) { + if ( testCompileVersion.asInt() > maxSupportedJdkVersion.asInt() ) { System.out.println( - "[WARN] Gradle does not support bytecode version '${testCompilerVersion}'." - + " Forcing test bytecode to version ${GRADLE_MAX_SUPPORTED_BYTECODE_VERSION}." + "[WARN] Gradle does not support bytecode version '" + testCompileVersion + "'." + + " Forcing test bytecode to version " + maxSupportedJdkVersion + "." ); testReleaseVersion = maxSupportedJdkVersion; } @@ -138,7 +138,7 @@ public class JdkVersionConfig { testLauncherVersion = testCompileVersion; return new JdkVersionConfig( - false, + true, baselineJdkVersion, mainCompileVersion, mainReleaseVersion, diff --git a/tooling/hibernate-gradle-plugin/hibernate-gradle-plugin.gradle b/tooling/hibernate-gradle-plugin/hibernate-gradle-plugin.gradle index ed65b61e34..f947efaf6e 100644 --- a/tooling/hibernate-gradle-plugin/hibernate-gradle-plugin.gradle +++ b/tooling/hibernate-gradle-plugin/hibernate-gradle-plugin.gradle @@ -130,8 +130,8 @@ tasks.withType( JavaCompile ) { if ( !jdkVersions.explicit ) { tasks.withType( GroovyCompile ) { - sourceCompatibility = JavaVersion.toVersion( gradle.jdkVersions.baseline ) - targetCompatibility = JavaVersion.toVersion( gradle.jdkVersions.baseline ) + sourceCompatibility = JavaVersion.toVersion( jdkVersions.baseline ) + targetCompatibility = JavaVersion.toVersion( jdkVersions.baseline ) } } else { diff --git a/tooling/metamodel-generator/hibernate-jpamodelgen.gradle b/tooling/metamodel-generator/hibernate-jpamodelgen.gradle index a158ec3b44..334146d7a0 100644 --- a/tooling/metamodel-generator/hibernate-jpamodelgen.gradle +++ b/tooling/metamodel-generator/hibernate-jpamodelgen.gradle @@ -49,7 +49,7 @@ if ( jdkVersions.test.release.asInt() >= 17 && jdkVersions.explicit ) { //compileTestJava17Java { compileTestJava { javaCompiler = javaToolchains.compilerFor { - languageVersion = gradle.jdkVersions.test.compile + languageVersion = jdkVersions.test.compile } sourceCompatibility = 17 targetCompatibility = 17 @@ -57,10 +57,10 @@ if ( jdkVersions.test.release.asInt() >= 17 && jdkVersions.explicit ) { test { javaLauncher = javaToolchains.launcherFor { - languageVersion = gradle.ext.javaVersions.test.launcher + languageVersion = jdkVersions.test.launcher } - if ( gradle.ext.javaVersions.test.launcher.asInt() >= 19 ) { + if ( jdkVersions.test.launcher.asInt() >= 19 ) { logger.warn( "The version of Java bytecode that will be tested is not supported by Bytebuddy by default. " + " Setting 'net.bytebuddy.experimental=true'." ) systemProperty 'net.bytebuddy.experimental', true