Skip to content

Commit 4476026

Browse files
Giovdsmthmulders
authored andcommitted
[MNG-7217] Update Commons CLI to version 1.5.0
Closes #605
1 parent c229278 commit 4476026

File tree

5 files changed

+85
-90
lines changed

5 files changed

+85
-90
lines changed

maven-embedder/src/main/java/org/apache/maven/cli/CLIManager.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
import org.apache.commons.cli.CommandLine;
2626
import org.apache.commons.cli.CommandLineParser;
27-
import org.apache.commons.cli.GnuParser;
27+
import org.apache.commons.cli.DefaultParser;
2828
import org.apache.commons.cli.HelpFormatter;
2929
import org.apache.commons.cli.Option;
3030
import org.apache.commons.cli.Options;
@@ -123,7 +123,7 @@ public CLIManager()
123123
options = new Options();
124124
options.addOption( Option.builder( Character.toString( HELP ) ).longOpt( "help" ).desc( "Display help information" ).build() );
125125
options.addOption( Option.builder( Character.toString( ALTERNATE_POM_FILE ) ).longOpt( "file" ).hasArg().desc( "Force the use of an alternate POM file (or directory with pom.xml)" ).build() );
126-
options.addOption( Option.builder( Character.toString( SET_SYSTEM_PROPERTY ) ).hasArg().desc( "Define a system property" ).build() );
126+
options.addOption( Option.builder( Character.toString( SET_SYSTEM_PROPERTY ) ).numberOfArgs( 2 ).valueSeparator( '=' ).desc( "Define a system property" ).build() );
127127
options.addOption( Option.builder( Character.toString( OFFLINE ) ).longOpt( "offline" ).desc( "Work offline" ).build() );
128128
options.addOption( Option.builder( Character.toString( VERSION ) ).longOpt( "version" ).desc( "Display version information" ).build() );
129129
options.addOption( Option.builder( Character.toString( QUIET ) ).longOpt( "quiet" ).desc( "Quiet output - only show errors" ).build() );
@@ -169,7 +169,7 @@ public CommandLine parse( String[] args )
169169
// We need to eat any quotes surrounding arguments...
170170
String[] cleanArgs = CleanArgument.cleanArgs( args );
171171

172-
CommandLineParser parser = new GnuParser();
172+
CommandLineParser parser = new DefaultParser();
173173

174174
return parser.parse( options, cleanArgs );
175175
}

maven-embedder/src/main/java/org/apache/maven/cli/MavenCli.java

Lines changed: 6 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1718,18 +1718,11 @@ static void populateProperties( CommandLine commandLine, Properties systemProper
17181718
// are most dominant.
17191719
// ----------------------------------------------------------------------
17201720

1721-
if ( commandLine.hasOption( CLIManager.SET_SYSTEM_PROPERTY ) )
1722-
{
1723-
String[] defStrs = commandLine.getOptionValues( CLIManager.SET_SYSTEM_PROPERTY );
1724-
1725-
if ( defStrs != null )
1726-
{
1727-
for ( String defStr : defStrs )
1728-
{
1729-
setCliProperty( defStr, userProperties );
1730-
}
1731-
}
1732-
}
1721+
final Properties userSpecifiedProperties = commandLine.getOptionProperties(
1722+
String.valueOf( CLIManager.SET_SYSTEM_PROPERTY ) );
1723+
userSpecifiedProperties.forEach(
1724+
( prop, value ) -> setCliProperty( (String) prop, (String) value, userProperties )
1725+
);
17331726

17341727
SystemProperties.addSystemProperties( systemProperties );
17351728

@@ -1747,27 +1740,8 @@ static void populateProperties( CommandLine commandLine, Properties systemProper
17471740
systemProperties.setProperty( "maven.build.version", mavenBuildVersion );
17481741
}
17491742

1750-
private static void setCliProperty( String property, Properties properties )
1743+
private static void setCliProperty( String name, String value, Properties properties )
17511744
{
1752-
String name;
1753-
1754-
String value;
1755-
1756-
int i = property.indexOf( '=' );
1757-
1758-
if ( i <= 0 )
1759-
{
1760-
name = property.trim();
1761-
1762-
value = "true";
1763-
}
1764-
else
1765-
{
1766-
name = property.substring( 0, i ).trim();
1767-
1768-
value = property.substring( i + 1 );
1769-
}
1770-
17711745
properties.setProperty( name, value );
17721746

17731747
// ----------------------------------------------------------------------

maven-embedder/src/test/java/org/apache/maven/cli/CLIManagerTest.java

Lines changed: 0 additions & 50 deletions
This file was deleted.

maven-embedder/src/test/java/org/apache/maven/cli/MavenCliTest.java

Lines changed: 75 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@
4545
import java.util.List;
4646

4747
import org.apache.commons.cli.CommandLine;
48-
import org.apache.commons.cli.GnuParser;
48+
import org.apache.commons.cli.CommandLineParser;
49+
import org.apache.commons.cli.DefaultParser;
4950
import org.apache.commons.cli.Option;
5051
import org.apache.commons.cli.Options;
5152
import org.apache.commons.cli.ParseException;
52-
import org.apache.commons.cli.Parser;
5353
import org.apache.maven.Maven;
5454
import org.apache.maven.eventspy.internal.EventSpyDispatcher;
5555
import org.apache.maven.execution.MavenExecutionRequest;
@@ -96,7 +96,7 @@ public void tearDown()
9696
@Test
9797
public void testPerformProfileActivation() throws ParseException
9898
{
99-
final Parser parser = new GnuParser();
99+
final CommandLineParser parser = new DefaultParser();
100100

101101
final Options options = new Options();
102102
options.addOption( Option.builder( Character.toString( CLIManager.ACTIVATE_PROFILES ) ).hasArg().build() );
@@ -122,7 +122,7 @@ public void testPerformProfileActivation() throws ParseException
122122
@Test
123123
public void testDetermineProjectActivation() throws ParseException
124124
{
125-
final Parser parser = new GnuParser();
125+
final CommandLineParser parser = new DefaultParser();
126126

127127
final Options options = new Options();
128128
options.addOption( Option.builder( CLIManager.PROJECT_LIST ).hasArg().build() );
@@ -492,6 +492,77 @@ public void testVersionStringWithoutAnsi() throws Exception
492492
assertEquals( MessageUtils.stripAnsiCodes( versionOut ), versionOut );
493493
}
494494

495+
@Test
496+
public void populatePropertiesCanContainEqualsSign() throws Exception
497+
{
498+
// Arrange
499+
CliRequest request = new CliRequest( new String[] { "-Dw=x=y", "validate" }, null );
500+
501+
// Act
502+
cli.cli( request );
503+
cli.properties( request );
504+
505+
// Assert
506+
assertThat( request.getUserProperties().getProperty( "w" ), is( "x=y" ) );
507+
}
508+
509+
@Test
510+
public void populatePropertiesSpace() throws Exception
511+
{
512+
// Arrange
513+
CliRequest request = new CliRequest( new String[] { "-D", "z=2", "validate" }, null );
514+
515+
// Act
516+
cli.cli( request );
517+
cli.properties( request );
518+
519+
// Assert
520+
assertThat( request.getUserProperties().getProperty( "z" ), is( "2" ) );
521+
}
522+
523+
@Test
524+
public void populatePropertiesShorthand() throws Exception
525+
{
526+
// Arrange
527+
CliRequest request = new CliRequest( new String[] { "-Dx", "validate" }, null );
528+
529+
// Act
530+
cli.cli( request );
531+
cli.properties( request );
532+
533+
// Assert
534+
assertThat( request.getUserProperties().getProperty( "x" ), is( "true" ) );
535+
}
536+
537+
@Test
538+
public void populatePropertiesMultiple() throws Exception
539+
{
540+
// Arrange
541+
CliRequest request = new CliRequest( new String[] { "-Dx=1", "-Dy", "validate" }, null );
542+
543+
// Act
544+
cli.cli( request );
545+
cli.properties( request );
546+
547+
// Assert
548+
assertThat( request.getUserProperties().getProperty( "x" ), is( "1" ) );
549+
assertThat( request.getUserProperties().getProperty( "y" ), is( "true" ) );
550+
}
551+
552+
@Test
553+
public void populatePropertiesOverwrite() throws Exception
554+
{
555+
// Arrange
556+
CliRequest request = new CliRequest( new String[] { "-Dx", "-Dx=false", "validate" }, null );
557+
558+
// Act
559+
cli.cli( request );
560+
cli.properties( request );
561+
562+
// Assert
563+
assertThat( request.getUserProperties().getProperty( "x" ), is( "false" ) );
564+
}
565+
495566
private MavenProject createMavenProject( String groupId, String artifactId )
496567
{
497568
MavenProject project = new MavenProject();

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ under the License.
4949
<maven.compiler.source>1.8</maven.compiler.source>
5050
<maven.compiler.target>1.8</maven.compiler.target>
5151
<classWorldsVersion>2.6.0</classWorldsVersion>
52-
<commonsCliVersion>1.4</commonsCliVersion>
52+
<commonsCliVersion>1.5.0</commonsCliVersion>
5353
<commonsLangVersion>3.12.0</commonsLangVersion>
5454
<junitVersion>5.8.1</junitVersion>
5555
<mockitoVersion>3.2.0</mockitoVersion>

0 commit comments

Comments
 (0)