[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/03d9d600 Tree: http://git-wip-us.apache.org/repos/asf/maven/tree/03d9d600 Diff: http://git-wip-us.apache.org/repos/asf/maven/diff/03d9d600 Branch: refs/heads/MNG-6078 Commit: 03d9d600b5f37cd61b478370f0b698e241e7ff79 Parents: aecccf9 Author: Karl Heinz Marbaise <[email protected]> Authored: Sun Aug 7 12:21:01 2016 +0200 Committer: Karl Heinz Marbaise <[email protected]> Committed: Sun Jan 29 14:05:58 2017 +0100 ---------------------------------------------------------------------- .../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/03d9d600/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 c0b0dcf..57b0f63 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 @@ -27,6 +27,7 @@ import com.google.inject.AbstractModule; 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; @@ -543,7 +544,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 ); } @@ -1582,9 +1585,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/03d9d600/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/03d9d600/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 +
