Skip to content

Commit

Permalink
[MSHARED-1129] Prepare for replace CLI options with CLI args
Browse files Browse the repository at this point in the history
- add new methods
- deprecate old
  • Loading branch information
slawekjaranowski committed Sep 11, 2022
1 parent f2ff914 commit f25967c
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 25 deletions.
84 changes: 62 additions & 22 deletions src/main/java/org/apache/maven/shared/verifier/Verifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public class Verifier
{
private static final String LOG_FILENAME = "log.txt";

private static final String[] DEFAULT_CLI_OPTIONS = {"-e", "--batch-mode"};
private static final String[] DEFAULT_CLI_ARGUMENTS = {"-e", "--batch-mode"};

private String localRepo;

Expand All @@ -74,13 +74,13 @@ public class Verifier

private final ByteArrayOutputStream errStream = new ByteArrayOutputStream();

private final String[] defaultCliOptions;
private final String[] defaultCliArguments;

private PrintStream originalOut;

private PrintStream originalErr;

private List<String> cliOptions = new ArrayList<String>();
private List<String> cliArguments = new ArrayList<>();

private Properties systemProperties = new Properties();

Expand Down Expand Up @@ -143,40 +143,41 @@ public Verifier( String basedir, String settingsFile )
public Verifier( String basedir, String settingsFile, boolean debug )
throws VerificationException
{
this( basedir, settingsFile, debug, DEFAULT_CLI_OPTIONS );
this( basedir, settingsFile, debug, DEFAULT_CLI_ARGUMENTS );
}

public Verifier( String basedir, String settingsFile, boolean debug, String[] defaultCliOptions )
public Verifier( String basedir, String settingsFile, boolean debug, String[] defaultCliArguments )
throws VerificationException
{
this( basedir, settingsFile, debug, null, defaultCliOptions );
this( basedir, settingsFile, debug, null, defaultCliArguments );
}

public Verifier( String basedir, String settingsFile, boolean debug, boolean forkJvm )
throws VerificationException
{
this( basedir, settingsFile, debug, forkJvm, DEFAULT_CLI_OPTIONS );
this( basedir, settingsFile, debug, forkJvm, DEFAULT_CLI_ARGUMENTS );
}

public Verifier( String basedir, String settingsFile, boolean debug, boolean forkJvm, String[] defaultCliOptions )
public Verifier( String basedir, String settingsFile, boolean debug, boolean forkJvm, String[] defaultCliArguments )
throws VerificationException
{
this( basedir, settingsFile, debug, forkJvm, defaultCliOptions, null );
this( basedir, settingsFile, debug, forkJvm, defaultCliArguments, null );
}

public Verifier( String basedir, String settingsFile, boolean debug, String mavenHome )
throws VerificationException
{
this( basedir, settingsFile, debug, null, DEFAULT_CLI_OPTIONS, mavenHome );
this( basedir, settingsFile, debug, null, DEFAULT_CLI_ARGUMENTS, mavenHome );
}

public Verifier( String basedir, String settingsFile, boolean debug, String mavenHome, String[] defaultCliOptions )
public Verifier( String basedir, String settingsFile, boolean debug, String mavenHome,
String[] defaultCliArguments )
throws VerificationException
{
this( basedir, settingsFile, debug, null, defaultCliOptions, mavenHome );
this( basedir, settingsFile, debug, null, defaultCliArguments, mavenHome );
}

private Verifier( String basedir, String settingsFile, boolean debug, Boolean forkJvm, String[] defaultCliOptions,
private Verifier( String basedir, String settingsFile, boolean debug, Boolean forkJvm, String[] defaultCliArguments,
String mavenHome ) throws VerificationException
{
this.basedir = basedir;
Expand Down Expand Up @@ -210,7 +211,7 @@ private Verifier( String basedir, String settingsFile, boolean debug, Boolean fo
forkMode = "auto";
}

this.defaultCliOptions = defaultCliOptions == null ? new String[0] : defaultCliOptions.clone();
this.defaultCliArguments = defaultCliArguments == null ? new String[0] : defaultCliArguments.clone();
}

private static String getDefaultMavenHome()
Expand Down Expand Up @@ -1237,12 +1238,12 @@ public void executeGoals( List<String> goals, Map<String, String> envVars )

File logFile = new File( getBasedir(), getLogFileName() );

for ( String cliOption : cliOptions )
for ( String cliArgument : cliArguments )
{
args.add( cliOption.replace( "${basedir}", getBasedir() ) );
args.add( cliArgument.replace( "${basedir}", getBasedir() ) );
}

Collections.addAll( args, defaultCliOptions );
Collections.addAll( args, defaultCliArguments );

if ( this.mavenDebug )
{
Expand All @@ -1266,10 +1267,10 @@ public void executeGoals( List<String> goals, Map<String, String> envVars )

try
{
String[] cliArgs = args.toArray( new String[args.size()] );

MavenLauncher launcher = getMavenLauncher( envVars );

String[] cliArgs = args.toArray( new String[0] );
ret = launcher.run( cliArgs, systemProperties, getBasedir(), logFile );
}
catch ( LauncherException e )
Expand Down Expand Up @@ -1566,38 +1567,77 @@ public void reset()
}
}

/**
* @deprecated will be removed without replacement,
* for arguments adding please use {@link #addCliArgument(String)}, {@link #addCliArguments(String...)}
*/
@Deprecated
public List<String> getCliOptions()
{
return cliOptions;
return cliArguments;
}

/**
* @deprecated will be removed
*/
@Deprecated
public void setCliOptions( List<String> cliOptions )
{
this.cliOptions = cliOptions;
this.cliArguments = cliOptions;
}

/**
* Add a command line argument, each argument must be set separately one by one.
* <p>
* <code>${basedir}</code> in argument will be replaced by value of {@link #getBasedir()} during execution.
* @param option an argument to add
* @deprecated please use {@link #addCliArgument(String)}
*/
@Deprecated
public void addCliOption( String option )
{
cliOptions.add( option );
addCliArgument( option );
}

/**
* Add a command line argument, each argument must be set separately one by one.
* <p>
* <code>${basedir}</code> in argument will be replaced by value of {@link #getBasedir()} during execution.
*
* @param cliArgument an argument to add
*/
public void addCliArgument( String cliArgument )
{
cliArguments.add( cliArgument );
}

/**
* Add a command line arguments, each argument must be set separately one by one.
* <p>
* <code>${basedir}</code> in argument will be replaced by value of {@link #getBasedir()} during execution.
*
* @param options an arguments list to add
* @deprecated
*/
@Deprecated
public void addCliOptions( String... options )
{
Collections.addAll( cliOptions, options );
addCliArguments( options );
}

/**
* Add a command line arguments, each argument must be set separately one by one.
* <p>
* <code>${basedir}</code> in argument will be replaced by value of {@link #getBasedir()} during execution.
*
* @param cliArguments an arguments list to add
*/
public void addCliArguments( String... cliArguments )
{
Collections.addAll( this.cliArguments, cliArguments );
}


public Properties getSystemProperties()
{
return systemProperties;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,18 +168,18 @@ void argumentShouldBePassedAsIs( String inputArgument, String expectedArgument )
{
TestVerifier verifier = new TestVerifier( "src/test/resources" );

verifier.addCliOption( inputArgument );
verifier.addCliArgument( inputArgument );
verifier.executeGoal( "test" );

assertThat( verifier.launcher.cliArgs, hasItemInArray( expectedArgument ) );
}

@Test
void cliOptionsShouldAddSeparateArguments() throws VerificationException
void addCliArgsShouldAddSeparateArguments() throws VerificationException
{
TestVerifier verifier = new TestVerifier( "src/test/resources" );

verifier.addCliOptions( "cliArg1", "cliArg2" );
verifier.addCliArguments( "cliArg1", "cliArg2" );
verifier.executeGoal( "test" );

assertThat( verifier.launcher.cliArgs, allOf(
Expand Down

0 comments on commit f25967c

Please sign in to comment.