From 931596d7e16b0ce6c1a6e679470eeb536c1efa6a Mon Sep 17 00:00:00 2001 From: Oliver Kopp Date: Thu, 4 Jul 2024 02:28:49 +0200 Subject: [PATCH 1/5] Add usage of java Command-Line Argument File on Windows --- .../moduleplugin/tasks/RunTaskMutator.java | 34 ++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/javamodularity/moduleplugin/tasks/RunTaskMutator.java b/src/main/java/org/javamodularity/moduleplugin/tasks/RunTaskMutator.java index 613090d..10af506 100644 --- a/src/main/java/org/javamodularity/moduleplugin/tasks/RunTaskMutator.java +++ b/src/main/java/org/javamodularity/moduleplugin/tasks/RunTaskMutator.java @@ -7,18 +7,23 @@ import org.gradle.api.logging.Logger; import org.gradle.api.logging.Logging; import org.gradle.api.tasks.JavaExec; +import org.gradle.internal.os.OperatingSystem; import org.gradle.util.GradleVersion; import org.javamodularity.moduleplugin.extensions.PatchModuleContainer; import org.javamodularity.moduleplugin.extensions.RunModuleOptions; import org.javamodularity.moduleplugin.internal.TaskOption; +import java.io.IOException; import java.nio.file.Files; +import java.nio.file.Path; import java.util.ArrayList; import java.util.List; +import java.util.StringJoiner; import java.util.stream.Collectors; public class RunTaskMutator extends AbstractExecutionMutator { private static final Logger LOGGER = Logging.getLogger(RunTaskMutator.class); + private static final String LINE_SEP = System.getProperty("line.separator"); public RunTaskMutator(JavaExec execTask, Project project) { super(execTask, project); @@ -35,7 +40,34 @@ private void updateJavaExecTask() { @Override public void execute(Task task) { List jvmArgs = buildJavaExecJvmArgs(); - execTask.setJvmArgs(jvmArgs); + + if (!OperatingSystem.current().isWindows()) { + // No need for patching + execTask.setJvmArgs(jvmArgs); + execTask.setClasspath(project.files()); + } + + // https://github.com/java9-modularity/gradle-modules-plugin/issues/281 + + List newJvmArgs = new ArrayList<>(); + + StringJoiner parametersJoiner = new StringJoiner("\"" + LINE_SEP + "\"", "\"", "\""); + for (String jvmArg : jvmArgs) { + if (jvmArg.startsWith("@")) { + newJvmArgs.add(jvmArg); + } else { + parametersJoiner.add(jvmArg.replace("\\", "\\\\")); + } + } + try { + Path parameterFile = Files.createTempFile("jvm-args", ".txt"); + Files.write(parameterFile, parametersJoiner.toString().getBytes()); + newJvmArgs.add("@" + parameterFile.toAbsolutePath()); + execTask.setJvmArgs(newJvmArgs); + LOGGER.info("Patched jvmArgs for task {}: {}", execTask.getName(), newJvmArgs); + } catch (IOException e) { + execTask.setJvmArgs(jvmArgs); + } execTask.setClasspath(project.files()); } }); From bcb6c9163f80dbc69bc82a073ea4f0ba47ba7786 Mon Sep 17 00:00:00 2001 From: Oliver Kopp Date: Thu, 4 Jul 2024 10:09:04 +0200 Subject: [PATCH 2/5] Use logging placeholder --- .../org/javamodularity/moduleplugin/ModuleSystemPlugin.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/javamodularity/moduleplugin/ModuleSystemPlugin.java b/src/main/java/org/javamodularity/moduleplugin/ModuleSystemPlugin.java index 33a733d..dd9f3f7 100644 --- a/src/main/java/org/javamodularity/moduleplugin/ModuleSystemPlugin.java +++ b/src/main/java/org/javamodularity/moduleplugin/ModuleSystemPlugin.java @@ -19,9 +19,9 @@ public class ModuleSystemPlugin implements Plugin { @Override public void apply(Project project) { if(GradleVersion.current().compareTo(GradleVersion.version("5.1")) < 0) { - LOGGER.warn("WARNING: You use " + GradleVersion.current() + - ". The minimum version supported (with some limitations) by this plugin is 5.1." + - " It is strongly recommended to use at least Gradle 5.6."); + LOGGER.warn("WARNING: You use {}." + + " The minimum version supported (with some limitations) by this plugin is 5.1." + + " It is strongly recommended to use at least Gradle 5.6.", GradleVersion.current()); } project.getPlugins().apply(JavaPlugin.class); new ModuleName().findModuleName(project).ifPresent(moduleName -> configureModularity(project, moduleName)); From 8e59b6aa4f01b10bf608e19bb2a707354fa6f759 Mon Sep 17 00:00:00 2001 From: Oliver Kopp Date: Thu, 4 Jul 2024 11:07:13 +0200 Subject: [PATCH 3/5] Add option "createCommandLineArgumentFile" --- README.md | 18 ++++++++++++++++++ .../extensions/RunModuleOptions.java | 10 ++++++++++ .../moduleplugin/tasks/RunTaskMutator.java | 12 +++++------- 3 files changed, 33 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 7ae794d..a9c2c45 100644 --- a/README.md +++ b/README.md @@ -943,6 +943,24 @@ Examples on how and where Gradle's eclipse-plugin could (and should) be improved `.classpath`-file is affected if the feature is enabled are available on [GitHub](https://github.com/Alfred-65/gradle-modules-plugin.investigation). +Preventing "command line too long" errors +=== +When using many modules, the command line for `--module-path` can become too long. +A workaround is to use Java's [Command-Line Argument Files](https://docs.oracle.com/javase/9/tools/java.htm#JSWOR-GUID-4856361B-8BFD-4964-AE84-121F5F6CF111). +The workaround can be enabled by setting `createCommandLineArgumentFile` to `true` in the `moduleOptions` part in the `run` configuration. + +
+Groovy DSL + +```groovy +run { + moduleOptions { + createCommandLineArgumentFile = true + } +} +``` +
+ Limitations === diff --git a/src/main/java/org/javamodularity/moduleplugin/extensions/RunModuleOptions.java b/src/main/java/org/javamodularity/moduleplugin/extensions/RunModuleOptions.java index ce8bb99..0fd90d6 100644 --- a/src/main/java/org/javamodularity/moduleplugin/extensions/RunModuleOptions.java +++ b/src/main/java/org/javamodularity/moduleplugin/extensions/RunModuleOptions.java @@ -4,6 +4,16 @@ public class RunModuleOptions extends RuntimeModuleOptions { + private Boolean createCommandLineArgumentFile = false; + + public Boolean getCreateCommandLineArgumentFile() { + return createCommandLineArgumentFile; + } + + public void setCreateCommandLineArgumentFile(Boolean createCommandLineArgumentFile) { + this.createCommandLineArgumentFile = createCommandLineArgumentFile; + } + public RunModuleOptions(Project project) { super(project); } diff --git a/src/main/java/org/javamodularity/moduleplugin/tasks/RunTaskMutator.java b/src/main/java/org/javamodularity/moduleplugin/tasks/RunTaskMutator.java index 10af506..8cece8f 100644 --- a/src/main/java/org/javamodularity/moduleplugin/tasks/RunTaskMutator.java +++ b/src/main/java/org/javamodularity/moduleplugin/tasks/RunTaskMutator.java @@ -7,7 +7,6 @@ import org.gradle.api.logging.Logger; import org.gradle.api.logging.Logging; import org.gradle.api.tasks.JavaExec; -import org.gradle.internal.os.OperatingSystem; import org.gradle.util.GradleVersion; import org.javamodularity.moduleplugin.extensions.PatchModuleContainer; import org.javamodularity.moduleplugin.extensions.RunModuleOptions; @@ -30,24 +29,23 @@ public RunTaskMutator(JavaExec execTask, Project project) { } public void configureRun() { - execTask.getExtensions().create("moduleOptions", RunModuleOptions.class, project); - updateJavaExecTask(); + RunModuleOptions moduleOptions = execTask.getExtensions().create("moduleOptions", RunModuleOptions.class, project); + updateJavaExecTask(moduleOptions); } - private void updateJavaExecTask() { + private void updateJavaExecTask(RunModuleOptions moduleOptions) { // don't convert to lambda: https://github.com/java9-modularity/gradle-modules-plugin/issues/54 execTask.doFirst(new Action() { @Override public void execute(Task task) { List jvmArgs = buildJavaExecJvmArgs(); - if (!OperatingSystem.current().isWindows()) { - // No need for patching + if (!moduleOptions.getCreateCommandLineArgumentFile()) { execTask.setJvmArgs(jvmArgs); execTask.setClasspath(project.files()); } - // https://github.com/java9-modularity/gradle-modules-plugin/issues/281 + // Workaround for 206 command line too long - https://github.com/java9-modularity/gradle-modules-plugin/issues/281 List newJvmArgs = new ArrayList<>(); From b53a801cb69bdf6eab4ca6295a075587d176a39c Mon Sep 17 00:00:00 2001 From: Oliver Kopp Date: Thu, 4 Jul 2024 11:10:20 +0200 Subject: [PATCH 4/5] Add warning message in case of an exception --- .../org/javamodularity/moduleplugin/tasks/RunTaskMutator.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/org/javamodularity/moduleplugin/tasks/RunTaskMutator.java b/src/main/java/org/javamodularity/moduleplugin/tasks/RunTaskMutator.java index 8cece8f..56cfae2 100644 --- a/src/main/java/org/javamodularity/moduleplugin/tasks/RunTaskMutator.java +++ b/src/main/java/org/javamodularity/moduleplugin/tasks/RunTaskMutator.java @@ -64,6 +64,7 @@ public void execute(Task task) { execTask.setJvmArgs(newJvmArgs); LOGGER.info("Patched jvmArgs for task {}: {}", execTask.getName(), newJvmArgs); } catch (IOException e) { + LOGGER.warn("Could not create temporary file for jvmArgs. Falling back to default behavior.", e); execTask.setJvmArgs(jvmArgs); } execTask.setClasspath(project.files()); From ad14603f88bf9021d44dc39e9c82efd49841ab63 Mon Sep 17 00:00:00 2001 From: Oliver Kopp Date: Thu, 4 Jul 2024 11:10:55 +0200 Subject: [PATCH 5/5] Add deletion on exit --- .../org/javamodularity/moduleplugin/tasks/RunTaskMutator.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/org/javamodularity/moduleplugin/tasks/RunTaskMutator.java b/src/main/java/org/javamodularity/moduleplugin/tasks/RunTaskMutator.java index 56cfae2..41a39e0 100644 --- a/src/main/java/org/javamodularity/moduleplugin/tasks/RunTaskMutator.java +++ b/src/main/java/org/javamodularity/moduleplugin/tasks/RunTaskMutator.java @@ -63,6 +63,7 @@ public void execute(Task task) { newJvmArgs.add("@" + parameterFile.toAbsolutePath()); execTask.setJvmArgs(newJvmArgs); LOGGER.info("Patched jvmArgs for task {}: {}", execTask.getName(), newJvmArgs); + parameterFile.toFile().deleteOnExit(); } catch (IOException e) { LOGGER.warn("Could not create temporary file for jvmArgs. Falling back to default behavior.", e); execTask.setJvmArgs(jvmArgs);