[MSHARED-1014] Optionally inherit system environment variables by Commandline
diff --git a/src/main/java/org/apache/maven/shared/utils/cli/Commandline.java b/src/main/java/org/apache/maven/shared/utils/cli/Commandline.java
index e03e905..2d5d63c 100644
--- a/src/main/java/org/apache/maven/shared/utils/cli/Commandline.java
+++ b/src/main/java/org/apache/maven/shared/utils/cli/Commandline.java
@@ -65,12 +65,14 @@
public class Commandline
implements Cloneable
{
- private final List<Arg> arguments = new Vector<Arg>();
+ private final List<Arg> arguments = new Vector<>();
private final Map<String, String> envVars = Collections.synchronizedMap( new LinkedHashMap<String, String>() );
private Shell shell;
+ private boolean shellEnvironmentInherited = true;
+
/**
* Create a new command line object.
* Shell is autodetected from operating system.
@@ -198,14 +200,13 @@
*/
public void addEnvironment( String name, String value )
{
- //envVars.add( name + "=" + value );
envVars.put( name, value );
}
/**
* Add system environment variables.
*/
- public void addSystemEnvironment()
+ private void addSystemEnvironment()
{
Properties systemEnvVars = CommandLineUtils.getSystemEnvVars();
@@ -226,7 +227,11 @@
*/
public String[] getEnvironmentVariables()
{
- addSystemEnvironment();
+ if ( isShellEnvironmentInherited() )
+ {
+ addSystemEnvironment();
+ }
+
List<String> environmentVars = new ArrayList<>();
for ( String name : envVars.keySet() )
{
@@ -297,7 +302,7 @@
*/
public String[] getArguments( boolean mask )
{
- List<String> result = new ArrayList<String>( arguments.size() * 2 );
+ List<String> result = new ArrayList<>( arguments.size() * 2 );
for ( Arg argument : arguments )
{
Argument arg = (Argument) argument;
@@ -378,6 +383,29 @@
}
/**
+ * Indicates whether the environment variables of the current process
+ * should are propagated to the executing Command.
+ * By default, the current environment variables are inherited by the new Command line execution.
+ *
+ * @return <code>true</code> if the environment variables should be propagated, <code>false</code> otherwise.
+ */
+ public boolean isShellEnvironmentInherited()
+ {
+ return shellEnvironmentInherited;
+ }
+
+ /**
+ * Specifies whether the environment variables of the current process should be propagated to the executing Command.
+ *
+ * @param shellEnvironmentInherited <code>true</code> if the environment variables should be propagated,
+ * <code>false</code> otherwise.
+ */
+ public void setShellEnvironmentInherited( boolean shellEnvironmentInherited )
+ {
+ this.shellEnvironmentInherited = shellEnvironmentInherited;
+ }
+
+ /**
* Execute the command.
*
* @return the process
diff --git a/src/test/java/org/apache/maven/shared/utils/cli/CommandLineUtilsTest.java b/src/test/java/org/apache/maven/shared/utils/cli/CommandLineUtilsTest.java
index 81b2997..fdc83b0 100644
--- a/src/test/java/org/apache/maven/shared/utils/cli/CommandLineUtilsTest.java
+++ b/src/test/java/org/apache/maven/shared/utils/cli/CommandLineUtilsTest.java
@@ -56,9 +56,8 @@
@Test
public void testEnsureCaseSensitivity()
- throws Exception
{
- Map<String, String> data = new HashMap<String, String>();
+ Map<String, String> data = new HashMap<>();
data.put( "abz", "cool" );
assertTrue( CommandLineUtils.ensureCaseSensitivity( data, false ).containsKey( "ABZ" ) );
assertTrue( CommandLineUtils.ensureCaseSensitivity( data, true ).containsKey( "abz" ) );
@@ -69,7 +68,6 @@
*/
@Test
public void testGetSystemEnvVarsWindows()
- throws Exception
{
if ( !Os.isFamily( Os.FAMILY_WINDOWS ) )
{
@@ -103,14 +101,9 @@
assertCmdLineArgs( new String[] { "foo", " ' ", "bar" }, "foo \" ' \" bar" );
}
-
@Test
- public void givenASingleQuoteMarkInArgument_whenExecutingCode_thenNoExceptionIsThrown() throws Exception {
- new Commandline("echo \"let's go\"").execute();
- }
-
- @Test
- public void givenADoubleQuoteMarkInArgument_whenExecutingCode_thenCommandLineExceptionIsThrown() throws Exception {
+ public void givenADoubleQuoteMarkInArgument_whenExecutingCode_thenCommandLineExceptionIsThrown()
+ {
try {
new Commandline("echo \"let\"s go\"").execute();
} catch (CommandLineException e) {
@@ -124,8 +117,7 @@
@Test
public void givenASingleQuoteMarkInArgument_whenExecutingCode_thenExitCode0Returned() throws Exception {
final Process p = new Commandline("echo \"let's go\"").execute();
- // Note, this sleep should be removed when java version reaches Java 8
- Thread.sleep(1000);
+ p.waitFor();
assertEquals(0, p.exitValue());
}
@@ -160,7 +152,9 @@
public void givenAnEscapedDoubleQuoteMarkInArgument_whenTranslatingToCmdLineArgs_thenNoExceptionIsThrown()
throws Exception
{
- new Commandline( "echo \"let\\\"s go\"" ).execute();
+ Process p = new Commandline( "echo \"let\\\"s go\"" ).execute();
+ p.waitFor();
+ assertEquals(0, p.exitValue());
}
private void assertCmdLineArgs( final String[] expected, final String cmdLine )
@@ -185,7 +179,7 @@
}
@Test
- public void environmentVariableFromSystemIsCopied() {
+ public void environmentVariableFromSystemIsCopiedByDefault() {
Commandline commandline = new Commandline();
@@ -196,6 +190,18 @@
}
@Test
+ public void environmentVariableFromSystemIsNotCopiedIfInheritedIsFalse() {
+
+ Commandline commandline = new Commandline();
+ commandline.setShellEnvironmentInherited( false );
+
+ String[] environmentVariables = commandline.getEnvironmentVariables();
+
+ assertNotNull(environmentVariables);
+ assertEquals(0, environmentVariables.length );
+ }
+
+ @Test
public void environmentVariableFromSystemIsRemoved() {
Commandline commandline = new Commandline();