From 89ee4ede957698869a138003718acc632c334a3a Mon Sep 17 00:00:00 2001 From: Slawomir Jaranowski Date: Sat, 26 Feb 2022 17:25:51 +0100 Subject: [PATCH] [MSHARED-1019] Allow pass raw cli option to Maven process --- .../invoker/DefaultInvocationRequest.java | 21 ++++++++++ .../shared/invoker/InvocationRequest.java | 18 +++++++++ .../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 @@ 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 args = new ArrayList<>(); + /** *

getBaseDirectory.

* @@ -499,6 +504,22 @@ public String getPomFileName() return pomFilename; } + + @Override + public InvocationRequest addArg( String arg ) + { + if ( StringUtils.isNotBlank( arg ) ) + { + args.add( arg ); + } + return this; + } + + public List 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 @@ -190,6 +190,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 getArgs(); + /** * Gets the path to the base directory of the POM for the Maven invocation. If {@link #getPomFile()} does not return * null, this setting only affects the working directory for the Maven invocation. @@ -526,6 +534,16 @@ enum CheckSumPolicy */ 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 * null, 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 Commandline build( InvocationRequest request ) setThreads( request, cli ); + setArgs( request, cli ); + return cli; } @@ -552,6 +554,14 @@ protected void setThreads( InvocationRequest request, Commandline cli ) } + 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 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 void setUp() throws IOException Properties p = new Properties( sysProps ); System.setProperties( p ); - + lrd = temporaryFolder.newFile(); } @@ -74,7 +75,7 @@ public void tearDown() System.setProperties( sysProps ); } - + @Test public void testShouldFailToSetLocalRepoLocationGloballyWhenItIsAFile() { @@ -176,7 +177,7 @@ public void testRequestProvidedWorkingDirectoryShouldOverrideGlobal() InvocationRequest req = newRequest(); req.setBaseDirectory( wd ); - mclb.setupBaseDirectory( req); + mclb.setupBaseDirectory( req ); assertEquals( mclb.getBaseDirectory(), wd.getCanonicalFile() ); } @@ -337,7 +338,7 @@ public void testShouldSetQuietFlagFromRequest() mclb.setFlags( newRequest().setQuiet( true ), cli ); - assertArgumentsPresent( cli, Collections.singleton( "-q" )); + assertArgumentsPresent( cli, Collections.singleton( "-q" ) ); } @Test @@ -345,7 +346,7 @@ public void testShouldSetNonRecursiveFlagsFromRequest() { mclb.setFlags( newRequest().setRecursive( false ), cli ); - assertArgumentsPresent( cli, Collections.singleton( "-N" )); + assertArgumentsPresent( cli, Collections.singleton( "-N" ) ); } @Test @@ -353,7 +354,7 @@ public void testShouldSetShowVersionFlagsFromRequest() { mclb.setFlags( newRequest().setShowVersion( true ), cli ); - assertArgumentsPresent( cli, Collections.singleton( "-V" )); + assertArgumentsPresent( cli, Collections.singleton( "-V" ) ); } @Test @@ -389,7 +390,7 @@ public void testProjectsAndAlsoMake() { mclb.setReactorBehavior( newRequest().setProjects( Collections.singletonList( "proj1" ) ).setAlsoMake( true ), - cli ); + cli ); assertArgumentsPresentInOrder( cli, "-pl", "proj1", "-am" ); } @@ -471,6 +472,23 @@ public void testShouldSetFailNeverFlagFromRequest() 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 void testShouldUseDefaultOfFailFastWhenSpecifiedInRequest() 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 @@ private File createDummyFile( File directory, String filename ) throws IOException { File dummyFile = new File( directory, filename ); - + try ( FileWriter writer = new FileWriter( dummyFile ) ) { writer.write( "This is a dummy file." );