[MNG-7217] Update Commons CLI to version 1.5.0

Closes #605
This commit is contained in:
Giovanni van der Schelde 2021-11-03 12:24:38 +01:00 committed by Maarten Mulders
parent c229278477
commit 4476026c52
5 changed files with 85 additions and 90 deletions

View File

@ -24,7 +24,7 @@
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.GnuParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
@ -123,7 +123,7 @@ public CLIManager()
options = new Options();
options.addOption( Option.builder( Character.toString( HELP ) ).longOpt( "help" ).desc( "Display help information" ).build() );
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() );
options.addOption( Option.builder( Character.toString( SET_SYSTEM_PROPERTY ) ).hasArg().desc( "Define a system property" ).build() );
options.addOption( Option.builder( Character.toString( SET_SYSTEM_PROPERTY ) ).numberOfArgs( 2 ).valueSeparator( '=' ).desc( "Define a system property" ).build() );
options.addOption( Option.builder( Character.toString( OFFLINE ) ).longOpt( "offline" ).desc( "Work offline" ).build() );
options.addOption( Option.builder( Character.toString( VERSION ) ).longOpt( "version" ).desc( "Display version information" ).build() );
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 )
// We need to eat any quotes surrounding arguments...
String[] cleanArgs = CleanArgument.cleanArgs( args );
CommandLineParser parser = new GnuParser();
CommandLineParser parser = new DefaultParser();
return parser.parse( options, cleanArgs );
}

View File

@ -1718,18 +1718,11 @@ static void populateProperties( CommandLine commandLine, Properties systemProper
// are most dominant.
// ----------------------------------------------------------------------
if ( commandLine.hasOption( CLIManager.SET_SYSTEM_PROPERTY ) )
{
String[] defStrs = commandLine.getOptionValues( CLIManager.SET_SYSTEM_PROPERTY );
if ( defStrs != null )
{
for ( String defStr : defStrs )
{
setCliProperty( defStr, userProperties );
}
}
}
final Properties userSpecifiedProperties = commandLine.getOptionProperties(
String.valueOf( CLIManager.SET_SYSTEM_PROPERTY ) );
userSpecifiedProperties.forEach(
( prop, value ) -> setCliProperty( (String) prop, (String) value, userProperties )
);
SystemProperties.addSystemProperties( systemProperties );
@ -1747,27 +1740,8 @@ static void populateProperties( CommandLine commandLine, Properties systemProper
systemProperties.setProperty( "maven.build.version", mavenBuildVersion );
}
private static void setCliProperty( String property, Properties properties )
private static void setCliProperty( String name, String value, Properties properties )
{
String name;
String value;
int i = property.indexOf( '=' );
if ( i <= 0 )
{
name = property.trim();
value = "true";
}
else
{
name = property.substring( 0, i ).trim();
value = property.substring( i + 1 );
}
properties.setProperty( name, value );
// ----------------------------------------------------------------------

View File

@ -1,50 +0,0 @@
package org.apache.maven.cli;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.apache.commons.cli.CommandLine;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class CLIManagerTest
{
private CLIManager cliManager;
@BeforeEach
public void setup()
{
cliManager = new CLIManager();
}
@Test
public void spacedOptions()
throws Exception
{
CommandLine cmdLine = cliManager.parse( "-X -Dx=1 -D y=2 test".split( " " ) );
assertTrue( cmdLine.hasOption( CLIManager.VERBOSE ) );
assertThat( cmdLine.getOptionValues( CLIManager.SET_SYSTEM_PROPERTY )[0], is( "x=1" ) );
assertThat( cmdLine.getOptionValues( CLIManager.SET_SYSTEM_PROPERTY )[1], is( "y=2" ) );
}
}

View File

@ -45,11 +45,11 @@
import java.util.List;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.GnuParser;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.cli.Parser;
import org.apache.maven.Maven;
import org.apache.maven.eventspy.internal.EventSpyDispatcher;
import org.apache.maven.execution.MavenExecutionRequest;
@ -96,7 +96,7 @@ public void tearDown()
@Test
public void testPerformProfileActivation() throws ParseException
{
final Parser parser = new GnuParser();
final CommandLineParser parser = new DefaultParser();
final Options options = new Options();
options.addOption( Option.builder( Character.toString( CLIManager.ACTIVATE_PROFILES ) ).hasArg().build() );
@ -122,7 +122,7 @@ public void testPerformProfileActivation() throws ParseException
@Test
public void testDetermineProjectActivation() throws ParseException
{
final Parser parser = new GnuParser();
final CommandLineParser parser = new DefaultParser();
final Options options = new Options();
options.addOption( Option.builder( CLIManager.PROJECT_LIST ).hasArg().build() );
@ -492,6 +492,77 @@ public void testVersionStringWithoutAnsi() throws Exception
assertEquals( MessageUtils.stripAnsiCodes( versionOut ), versionOut );
}
@Test
public void populatePropertiesCanContainEqualsSign() throws Exception
{
// Arrange
CliRequest request = new CliRequest( new String[] { "-Dw=x=y", "validate" }, null );
// Act
cli.cli( request );
cli.properties( request );
// Assert
assertThat( request.getUserProperties().getProperty( "w" ), is( "x=y" ) );
}
@Test
public void populatePropertiesSpace() throws Exception
{
// Arrange
CliRequest request = new CliRequest( new String[] { "-D", "z=2", "validate" }, null );
// Act
cli.cli( request );
cli.properties( request );
// Assert
assertThat( request.getUserProperties().getProperty( "z" ), is( "2" ) );
}
@Test
public void populatePropertiesShorthand() throws Exception
{
// Arrange
CliRequest request = new CliRequest( new String[] { "-Dx", "validate" }, null );
// Act
cli.cli( request );
cli.properties( request );
// Assert
assertThat( request.getUserProperties().getProperty( "x" ), is( "true" ) );
}
@Test
public void populatePropertiesMultiple() throws Exception
{
// Arrange
CliRequest request = new CliRequest( new String[] { "-Dx=1", "-Dy", "validate" }, null );
// Act
cli.cli( request );
cli.properties( request );
// Assert
assertThat( request.getUserProperties().getProperty( "x" ), is( "1" ) );
assertThat( request.getUserProperties().getProperty( "y" ), is( "true" ) );
}
@Test
public void populatePropertiesOverwrite() throws Exception
{
// Arrange
CliRequest request = new CliRequest( new String[] { "-Dx", "-Dx=false", "validate" }, null );
// Act
cli.cli( request );
cli.properties( request );
// Assert
assertThat( request.getUserProperties().getProperty( "x" ), is( "false" ) );
}
private MavenProject createMavenProject( String groupId, String artifactId )
{
MavenProject project = new MavenProject();

View File

@ -49,7 +49,7 @@ under the License.
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<classWorldsVersion>2.6.0</classWorldsVersion>
<commonsCliVersion>1.4</commonsCliVersion>
<commonsCliVersion>1.5.0</commonsCliVersion>
<commonsLangVersion>3.12.0</commonsLangVersion>
<junitVersion>5.8.1</junitVersion>
<mockitoVersion>3.2.0</mockitoVersion>