From f4332895907709356df66e135efbe21a601b2413 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Efe=20G=C3=B6kdemir?= Date: Wed, 23 Sep 2026 19:13:54 +0300 Subject: [PATCH] Capture embedded executor properties per execution MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Snapshot system properties when an execution begins so a reusable embedded executor preserves caller changes made between runs. Add a regression test covering a replaced system properties object. Co-authored-by: Codex Signed-off-by: Efe Gökdemir --- .../embedded/EmbeddedMavenExecutor.java | 2 + .../embedded/EmbeddedMavenExecutorTest.java | 45 +++++++++++++++++++ 2 files changed, 47 insertions(+) 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); + } + } + } }