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/03/01 14:43:01 UTC

[maven-invoker] branch master updated: [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 master
in repository https://gitbox.apache.org/repos/asf/maven-invoker.git


The following commit(s) were added to refs/heads/master by this push:
     new 89ee4ed  [MSHARED-1019] Allow pass raw cli option to Maven process
89ee4ed is described below

commit 89ee4ede957698869a138003718acc632c334a3a
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   | 21 ++++++++++++
 .../maven/shared/invoker/InvocationRequest.java    | 18 ++++++++++
 .../shared/invoker/MavenCommandLineBuilder.java    | 10 ++++++
 src/site/apt/index.apt.vm                          |  2 ++
 .../invoker/MavenCommandLineBuilderTest.java       | 38 ++++++++++++++++------
 5 files changed, 79 insertions(+), 10 deletions(-)

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..0866ce3 100644
--- a/src/main/java/org/apache/maven/shared/invoker/DefaultInvocationRequest.java
+++ b/src/main/java/org/apache/maven/shared/invoker/DefaultInvocationRequest.java
@@ -21,12 +21,15 @@ package org.apache.maven.shared.invoker;
 
 import java.io.File;
 import java.io.InputStream;
+import java.util.ArrayList;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Properties;
 
+import org.apache.maven.shared.utils.StringUtils;
+
 /**
  * Specifies the parameters used to control a Maven invocation.
  *
@@ -113,6 +116,8 @@ public class DefaultInvocationRequest
 
     private boolean noTransferProgress;
 
+    private List<String> args = new ArrayList<>();
+
     /**
      * <p>getBaseDirectory.</p>
      *
@@ -499,6 +504,22 @@ public class DefaultInvocationRequest
         return pomFilename;
     }
 
+
+    @Override
+    public InvocationRequest addArg( String arg )
+    {
+        if ( StringUtils.isNotBlank( arg ) )
+        {
+            args.add( arg );
+        }
+        return this;
+    }
+
+    public List<String> getArgs()
+    {
+        return args;
+    }
+
     /** {@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..794c07e 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,14 @@ public interface InvocationRequest
     String getPomFileName();
 
     /**
+     * List of raw line arguments which will be passed to cli.
+     *
+     * @return a list of cli arguments
+     * @since 3.2.0
+     */
+    List<String> getArgs();
+
+    /**
      * 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 +535,16 @@ public interface InvocationRequest
     InvocationRequest setPomFileName( String pomFilename );
 
     /**
+     * Add a raw  argument to Maven  cli command at the end of other arguments.
+     * Can be called multiple time in order to add many arguments.
+     *
+     * @param arg a raw Maven arg line
+     * @return This invocation request.
+     * @since 3.2.0
+     */
+    InvocationRequest addArg( String arg );
+
+    /**
      * 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..7c7e985 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 );
 
+        setArgs( request, cli );
+
         return cli;
     }
 
@@ -552,6 +554,14 @@ public class MavenCommandLineBuilder
 
     }
 
+    private void setArgs( InvocationRequest request, Commandline cli )
+    {
+        for ( String arg : request.getArgs() )
+        {
+            cli.createArg().setValue( arg );
+        }
+    }
+
     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..20e747c 100644
--- a/src/test/java/org/apache/maven/shared/invoker/MavenCommandLineBuilderTest.java
+++ b/src/test/java/org/apache/maven/shared/invoker/MavenCommandLineBuilderTest.java
@@ -40,6 +40,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;
@@ -63,7 +64,7 @@ public class MavenCommandLineBuilderTest
         Properties p = new Properties( sysProps );
 
         System.setProperties( p );
-        
+
         lrd = temporaryFolder.newFile();
 
     }
@@ -74,7 +75,7 @@ public class MavenCommandLineBuilderTest
         System.setProperties( sysProps );
     }
 
-    
+
     @Test
     public void testShouldFailToSetLocalRepoLocationGloballyWhenItIsAFile()
     {
@@ -176,7 +177,7 @@ public class MavenCommandLineBuilderTest
         InvocationRequest req = newRequest();
         req.setBaseDirectory( wd );
 
-        mclb.setupBaseDirectory( req);
+        mclb.setupBaseDirectory( req );
 
         assertEquals( mclb.getBaseDirectory(), wd.getCanonicalFile() );
     }
@@ -337,7 +338,7 @@ public class MavenCommandLineBuilderTest
 
         mclb.setFlags( newRequest().setQuiet( true ), cli );
 
-        assertArgumentsPresent( cli, Collections.singleton( "-q" ));
+        assertArgumentsPresent( cli, Collections.singleton( "-q" ) );
     }
 
     @Test
@@ -345,7 +346,7 @@ public class MavenCommandLineBuilderTest
     {
         mclb.setFlags( newRequest().setRecursive( false ), cli );
 
-        assertArgumentsPresent( cli, Collections.singleton( "-N" ));
+        assertArgumentsPresent( cli, Collections.singleton( "-N" ) );
     }
 
     @Test
@@ -353,7 +354,7 @@ public class MavenCommandLineBuilderTest
     {
         mclb.setFlags( newRequest().setShowVersion( true ), cli );
 
-        assertArgumentsPresent( cli, Collections.singleton( "-V" ));
+        assertArgumentsPresent( cli, Collections.singleton( "-V" ) );
     }
 
     @Test
@@ -389,7 +390,7 @@ public class MavenCommandLineBuilderTest
     {
 
         mclb.setReactorBehavior( newRequest().setProjects( Collections.singletonList( "proj1" ) ).setAlsoMake( true ),
-                                cli );
+                                 cli );
 
         assertArgumentsPresentInOrder( cli, "-pl", "proj1", "-am" );
     }
@@ -471,6 +472,23 @@ public class MavenCommandLineBuilderTest
         assertArgumentsPresent( cli, Collections.singleton( "-fn" ) );
     }
 
+
+    @Test
+    public void testShouldAddArg() throws CommandLineConfigurationException
+    {
+        InvocationRequest request = newRequest()
+            .addArg( "arg1" )
+            .addArg( "arg2" )
+            .setQuiet( true )
+            .setBuilder( "bId" );
+
+        Commandline commandline = mclb.build( request );
+
+        String[] arguments = commandline.getArguments();
+
+        assertArrayEquals( Arrays.asList( "-b", "bId", "-q", "arg1",  "arg2" ).toArray(), arguments );
+    }
+
     @Test
     public void testShouldUseDefaultOfFailFastWhenSpecifiedInRequest()
     {
@@ -489,14 +507,14 @@ public class MavenCommandLineBuilderTest
     public void testShouldSetNoTransferProgressFlagFromRequest()
     {
         mclb.setFlags( newRequest().setNoTransferProgress( true ), cli );
-        assertArgumentsPresent( cli, Collections.singleton( "-ntp" ));
+        assertArgumentsPresent( cli, Collections.singleton( "-ntp" ) );
     }
 
     @Test
     public void testShouldSpecifyFileOptionUsingNonStandardPomFileLocation()
         throws Exception
     {
-        File projectDir =  temporaryFolder.newFolder( "invoker-tests", "file-option-nonstd-pom-file-location" );
+        File projectDir = temporaryFolder.newFolder( "invoker-tests", "file-option-nonstd-pom-file-location" );
 
         File pomFile = createDummyFile( projectDir, "non-standard-pom.xml" ).getCanonicalFile();
 
@@ -946,7 +964,7 @@ public class MavenCommandLineBuilderTest
         throws IOException
     {
         File dummyFile = new File( directory, filename );
-        
+
         try ( FileWriter writer = new FileWriter( dummyFile ) )
         {
             writer.write( "This is a dummy file." );