From 0e2513663f200292c8ff0a6a51e8192bbff5d8b3 Mon Sep 17 00:00:00 2001 From: Leon Linhart Date: Thu, 2 Mar 2023 18:53:27 +0100 Subject: [PATCH] feat: support Gradle 8 Closes #9 --- docs/changelog/0.2.0.md | 5 ++++ .../gradle/ecj/plugins/ECJPlugin.kt | 29 ++++++++++++------- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/docs/changelog/0.2.0.md b/docs/changelog/0.2.0.md index a397e45..50816ef 100644 --- a/docs/changelog/0.2.0.md +++ b/docs/changelog/0.2.0.md @@ -4,6 +4,11 @@ _Not Released Yet_ #### Improvements +- Added support for Gradle 8.0.2. + - On Gradle 8.0.2 and later, the `javaCompiler` property of `JavaCompile` + tasks is now respected and can be used to configure the JVM to be used to + run invoke the compiler. + - Additionally, the up-to-date check should behave more predictably on Gradle 8. - Reworked the plugin to avoid eager task creation by migration to Gradle's APIs for lazy configuration. [[GH-10](https://github.com/TheMrMilchmann/gradle-ecj/pull/10)] (Thanks to @arturbosch) - Updated default ECJ version to `3.32.0` (from `3.30.0`). \ No newline at end of file diff --git a/src/main/kotlin/io/github/themrmilchmann/gradle/ecj/plugins/ECJPlugin.kt b/src/main/kotlin/io/github/themrmilchmann/gradle/ecj/plugins/ECJPlugin.kt index f972f66..1d2fcae 100644 --- a/src/main/kotlin/io/github/themrmilchmann/gradle/ecj/plugins/ECJPlugin.kt +++ b/src/main/kotlin/io/github/themrmilchmann/gradle/ecj/plugins/ECJPlugin.kt @@ -80,11 +80,6 @@ public class ECJPlugin : Plugin { val javaToolchains = extensions.getByType(JavaToolchainService::class.java) tasks.withType(JavaCompile::class.java).configureEach { - if (GradleVersion.current() < GRADLE_8_0) { - /* Overwrite the javaCompiler to make sure that it is not inferred from the toolchain. */ - javaCompiler.set(provider { null }) - } - /* ECJ does not support generating JNI headers. Make sure the property is not used. */ options.headerOutputDirectory.set(provider { null }) @@ -99,23 +94,37 @@ public class ECJPlugin : Plugin { * task inputs. Hence, we can only use the languageVersion for now. This could lead to some undesired cache * hits but the chance should be low and there is very little risk of this being an issue. */ - val javaLauncher = provider { + val defaultJavaCompiler = provider { if (java.toolchain.languageVersion.orNull?.canCompileOrRun(REQUIRED_JAVA_VERSION) == true) { - javaToolchains.launcherFor(java.toolchain).orNull ?: error("Could not get launcher for toolchain: ${java.toolchain}") + javaToolchains.compilerFor(java.toolchain).orNull ?: error("Could not get launcher for toolchain: ${java.toolchain}") } else { - javaToolchains.launcherFor { + javaToolchains.compilerFor { languageVersion.set(JavaLanguageVersion.of(PREFERRED_JAVA_VERSION)) }.orNull ?: error("Could not provision launcher for Java $PREFERRED_JAVA_VERSION") } } - inputs.property("javaLauncher", javaLauncher.map { it.metadata.languageVersion.asInt() }) + if (GradleVersion.current() < GRADLE_8_0) { + /* Overwrite the javaCompiler to make sure that it is not inferred from the toolchain. */ + javaCompiler.set(provider { null }) + inputs.property("javaLauncher", defaultJavaCompiler.map { it.metadata.languageVersion.asInt() }) + } else { + this.javaCompiler.convention(defaultJavaCompiler) + } /* See https://docs.gradle.org/7.4.2/userguide/validation_problems.html#implementation_unknown */ @Suppress("ObjectLiteralToLambda") doFirst(object : Action { override fun execute(t: Task) { - options.forkOptions.executable = javaLauncher.get().executablePath.asFile.absolutePath + val javacExecutable = javaCompiler.orElse(defaultJavaCompiler).get().executablePath.asFile + + /* + * We replace the "javac" part of the original executable name instead of simply resolving "java" to + * account for file extensions. + */ + val javaExecutable = javacExecutable.resolveSibling(javacExecutable.name.replace("javac", "java")) + + options.forkOptions.executable = javaExecutable.absolutePath } }) }