Skip to content

Commit

Permalink
[MSHARED-1019] Allow pass raw cli option to Maven process
Browse files Browse the repository at this point in the history
  • Loading branch information
slawekjaranowski committed Mar 1, 2022
1 parent 71a1344 commit 89ee4ed
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 10 deletions.
Expand Up @@ -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.
*
Expand Down Expand Up @@ -113,6 +116,8 @@ public class DefaultInvocationRequest

private boolean noTransferProgress;

private List<String> args = new ArrayList<>();

/**
* <p>getBaseDirectory.</p>
*
Expand Down Expand Up @@ -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<String> getArgs()
{
return args;
}

/** {@inheritDoc} */
public InvocationRequest setPomFileName( String pomFilename )
{
Expand Down
Expand Up @@ -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<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.
Expand Down Expand Up @@ -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
* <code>null</code>, this setting only affects the working directory for the Maven invocation.
Expand Down
Expand Up @@ -104,6 +104,8 @@ public Commandline build( InvocationRequest request )

setThreads( request, cli );

setArgs( request, cli );

return cli;
}

Expand Down Expand Up @@ -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 )
Expand Down
2 changes: 2 additions & 0 deletions src/site/apt/index.apt.vm
Expand Up @@ -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

[]

[]
Expand Down
Expand Up @@ -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;
Expand All @@ -63,7 +64,7 @@ public void setUp() throws IOException
Properties p = new Properties( sysProps );

System.setProperties( p );

lrd = temporaryFolder.newFile();

}
Expand All @@ -74,7 +75,7 @@ public void tearDown()
System.setProperties( sysProps );
}


@Test
public void testShouldFailToSetLocalRepoLocationGloballyWhenItIsAFile()
{
Expand Down Expand Up @@ -176,7 +177,7 @@ public void testRequestProvidedWorkingDirectoryShouldOverrideGlobal()
InvocationRequest req = newRequest();
req.setBaseDirectory( wd );

mclb.setupBaseDirectory( req);
mclb.setupBaseDirectory( req );

assertEquals( mclb.getBaseDirectory(), wd.getCanonicalFile() );
}
Expand Down Expand Up @@ -337,23 +338,23 @@ public void testShouldSetQuietFlagFromRequest()

mclb.setFlags( newRequest().setQuiet( true ), cli );

assertArgumentsPresent( cli, Collections.singleton( "-q" ));
assertArgumentsPresent( cli, Collections.singleton( "-q" ) );
}

@Test
public void testShouldSetNonRecursiveFlagsFromRequest()
{
mclb.setFlags( newRequest().setRecursive( false ), cli );

assertArgumentsPresent( cli, Collections.singleton( "-N" ));
assertArgumentsPresent( cli, Collections.singleton( "-N" ) );
}

@Test
public void testShouldSetShowVersionFlagsFromRequest()
{
mclb.setFlags( newRequest().setShowVersion( true ), cli );

assertArgumentsPresent( cli, Collections.singleton( "-V" ));
assertArgumentsPresent( cli, Collections.singleton( "-V" ) );
}

@Test
Expand Down Expand Up @@ -389,7 +390,7 @@ public void testProjectsAndAlsoMake()
{

mclb.setReactorBehavior( newRequest().setProjects( Collections.singletonList( "proj1" ) ).setAlsoMake( true ),
cli );
cli );

assertArgumentsPresentInOrder( cli, "-pl", "proj1", "-am" );
}
Expand Down Expand Up @@ -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()
{
Expand All @@ -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();

Expand Down Expand Up @@ -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." );
Expand Down

0 comments on commit 89ee4ed

Please sign in to comment.