This is an automated email from the ASF dual-hosted git repository.

bmarwell pushed a commit to branch MJLINK-64_quoting_fork
in repository https://gitbox.apache.org/repos/asf/maven-jlink-plugin.git

commit ff0e74c4fac8751fba14d4d4ed0f9d69d1073f59
Author: Benjamin Marwell <bmarw...@apache.org>
AuthorDate: Wed Jul 26 20:15:27 2023 +0200

    [MJLINK-64] add quoting on forking
---
 pom.xml                                            |  6 ++++
 .../projects/cli-options/add-options/verify.groovy |  5 ++--
 .../jlink/AbstractJLinkToolchainExecutor.java      |  2 +-
 .../org/apache/maven/plugins/jlink/JLinkMojo.java  |  4 +--
 .../apache/maven/plugins/jlink/JLinkMojoTest.java} | 33 ++++++++++++++--------
 5 files changed, 32 insertions(+), 18 deletions(-)

diff --git a/pom.xml b/pom.xml
index 6d53936..bcd6a28 100644
--- a/pom.xml
+++ b/pom.xml
@@ -107,6 +107,12 @@
       <version>${mavenVersion}</version>
       <scope>provided</scope>
     </dependency>
+    <dependency>
+      <groupId>org.apache.maven.plugin-testing</groupId>
+      <artifactId>maven-plugin-testing-harness</artifactId>
+      <version>3.3.0</version>
+      <scope>test</scope>
+    </dependency>
     <dependency>
       <groupId>org.apache.maven.shared</groupId>
       <artifactId>maven-shared-utils</artifactId>
diff --git a/src/it/projects/cli-options/add-options/verify.groovy 
b/src/it/projects/cli-options/add-options/verify.groovy
index 8bd9385..2281a99 100644
--- a/src/it/projects/cli-options/add-options/verify.groovy
+++ b/src/it/projects/cli-options/add-options/verify.groovy
@@ -24,12 +24,11 @@ import java.util.jar.*
 import org.codehaus.plexus.util.*
 
 String buildlog = new File( basedir, "build.log").text
-assert buildlog.contains( "--add-options" )
-assert buildlog.contains( "\"-Xmx128m --enable-preview -Dvar=value\"" )
+assert buildlog.contains( "--add-options, \"-Xmx128m --enable-preview 
-Dvar=value\"" )
+assert buildlog.contains( "addOptions = [-Xmx128m, --enable-preview, 
-Dvar=value]" )
 
 File target = new File( basedir, "target" )
 assert target.isDirectory()
 
 File artifact = new File( target, 
"maven-jlink-plugin-cli-options-add-options-101.0.zip" )
 assert artifact.isFile()
-
diff --git 
a/src/main/java/org/apache/maven/plugins/jlink/AbstractJLinkToolchainExecutor.java
 
b/src/main/java/org/apache/maven/plugins/jlink/AbstractJLinkToolchainExecutor.java
index 086492c..15fd7db 100644
--- 
a/src/main/java/org/apache/maven/plugins/jlink/AbstractJLinkToolchainExecutor.java
+++ 
b/src/main/java/org/apache/maven/plugins/jlink/AbstractJLinkToolchainExecutor.java
@@ -102,7 +102,7 @@ abstract class AbstractJLinkToolchainExecutor extends 
AbstractJLinkExecutor {
 
     private Commandline createJLinkCommandLine(List<String> jlinkArgs) {
         Commandline cmd = new Commandline();
-        jlinkArgs.forEach(arg -> cmd.createArg().setValue(arg));
+        jlinkArgs.forEach(arg -> cmd.createArg().setValue("\"" + arg + "\""));
 
         return cmd;
     }
diff --git a/src/main/java/org/apache/maven/plugins/jlink/JLinkMojo.java 
b/src/main/java/org/apache/maven/plugins/jlink/JLinkMojo.java
index a5f203d..34487a3 100644
--- a/src/main/java/org/apache/maven/plugins/jlink/JLinkMojo.java
+++ b/src/main/java/org/apache/maven/plugins/jlink/JLinkMojo.java
@@ -577,7 +577,7 @@ public class JLinkMojo extends AbstractJLinkMojo {
         }
     }
 
-    private List<String> createJlinkArgs(Collection<String> pathsOfModules, 
Collection<String> modulesToAdd) {
+    protected List<String> createJlinkArgs(Collection<String> pathsOfModules, 
Collection<String> modulesToAdd) {
         List<String> jlinkArgs = new ArrayList<>();
 
         if (stripDebug) {
@@ -612,7 +612,7 @@ public class JLinkMojo extends AbstractJLinkMojo {
             jlinkArgs.add("--disable-plugin");
             jlinkArgs.add(disablePlugin);
         }
-        if (pathsOfModules != null) {
+        if (pathsOfModules != null && !pathsOfModules.isEmpty()) {
             // @formatter:off
             jlinkArgs.add("--module-path");
             
jlinkArgs.add(getPlatformDependSeparateList(pathsOfModules).replace("\\", 
"\\\\"));
diff --git a/src/it/projects/cli-options/add-options/verify.groovy 
b/src/test/java/org/apache/maven/plugins/jlink/JLinkMojoTest.java
similarity index 54%
copy from src/it/projects/cli-options/add-options/verify.groovy
copy to src/test/java/org/apache/maven/plugins/jlink/JLinkMojoTest.java
index 8bd9385..77d670f 100644
--- a/src/it/projects/cli-options/add-options/verify.groovy
+++ b/src/test/java/org/apache/maven/plugins/jlink/JLinkMojoTest.java
@@ -1,4 +1,3 @@
-
 /*
  * Licensed to the Apache Software Foundation (ASF) under one
  * or more contributor license agreements.  See the NOTICE file
@@ -17,19 +16,29 @@
  * specific language governing permissions and limitations
  * under the License.
  */
+package org.apache.maven.plugins.jlink;
+
+import java.lang.reflect.Field;
+import java.util.List;
+
+import org.junit.jupiter.api.Test;
 
-import java.io.*
-import java.util.*
-import java.util.jar.*
-import org.codehaus.plexus.util.*
+import static org.assertj.core.api.Assertions.assertThat;
 
-String buildlog = new File( basedir, "build.log").text
-assert buildlog.contains( "--add-options" )
-assert buildlog.contains( "\"-Xmx128m --enable-preview -Dvar=value\"" )
+public class JLinkMojoTest {
 
-File target = new File( basedir, "target" )
-assert target.isDirectory()
+    @Test
+    void quote_every_argument() throws Exception {
+        // given
+        JLinkMojo mojo = new JLinkMojo();
+        Field stripDebug = mojo.getClass().getDeclaredField("stripDebug");
+        stripDebug.setAccessible(true);
+        stripDebug.set(mojo, Boolean.TRUE);
 
-File artifact = new File( target, 
"maven-jlink-plugin-cli-options-add-options-101.0.zip" )
-assert artifact.isFile()
+        // when
+        List<String> jlinkArgs = mojo.createJlinkArgs(List.of(), List.of());
 
+        // then
+        assertThat(jlinkArgs).noneMatch(arg -> arg.trim().isBlank());
+    }
+}

Reply via email to