From 9b4ccf4e561815ace32c48e1f989e725f6daf590 Mon Sep 17 00:00:00 2001 From: Cedric Champeau Date: Tue, 8 Mar 2022 08:46:54 +0100 Subject: [PATCH] Use argument file to call AOT tasks Once again, we're facing the fact that despite living in 2022, Windows is still incapable of providing sensible defaults: invoking a CLI tool with a long classpath will fail. As a consequence, we have to invoke the AOT CLI tools with an argument file to workaround the problem. Fixes #397 --- .../aot/AbstractMicronautAotCliTask.java | 66 ++++++++++++------- 1 file changed, 42 insertions(+), 24 deletions(-) diff --git a/aot-plugin/src/main/java/io/micronaut/gradle/aot/AbstractMicronautAotCliTask.java b/aot-plugin/src/main/java/io/micronaut/gradle/aot/AbstractMicronautAotCliTask.java index 4100ca54..feea18f4 100644 --- a/aot-plugin/src/main/java/io/micronaut/gradle/aot/AbstractMicronautAotCliTask.java +++ b/aot-plugin/src/main/java/io/micronaut/gradle/aot/AbstractMicronautAotCliTask.java @@ -31,6 +31,9 @@ import javax.inject.Inject; import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -69,34 +72,49 @@ protected void onSuccess(File outputDir) { } @TaskAction - public final void execute() { + public final void execute() throws IOException { File outputDir = getOutputDirectory().getAsFile().get(); getFileOperations().delete(outputDir); - ExecResult javaexec = getExecOperations().javaexec(spec -> { - FileCollection classpath = getOptimizerClasspath().plus(getClasspath()); - spec.setClasspath(classpath); - spec.getMainClass().set("io.micronaut.aot.cli.Main"); - List args = new ArrayList<>(Arrays.asList( - "--classpath", getClasspath().getAsPath(), - "--runtime", getTargetRuntime().get().name().toUpperCase(), - "--package", getTargetPackage().get() - )); - maybeAddOptimizerClasspath(args, getClasspath()); - configureExtraArguments(args); - spec.args(args); - getLogger().info("Running AOT optimizer with parameters: {}", args); - if (getDebug().get()) { - getLogger().info("Running with debug enabled"); - spec.jvmArgs("-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5005"); + File argFile = File.createTempFile("aot", "args"); + try { + ExecResult javaexec = getExecOperations().javaexec(spec -> { + FileCollection classpath = getOptimizerClasspath().plus(getClasspath()); + spec.setClasspath(classpath); + spec.getMainClass().set("io.micronaut.aot.cli.Main"); + List args = new ArrayList<>(Arrays.asList( + "--classpath", getClasspath().getAsPath(), + "--runtime", getTargetRuntime().get().name().toUpperCase(), + "--package", getTargetPackage().get() + )); + maybeAddOptimizerClasspath(args, getClasspath()); + configureExtraArguments(args); + boolean useArgFile = true; + try (PrintWriter wrt = new PrintWriter(new FileWriter(argFile))) { + args.forEach(wrt::println); + } catch (IOException e) { + useArgFile = false; + } + if (useArgFile) { + spec.args("@" + argFile.getAbsolutePath()); + } else { + spec.args(args); + } + getLogger().info("Running AOT optimizer {} with parameters: {}", useArgFile ? "using arg file" : "directly", args); + if (getDebug().get()) { + getLogger().info("Running with debug enabled"); + spec.jvmArgs("-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5005"); + } + if (getEnvironmentVariables().isPresent()) { + spec.environment(getEnvironmentVariables().get()); + } + }); + if (javaexec.getExitValue() != 0) { + throw new GradleException("AOT analysis failed"); } - if (getEnvironmentVariables().isPresent()) { - spec.environment(getEnvironmentVariables().get()); - } - }); - if (javaexec.getExitValue() != 0) { - throw new GradleException("AOT analysis failed"); + } finally { + onSuccess(outputDir); + argFile.delete(); } - onSuccess(outputDir); } private void maybeAddOptimizerClasspath(List args, ConfigurableFileCollection classpath) {