You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by mt...@apache.org on 2022/01/08 18:59:53 UTC

[maven] 01/01: [MNG-7217] Update Commons CLI to version 1.5.0

This is an automated email from the ASF dual-hosted git repository.

mthmulders pushed a commit to branch MNG-7217
in repository https://gitbox.apache.org/repos/asf/maven.git

commit 4476026c525a22900c6c5e4f0de6f7362cb201f2
Author: Giovanni van der Schelde <Gi...@infosupport.com>
AuthorDate: Wed Nov 3 12:24:38 2021 +0100

    [MNG-7217] Update Commons CLI to version 1.5.0
    
    Closes #605
---
 .../main/java/org/apache/maven/cli/CLIManager.java |  6 +-
 .../main/java/org/apache/maven/cli/MavenCli.java   | 38 ++---------
 .../java/org/apache/maven/cli/CLIManagerTest.java  | 50 --------------
 .../java/org/apache/maven/cli/MavenCliTest.java    | 79 ++++++++++++++++++++--
 pom.xml                                            |  2 +-
 5 files changed, 85 insertions(+), 90 deletions(-)

diff --git a/maven-embedder/src/main/java/org/apache/maven/cli/CLIManager.java b/maven-embedder/src/main/java/org/apache/maven/cli/CLIManager.java
index 49bf9db..45e322f 100644
--- a/maven-embedder/src/main/java/org/apache/maven/cli/CLIManager.java
+++ b/maven-embedder/src/main/java/org/apache/maven/cli/CLIManager.java
@@ -24,7 +24,7 @@ import java.io.PrintWriter;
 
 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 class 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 class CLIManager
         // 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 );
     }
diff --git a/maven-embedder/src/main/java/org/apache/maven/cli/MavenCli.java b/maven-embedder/src/main/java/org/apache/maven/cli/MavenCli.java
index 89f3833..ffb9ea8 100644
--- a/maven-embedder/src/main/java/org/apache/maven/cli/MavenCli.java
+++ b/maven-embedder/src/main/java/org/apache/maven/cli/MavenCli.java
@@ -1718,18 +1718,11 @@ public class MavenCli
         // 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 @@ public class MavenCli
         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 );
 
         // ----------------------------------------------------------------------
diff --git a/maven-embedder/src/test/java/org/apache/maven/cli/CLIManagerTest.java b/maven-embedder/src/test/java/org/apache/maven/cli/CLIManagerTest.java
deleted file mode 100644
index 68e4c22..0000000
--- a/maven-embedder/src/test/java/org/apache/maven/cli/CLIManagerTest.java
+++ /dev/null
@@ -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" ) );
-    }
-
-}
diff --git a/maven-embedder/src/test/java/org/apache/maven/cli/MavenCliTest.java b/maven-embedder/src/test/java/org/apache/maven/cli/MavenCliTest.java
index 4191e88..3d7e385 100644
--- a/maven-embedder/src/test/java/org/apache/maven/cli/MavenCliTest.java
+++ b/maven-embedder/src/test/java/org/apache/maven/cli/MavenCliTest.java
@@ -45,11 +45,11 @@ import java.util.Collections;
 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 class MavenCliTest
     @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 class MavenCliTest
     @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 class MavenCliTest
         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();
diff --git a/pom.xml b/pom.xml
index 5d8b410..194fce8 100644
--- a/pom.xml
+++ b/pom.xml
@@ -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>