Skip to content

Commit

Permalink
[MNG-7914] Provide a single entry point for configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet committed Jul 5, 2024
1 parent 76fbb4b commit f658846
Show file tree
Hide file tree
Showing 7 changed files with 1,796 additions and 29 deletions.
10 changes: 10 additions & 0 deletions apache-maven/src/assembly/maven/conf/maven.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#
# Maven user properties
#

# Required files to load
${includes} =

# Optionally load global user properties and project user properties
${optionals} = ${user.home}/.m2/maven.properties \
${session.rootDirectory}/.mvn/maven.properties
6 changes: 6 additions & 0 deletions maven-embedder/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,12 @@ under the License.
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.jimfs</groupId>
<artifactId>jimfs</artifactId>
<version>1.3.0</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
82 changes: 58 additions & 24 deletions maven-embedder/src/main/java/org/apache/maven/cli/MavenCli.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@
import java.io.InputStream;
import java.io.PrintStream;
import java.nio.charset.Charset;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.util.Map.Entry;
import java.util.function.Consumer;
Expand Down Expand Up @@ -62,6 +63,8 @@
import org.apache.maven.cli.logging.Slf4jConfigurationFactory;
import org.apache.maven.cli.logging.Slf4jLoggerManager;
import org.apache.maven.cli.logging.Slf4jStdoutLogger;
import org.apache.maven.cli.props.InterpolationHelper;
import org.apache.maven.cli.props.PropertiesLoader;
import org.apache.maven.cli.transfer.*;
import org.apache.maven.eventspy.internal.EventSpyDispatcher;
import org.apache.maven.exception.DefaultExceptionHandler;
Expand Down Expand Up @@ -179,6 +182,8 @@ public class MavenCli {

private MessageBuilderFactory messageBuilderFactory;

private FileSystem fileSystem = FileSystems.getDefault();

private static final Pattern NEXT_LINE = Pattern.compile("\r?\n");

public MavenCli() {
Expand Down Expand Up @@ -328,7 +333,7 @@ void initialize(CliRequest cliRequest) throws ExitException {
// We need to locate the top level project which may be pointed at using
// the -f/--file option. However, the command line isn't parsed yet, so
// we need to iterate through the args to find it and act upon it.
Path topDirectory = Paths.get(cliRequest.workingDirectory);
Path topDirectory = fileSystem.getPath(cliRequest.workingDirectory);
boolean isAltFile = false;
for (String arg : cliRequest.args) {
if (isAltFile) {
Expand Down Expand Up @@ -371,7 +376,9 @@ void initialize(CliRequest cliRequest) throws ExitException {
String mavenHome = System.getProperty("maven.home");

if (mavenHome != null) {
System.setProperty("maven.home", new File(mavenHome).getAbsolutePath());
System.setProperty(
"maven.home",
getCanonicalPath(fileSystem.getPath(mavenHome)).toString());
}
}

Expand Down Expand Up @@ -1581,20 +1588,15 @@ int calculateDegreeOfConcurrency(String threadConfiguration) {
// Properties handling
// ----------------------------------------------------------------------

static void populateProperties(
void populateProperties(
CommandLine commandLine, Properties paths, Properties systemProperties, Properties userProperties)
throws Exception {
EnvironmentUtils.addEnvVars(systemProperties);

// ----------------------------------------------------------------------
// Options that are set on the command line become system properties
// and therefore are set in the session properties. System properties
// are most dominant.
// Load environment and system properties
// ----------------------------------------------------------------------

final Properties userSpecifiedProperties =
commandLine.getOptionProperties(String.valueOf(CLIManager.SET_USER_PROPERTY));

EnvironmentUtils.addEnvVars(systemProperties);
SystemProperties.addSystemProperties(systemProperties);

// ----------------------------------------------------------------------
Expand All @@ -1610,20 +1612,48 @@ static void populateProperties(
String mavenBuildVersion = CLIReportingUtils.createMavenVersionString(buildProperties);
systemProperties.setProperty("maven.build.version", mavenBuildVersion);

BasicInterpolator interpolator =
createInterpolator(paths, systemProperties, userProperties, userSpecifiedProperties);
for (Map.Entry<Object, Object> e : userSpecifiedProperties.entrySet()) {
String name = (String) e.getKey();
String value = interpolator.interpolate((String) e.getValue());
userProperties.setProperty(name, value);
// ----------------------------------------------------------------------
// I'm leaving the setting of system properties here as not to break
// the SystemPropertyProfileActivator. This won't harm embedding. jvz.
// ----------------------------------------------------------------------
if (System.getProperty(name) == null) {
System.setProperty(name, value);
}
// ----------------------------------------------------------------------
// Options that are set on the command line become system properties
// and therefore are set in the session properties. System properties
// are most dominant.
// ----------------------------------------------------------------------

Properties userSpecifiedProperties =
commandLine.getOptionProperties(String.valueOf(CLIManager.SET_USER_PROPERTY));
userProperties.putAll(userSpecifiedProperties);

// ----------------------------------------------------------------------
// Load config files
// ----------------------------------------------------------------------
InterpolationHelper.SubstitutionCallback callback = or(paths::getProperty, systemProperties::getProperty);

if (systemProperties.getProperty("maven.conf") != null) {
Path mavenConf = fileSystem.getPath(systemProperties.getProperty("maven.conf"));

Path propertiesFile = mavenConf.resolve("maven.properties");
PropertiesLoader.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)));
}

private static InterpolationHelper.SubstitutionCallback or(InterpolationHelper.SubstitutionCallback... callbacks) {
return s -> {
for (InterpolationHelper.SubstitutionCallback cb : callbacks) {
String r = cb.getValue(s);
if (r != null) {
return r;
}
}
return null;
};
}

private static BasicInterpolator createInterpolator(Properties... properties) {
Expand Down Expand Up @@ -1689,4 +1719,8 @@ protected void customizeContainer(PlexusContainer container) {}
protected ModelProcessor createModelProcessor(PlexusContainer container) throws ComponentLookupException {
return container.lookup(ModelProcessor.class);
}

public void setFileSystem(FileSystem fileSystem) {
this.fileSystem = fileSystem;
}
}
Loading

0 comments on commit f658846

Please sign in to comment.