diff --git a/maven-executor/src/main/java/org/apache/maven/executor/embedded/EmbeddedMavenExecutor.java b/maven-executor/src/main/java/org/apache/maven/executor/embedded/EmbeddedMavenExecutor.java index eef1a04..333f145 100644 --- a/maven-executor/src/main/java/org/apache/maven/executor/embedded/EmbeddedMavenExecutor.java +++ b/maven-executor/src/main/java/org/apache/maven/executor/embedded/EmbeddedMavenExecutor.java @@ -136,6 +136,8 @@ public ExecutorResult execute(ExecutorRequest executorRequest) throws ExecutorEx if (closed.get()) { throw new ExecutorException("Executor is closed"); } + Properties originalProperties = new Properties(); + originalProperties.putAll(System.getProperties()); validate(executorRequest); String command = executorRequest.command(); Context context = contextMap.computeIfAbsent( diff --git a/maven-executor/src/test/java/org/apache/maven/executor/embedded/EmbeddedMavenExecutorTest.java b/maven-executor/src/test/java/org/apache/maven/executor/embedded/EmbeddedMavenExecutorTest.java index 6f096c7..9080d84 100644 --- a/maven-executor/src/test/java/org/apache/maven/executor/embedded/EmbeddedMavenExecutorTest.java +++ b/maven-executor/src/test/java/org/apache/maven/executor/embedded/EmbeddedMavenExecutorTest.java @@ -18,18 +18,63 @@ */ package org.apache.maven.executor.embedded; +import java.nio.file.Files; import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Properties; +import org.apache.maven.executor.Environment; import org.apache.maven.executor.Executor; +import org.apache.maven.executor.ExecutorRequest; import org.apache.maven.executor.MavenExecutorTestSupport; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +import static org.junit.jupiter.api.Assertions.assertEquals; /** * Embedded executor UT */ public class EmbeddedMavenExecutorTest extends MavenExecutorTestSupport { + @TempDir + Path tempDir; + @Override protected Executor doSelectExecutor(Path installationDirectory) { return new EmbeddedMavenExecutor(installationDirectory); } + + @Test + void preservesSystemPropertiesSetBetweenExecutions() throws Exception { + String propertyName = "maven.executor.test.property"; + String originalValue = System.getProperty(propertyName); + EmbeddedMavenExecutor executor = new EmbeddedMavenExecutor(Paths.get(Environment.MAVEN4_HOME)); + try { + Files.createDirectories(tempDir.resolve("home")); + ExecutorRequest request = ExecutorRequest.mavenBuilder() + .cwd(tempDir) + .userHomeDirectory(tempDir.resolve("home")) + .argument("--version") + .build(); + + System.clearProperty(propertyName); + executor.execute(request); + Properties properties = new Properties(); + properties.putAll(System.getProperties()); + properties.setProperty(propertyName, "set-between-executions"); + System.setProperties(properties); + + executor.execute(request); + + assertEquals("set-between-executions", System.getProperty(propertyName)); + } finally { + executor.close(); + if (originalValue == null) { + System.clearProperty(propertyName); + } else { + System.setProperty(propertyName, originalValue); + } + } + } }