[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.
This commit is contained in:
Karl Heinz Marbaise 2016-08-07 12:21:01 +02:00
parent 4604c49ee4
commit ca43030313
3 changed files with 62 additions and 6 deletions

View File

@ -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;
@ -542,7 +543,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 );
}
@ -1581,9 +1584,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 );

View File

@ -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 );
}
}

View File

@ -0,0 +1,3 @@
-T 3
-Drevision=1.3.0