Skip to content

Apache Lucene open-source search software

An alternative workaround for gradle -J javac arg passing bug (#11937)

Dawid Weissa5e252594685
diff --git a/gradle/hacks/turbocharge-jvm-opts.gradle b/gradle/hacks/turbocharge-jvm-opts.gradle
index 594a7468633..0f404e096b8 100644
--- a/gradle/hacks/turbocharge-jvm-opts.gradle
+++ b/gradle/hacks/turbocharge-jvm-opts.gradle
@@ -44,15 +44,26 @@ allprojects {
// This applies to any JVM when javac runs forked (e.g. error-prone)
// Avoiding the fork entirely is best.
tasks.withType(JavaCompile) { JavaCompile task ->
- if (task.path == ":lucene:core:compileMain19Java") {
- // uses "uschindler" toolchain method, which erases "dweiss" method, but later at configure time
- task.options.forkOptions.jvmArgs += vmOpts
- } else if (task.options.forkOptions.javaHome != null) {
- // uses "dweiss" toolchain method
- task.options.forkOptions.jvmArgs += vmOpts.collect {"-J" + it}
- } else {
- // native or error-prone
- task.options.forkOptions.jvmArgs += vmOpts
- }
+ task.options.forkOptions.jvmArgumentProviders.add(new CommandLineArgumentProvider() {
+ @Override
+ Iterable<String> asArguments() {
+ // Gradle bug: https://github.com/gradle/gradle/issues/22746
+ //
+ // Evaluation of this block is delayed until execution time when
+ // we know which "mode" java compiler task will pick and can set arguments
+ // accordingly.
+ //
+ // There is a side-effect to this that arguments passed via the provider
+ // are not part of up-to-date checks but these are internal JVM flags so we
+ // don't care.
+ //
+ // Pass VM options via -J only with a custom javaHome AND when java toolchains are not used.
+ if (task.options.forkOptions.javaHome != null && task.javaCompiler.getOrNull() == null) {
+ return vmOpts.collect {"-J" + it}
+ } else {
+ return vmOpts
+ }
+ }
+ })
}
}