You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by kh...@apache.org on 2016/09/24 15:19:51 UTC

[29/33] maven git commit: [MNG-6078] Can't overwrite properties which have been defined in .mvn/maven.config o Reversed the order of properties only to get the properties from command line at the end of the properties list which results in correct

[MNG-6078] Can't overwrite properties which have been defined in
.mvn/maven.config
 o Reversed the order of properties only to get the properties from
   command line at the end of the properties list which results
   in correct behaviour to be able to overwrite properties from
   command line for properties which have been defined in
   .mvn/maven.config file.


Project: http://git-wip-us.apache.org/repos/asf/maven/repo
Commit: http://git-wip-us.apache.org/repos/asf/maven/commit/9f2452ad
Tree: http://git-wip-us.apache.org/repos/asf/maven/tree/9f2452ad
Diff: http://git-wip-us.apache.org/repos/asf/maven/diff/9f2452ad

Branch: refs/heads/MNG-6056-feature-toggle
Commit: 9f2452adb6333d5f38f4cf3d652261726fa1f9e2
Parents: 4ba5779
Author: Karl Heinz Marbaise <kh...@apache.org>
Authored: Sun Aug 7 12:21:01 2016 +0200
Committer: Karl Heinz Marbaise <kh...@apache.org>
Committed: Sun Aug 7 13:07:28 2016 +0200

----------------------------------------------------------------------
 .../java/org/apache/maven/cli/MavenCli.java     | 12 ++++-
 .../java/org/apache/maven/cli/MavenCliTest.java | 53 ++++++++++++++++++--
 .../mavenConfigProperties/.mvn/maven.config     |  3 ++
 3 files changed, 62 insertions(+), 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/maven/blob/9f2452ad/maven-embedder/src/main/java/org/apache/maven/cli/MavenCli.java
----------------------------------------------------------------------
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 6b482aa..822f696 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
@@ -44,6 +44,7 @@ import java.util.StringTokenizer;
 import org.apache.commons.cli.CommandLine;
 import org.apache.commons.cli.ParseException;
 import org.apache.commons.cli.UnrecognizedOptionException;
+import org.apache.commons.lang3.ArrayUtils;
 import org.apache.maven.BuildAbort;
 import org.apache.maven.InternalErrorException;
 import org.apache.maven.Maven;
@@ -552,7 +553,9 @@ public class MavenCli
         }
     }
 
-    private void properties( CliRequest cliRequest )
+    //Needed to make this method package visible to make writing a unit test possible
+    //Maybe it's better to move some of those methods to separate class (SoC).
+    void properties( CliRequest cliRequest )
     {
         populateProperties( cliRequest.commandLine, cliRequest.systemProperties, cliRequest.userProperties );
     }
@@ -1668,9 +1671,14 @@ public class MavenCli
         if ( commandLine.hasOption( CLIManager.SET_SYSTEM_PROPERTY ) )
         {
             String[] defStrs = commandLine.getOptionValues( CLIManager.SET_SYSTEM_PROPERTY );
-
+            
             if ( defStrs != null )
             {
+                //The following is needed to get precedence
+                //of properties which are defined on command line
+                //over properties defined in the .mvn/maven.config. 
+                ArrayUtils.reverse( defStrs );
+                
                 for ( String defStr : defStrs )
                 {
                     setCliProperty( defStr, userProperties );

http://git-wip-us.apache.org/repos/asf/maven/blob/9f2452ad/maven-embedder/src/test/java/org/apache/maven/cli/MavenCliTest.java
----------------------------------------------------------------------
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 c8d75b1..d926624 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
@@ -21,10 +21,10 @@ package org.apache.maven.cli;
 
 import java.io.File;
 
-import junit.framework.TestCase;
-
 import org.apache.commons.cli.ParseException;
 
+import junit.framework.TestCase;
+
 public class MavenCliTest
     extends TestCase
 {
@@ -81,8 +81,8 @@ public class MavenCliTest
         // read .mvn/maven.config
         cli.initialize( request );
         cli.cli( request );
-        assertEquals( "multithreaded", request.commandLine.getOptionValue( "builder" ) );
-        assertEquals( "8", request.commandLine.getOptionValue( "threads" ) );
+        assertEquals( "multithreaded", request.commandLine.getOptionValue( CLIManager.BUILDER ) );
+        assertEquals( "8", request.commandLine.getOptionValue( CLIManager.THREADS ) );
 
         // override from command line
         request = new CliRequest( new String[] { "--builder", "foobar" }, null );
@@ -107,4 +107,49 @@ public class MavenCliTest
 
         }
     }
+    
+    /**
+     * Read .mvn/maven.config with the following definitions:
+     * <pre>
+     *   -T 3
+     *   -Drevision=1.3.0
+     * </pre>
+     * and check if the {@code -T 3} option can be overwritten via command line
+     * argument.
+     * @throws Exception in case of failure.
+     */
+    public void testMVNConfigurationThreadCanBeOverwrittenViaCommandLine() throws Exception {
+        System.setProperty( MavenCli.MULTIMODULE_PROJECT_DIRECTORY, new File( "src/test/projects/mavenConfigProperties" ).getCanonicalPath() );
+        CliRequest request = new CliRequest( new String[]{ "-T", "5" }, null );
+
+        cli.initialize( request );
+        // read .mvn/maven.config
+        cli.cli( request );
+        
+        assertEquals( "5", request.commandLine.getOptionValue( CLIManager.THREADS ) );
+    }
+    
+    /**
+     * Read .mvn/maven.config with the following definitions:
+     * <pre>
+     *   -T 3
+     *   -Drevision=1.3.0
+     * </pre>
+     * and check if the {@code -Drevision-1.3.0} option can be overwritten via command line
+     * argument.
+     * @throws Exception
+     */
+    public void testMVNConfigurationDefinedPropertiesCanBeOverwrittenViaCommandLine() throws Exception {
+        System.setProperty( MavenCli.MULTIMODULE_PROJECT_DIRECTORY, new File( "src/test/projects/mavenConfigProperties" ).getCanonicalPath() );
+        CliRequest request = new CliRequest( new String[]{ "-Drevision=8.1.0"}, null );
+
+        cli.initialize( request );
+        // read .mvn/maven.config
+        cli.cli( request );
+        cli.properties( request );
+        
+        String revision = System.getProperty( "revision" );
+        assertEquals( "8.1.0", revision );
+
+    }
 }

http://git-wip-us.apache.org/repos/asf/maven/blob/9f2452ad/maven-embedder/src/test/projects/mavenConfigProperties/.mvn/maven.config
----------------------------------------------------------------------
diff --git a/maven-embedder/src/test/projects/mavenConfigProperties/.mvn/maven.config b/maven-embedder/src/test/projects/mavenConfigProperties/.mvn/maven.config
new file mode 100644
index 0000000..c73de53
--- /dev/null
+++ b/maven-embedder/src/test/projects/mavenConfigProperties/.mvn/maven.config
@@ -0,0 +1,3 @@
+-T 3
+-Drevision=1.3.0
+