[MNG-8283] Parser must not alter global state (#1776)

Parser must not (esp "partially") alter global state like
setting Java System Properties. If invoker wants, invoker
can set those (and cleanup).

---

https://issues.apache.org/jira/browse/MNG-8283
This commit is contained in:
Tamas Cservenak 2024-10-05 09:32:46 +02:00 committed by GitHub
parent 1817aaeaad
commit 20fe966770
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 14 additions and 13 deletions

View File

@ -34,7 +34,6 @@ import java.util.Map;
import java.util.Objects;
import java.util.Properties;
import java.util.ServiceLoader;
import java.util.Set;
import java.util.function.Function;
import org.apache.maven.api.Constants;
@ -156,8 +155,6 @@ public abstract class BaseParser<O extends Options, R extends InvokerRequest<O>>
}
result = getCanonicalPath(Paths.get(mavenHome));
}
// TODO: we still do this but would be cool if this becomes unneeded
System.setProperty(Constants.MAVEN_HOME, result.toString());
return result;
}
@ -295,15 +292,6 @@ public abstract class BaseParser<O extends Options, R extends InvokerRequest<O>>
Path propertiesFile = mavenConf.resolve("maven.properties");
MavenPropertiesLoader.loadProperties(userProperties, propertiesFile, callback, false);
// ----------------------------------------------------------------------
// I'm leaving the setting of system properties here as not to break
// the SystemPropertyProfileActivator. This won't harm embedding. jvz.
// ----------------------------------------------------------------------
Set<String> sys = SystemProperties.getSystemProperties().stringPropertyNames();
userProperties.stringPropertyNames().stream()
.filter(k -> !sys.contains(k))
.forEach(k -> System.setProperty(k, userProperties.getProperty(k)));
return toMap(userProperties);
}

View File

@ -25,6 +25,7 @@ import java.io.PrintStream;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Properties;
@ -153,7 +154,17 @@ public abstract class LookupInvoker<
requireNonNull(invokerRequest);
try (C context = createContext(invokerRequest)) {
Properties props = (Properties) System.getProperties().clone();
try {
HashSet<String> sys =
new HashSet<>(invokerRequest.systemProperties().keySet());
invokerRequest.userProperties().entrySet().stream()
.filter(k -> !sys.contains(k.getKey()))
.forEach(k -> System.setProperty(k.getKey(), k.getValue()));
System.setProperty(
Constants.MAVEN_HOME,
invokerRequest.installationDirectory().toString());
validate(context);
prepare(context);
configureLogging(context);
@ -181,6 +192,8 @@ public abstract class LookupInvoker<
return execute(context);
} catch (Exception e) {
throw handleException(context, e);
} finally {
System.setProperties(props);
}
}
}

View File

@ -110,9 +110,9 @@ public abstract class DefaultMavenInvoker<
DefaultMavenExecutionRequest mavenExecutionRequest = new DefaultMavenExecutionRequest();
mavenExecutionRequest.setRepositoryCache(new DefaultRepositoryCache());
mavenExecutionRequest.setInteractiveMode(true);
mavenExecutionRequest.setCacheTransferError(false);
mavenExecutionRequest.setIgnoreInvalidArtifactDescriptor(true);
mavenExecutionRequest.setIgnoreMissingArtifactDescriptor(true);
mavenExecutionRequest.setProjectPresent(true);
mavenExecutionRequest.setRecursive(true);
mavenExecutionRequest.setReactorFailureBehavior(MavenExecutionRequest.REACTOR_FAIL_FAST);
mavenExecutionRequest.setStartTime(new Date());