You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by sj...@apache.org on 2022/02/26 16:26:11 UTC

[maven-invoker] 01/01: [MSHARED-1019] Allow pass raw cli option to Maven process

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

sjaranowski pushed a commit to branch MSHARED-1019
in repository https://gitbox.apache.org/repos/asf/maven-invoker.git

commit c196bf153cc11bce1f2218b9f11816268b2a8a12
Author: Slawomir Jaranowski <s....@gmail.com>
AuthorDate: Sat Feb 26 17:25:51 2022 +0100

    [MSHARED-1019] Allow pass raw cli option to Maven process
---
 .../shared/invoker/DefaultInvocationRequest.java   | 32 ++++++++++++++
 .../maven/shared/invoker/InvocationRequest.java    | 34 +++++++++++++++
 .../shared/invoker/MavenCommandLineBuilder.java    | 19 ++++++++
 src/site/apt/index.apt.vm                          |  2 +
 .../invoker/MavenCommandLineBuilderTest.java       | 50 ++++++++++++++++++++++
 5 files changed, 137 insertions(+)

diff --git a/src/main/java/org/apache/maven/shared/invoker/DefaultInvocationRequest.java b/src/main/java/org/apache/maven/shared/invoker/DefaultInvocationRequest.java
index 5f990fc..62d4353 100644
--- a/src/main/java/org/apache/maven/shared/invoker/DefaultInvocationRequest.java
+++ b/src/main/java/org/apache/maven/shared/invoker/DefaultInvocationRequest.java
@@ -113,6 +113,10 @@ public class DefaultInvocationRequest
 
     private boolean noTransferProgress;
 
+    private String argLine;
+
+    private boolean argLineAtStart;
+
     /**
      * <p>getBaseDirectory.</p>
      *
@@ -499,6 +503,34 @@ public class DefaultInvocationRequest
         return pomFilename;
     }
 
+    @Override
+    public String getArgLine()
+    {
+        return argLine;
+    }
+
+    @Override
+    public boolean isArgLineAtStart()
+    {
+        return argLineAtStart;
+    }
+
+    @Override
+    public InvocationRequest setArgLineAtStart( String argLine )
+    {
+        this.argLine = argLine;
+        this.argLineAtStart = true;
+        return this;
+    }
+
+    @Override
+    public InvocationRequest setArgLineAtEnd( String argLine )
+    {
+        this.argLine = argLine;
+        this.argLineAtStart = false;
+        return this;
+    }
+
     /** {@inheritDoc} */
     public InvocationRequest setPomFileName( String pomFilename )
     {
diff --git a/src/main/java/org/apache/maven/shared/invoker/InvocationRequest.java b/src/main/java/org/apache/maven/shared/invoker/InvocationRequest.java
index 53ea260..647e13f 100644
--- a/src/main/java/org/apache/maven/shared/invoker/InvocationRequest.java
+++ b/src/main/java/org/apache/maven/shared/invoker/InvocationRequest.java
@@ -191,6 +191,22 @@ public interface InvocationRequest
     String getPomFileName();
 
     /**
+     * Gets raw argLine
+     *
+     * @return a raw argLine for Maven cli.
+     * @since 3.2.0
+     */
+    String getArgLine();
+
+    /**
+     * Where to put raw argLine
+     *
+     * @return raw argLine position.
+     * @since 3.2.0
+     */
+    boolean isArgLineAtStart();
+
+    /**
      * Gets the path to the base directory of the POM for the Maven invocation. If {@link #getPomFile()} does not return
      * <code>null</code>, this setting only affects the working directory for the Maven invocation.
      *
@@ -527,6 +543,24 @@ public interface InvocationRequest
     InvocationRequest setPomFileName( String pomFilename );
 
     /**
+     * Sets raw line passed to Maven cli command at start before other arguments.
+     *
+     * @param argLine a raw Maven arg line
+     * @return This invocation request.
+     * @since 3.2.0
+     */
+    InvocationRequest setArgLineAtStart( String argLine );
+
+    /**
+     * Sets raw line passed to Maven cli command at the end of other arguments.
+     *
+     * @param argLine a raw Maven arg line
+     * @return This invocation request.
+     * @since 3.2.0
+     */
+    InvocationRequest setArgLineAtEnd( String argLine );
+
+    /**
      * Sets the path to the base directory of the POM for the Maven invocation. If {@link #getPomFile()} does not return
      * <code>null</code>, this setting only affects the working directory for the Maven invocation.
      *
diff --git a/src/main/java/org/apache/maven/shared/invoker/MavenCommandLineBuilder.java b/src/main/java/org/apache/maven/shared/invoker/MavenCommandLineBuilder.java
index ada410c..80f83c0 100644
--- a/src/main/java/org/apache/maven/shared/invoker/MavenCommandLineBuilder.java
+++ b/src/main/java/org/apache/maven/shared/invoker/MavenCommandLineBuilder.java
@@ -104,6 +104,8 @@ public class MavenCommandLineBuilder
 
         setThreads( request, cli );
 
+        setArgLine( request, cli );
+
         return cli;
     }
 
@@ -552,6 +554,23 @@ public class MavenCommandLineBuilder
 
     }
 
+    private void setArgLine( InvocationRequest request, Commandline cli )
+        throws CommandLineConfigurationException
+    {
+        String argLine = request.getArgLine();
+        if ( StringUtils.isNotEmpty( argLine ) )
+        {
+            try
+            {
+                cli.createArg( request.isArgLineAtStart() ).setLine( argLine );
+            }
+            catch ( CommandLineException e )
+            {
+                throw new CommandLineConfigurationException( e.getMessage(), e );
+            }
+        }
+    }
+
     private void setupMavenHome( InvocationRequest request )
     {
         if ( request.getMavenHome() != null )
diff --git a/src/site/apt/index.apt.vm b/src/site/apt/index.apt.vm
index 2589c61..b4cf515 100644
--- a/src/site/apt/index.apt.vm
+++ b/src/site/apt/index.apt.vm
@@ -102,6 +102,8 @@ ${project.name}
     
     * Toolchains location ( since Maven3 with -t )
 
+    * Additional raw cli options at the start or the end of command line
+
     []
     
   []
diff --git a/src/test/java/org/apache/maven/shared/invoker/MavenCommandLineBuilderTest.java b/src/test/java/org/apache/maven/shared/invoker/MavenCommandLineBuilderTest.java
index a499551..d61f4db 100644
--- a/src/test/java/org/apache/maven/shared/invoker/MavenCommandLineBuilderTest.java
+++ b/src/test/java/org/apache/maven/shared/invoker/MavenCommandLineBuilderTest.java
@@ -31,6 +31,7 @@ import java.util.Properties;
 import java.util.Set;
 
 import org.apache.maven.shared.utils.Os;
+import org.apache.maven.shared.utils.cli.CommandLineException;
 import org.apache.maven.shared.utils.cli.Commandline;
 import org.junit.After;
 import org.junit.Before;
@@ -40,6 +41,7 @@ import org.junit.rules.TemporaryFolder;
 
 import static org.hamcrest.CoreMatchers.is;
 import static org.hamcrest.CoreMatchers.notNullValue;
+import static org.junit.Assert.assertArrayEquals;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
@@ -471,6 +473,54 @@ public class MavenCommandLineBuilderTest
         assertArgumentsPresent( cli, Collections.singleton( "-fn" ) );
     }
 
+
+    @Test
+    public void testShouldAddRawArLineAtTheEnd() throws CommandLineConfigurationException
+    {
+        InvocationRequest request = newRequest()
+            .setArgLineAtEnd( "testArgLineAtEnd" )
+            .setQuiet( true )
+            .setBuilder( "bId" );
+
+        Commandline commandline = mclb.build( request );
+
+        String[] arguments = commandline.getArguments();
+
+        assertArrayEquals( Arrays.asList( "-b", "bId", "-q", "testArgLineAtEnd" ).toArray(), arguments );
+    }
+
+    @Test
+    public void testShouldAddRawArLineAtTheStart() throws CommandLineConfigurationException
+    {
+        InvocationRequest request = newRequest()
+            .setArgLineAtStart( "testArgLineAtEnd" )
+            .setQuiet( true )
+            .setBuilder( "bId" );
+
+        Commandline commandline = mclb.build( request );
+
+        String[] arguments = commandline.getArguments();
+
+        assertArrayEquals( Arrays.asList( "testArgLineAtEnd", "-b", "bId", "-q" ).toArray(), arguments );
+    }
+
+    @Test
+    public void testInvalidArgLineShouldThrowException() throws CommandLineConfigurationException
+    {
+        InvocationRequest request = newRequest()
+            .setArgLineAtEnd( "testArgLineAtEnd \"xxx" );
+
+        try {
+            mclb.build( request );
+            fail();
+        }
+        catch ( CommandLineConfigurationException e )
+        {
+            assertEquals( "unbalanced quotes in testArgLineAtEnd \"xxx", e.getMessage() );
+            assertEquals( CommandLineException.class, e.getCause().getClass() );
+        }
+    }
+
     @Test
     public void testShouldUseDefaultOfFailFastWhenSpecifiedInRequest()
     {