diff --git a/maven-embedder/src/main/java/org/apache/maven/cli/CLIRequestUtils.java b/maven-embedder/src/main/java/org/apache/maven/cli/CLIRequestUtils.java index 1947b8ca33..d0aedb7e81 100644 --- a/maven-embedder/src/main/java/org/apache/maven/cli/CLIRequestUtils.java +++ b/maven-embedder/src/main/java/org/apache/maven/cli/CLIRequestUtils.java @@ -4,12 +4,16 @@ import org.apache.commons.cli.CommandLine; import org.apache.maven.MavenTransferListener; import org.apache.maven.execution.DefaultMavenExecutionRequest; import org.apache.maven.execution.MavenExecutionRequest; +import org.codehaus.plexus.util.cli.CommandLineUtils; import java.io.File; +import java.io.IOException; import java.util.ArrayList; +import java.util.Iterator; import java.util.List; import java.util.Properties; import java.util.StringTokenizer; +import java.util.Map.Entry; public final class CLIRequestUtils { @@ -225,10 +229,27 @@ public final class CLIRequestUtils // System properties handling // ---------------------------------------------------------------------- - private static Properties getExecutionProperties( CommandLine commandLine ) + static Properties getExecutionProperties( CommandLine commandLine ) { Properties executionProperties = new Properties(); + // add the env vars to the property set, with the "env." prefix + // XXX support for env vars should probably be removed from the ModelInterpolator + try + { + Properties envVars = CommandLineUtils.getSystemEnvVars(); + Iterator i = envVars.entrySet().iterator(); + while ( i.hasNext() ) + { + Entry e = (Entry) i.next(); + executionProperties.setProperty( "env." + e.getKey().toString(), e.getValue().toString() ); + } + } + catch ( IOException e ) + { + System.err.println( "Error getting environment vars for profile activation: " + e ); + } + // ---------------------------------------------------------------------- // Options that are set on the command line become system properties // and therefore are set in the session properties. System properties @@ -276,13 +297,5 @@ public final class CLIRequestUtils } executionProperties.setProperty( name, value ); - - // ---------------------------------------------------------------------- - // I'm leaving the setting of system properties here as not to break - // the SystemPropertyProfileActivator. This won't harm embedding. jvz. - // ---------------------------------------------------------------------- - - System.setProperty( name, value ); } - } diff --git a/maven-embedder/src/test/java/org/apache/maven/cli/CLIRequestUtilsTest.java b/maven-embedder/src/test/java/org/apache/maven/cli/CLIRequestUtilsTest.java index 3c57f1764a..10ed37edad 100644 --- a/maven-embedder/src/test/java/org/apache/maven/cli/CLIRequestUtilsTest.java +++ b/maven-embedder/src/test/java/org/apache/maven/cli/CLIRequestUtilsTest.java @@ -44,4 +44,27 @@ public class CLIRequestUtilsTest assertTrue( ( goals == null ) || goals.isEmpty() ); } + public void testGetExecutionProperties() + throws Exception + { + System.setProperty( "test.property.1", "1.0" ); + System.setProperty( "test.property.2", "2.0" ); + Properties p = CLIRequestUtils.getExecutionProperties( new CLIManager().parse( new String[] { + "-Dtest.property.2=2.1", + "-Dtest.property.3=3.0" } ) ); + + // assume that everybody has a PATH env var + String envPath = p.getProperty( "env.PATH" ); + if ( envPath == null ) + { + envPath = p.getProperty( "env.Path" ); + } + assertNotNull( envPath ); + + assertEquals( "1.0", p.getProperty( "test.property.1" ) ); + assertEquals( "3.0", p.getProperty( "test.property.3" ) ); + + // sys props should override cmdline props + assertEquals( "2.0", p.getProperty( "test.property.2" ) ); + } }