Skip to content

Commit

Permalink
[MNG-6303] Interpolate user supplied properties
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet committed Mar 16, 2023
1 parent 8052c72 commit c7644da
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 11 deletions.
44 changes: 33 additions & 11 deletions maven-embedder/src/main/java/org/apache/maven/cli/MavenCli.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@
import org.codehaus.plexus.classworlds.realm.ClassRealm;
import org.codehaus.plexus.classworlds.realm.NoSuchRealmException;
import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
import org.codehaus.plexus.interpolation.AbstractValueSource;
import org.codehaus.plexus.interpolation.InterpolationException;
import org.codehaus.plexus.interpolation.StringSearchInterpolator;
import org.codehaus.plexus.logging.LoggerManager;
import org.codehaus.plexus.util.StringUtils;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
Expand Down Expand Up @@ -1477,7 +1480,6 @@ static void populateProperties(CommandLine commandLine, Properties systemPropert

final Properties userSpecifiedProperties =
commandLine.getOptionProperties(String.valueOf(CLIManager.SET_USER_PROPERTY));
userSpecifiedProperties.forEach((prop, value) -> setCliProperty((String) prop, (String) value, userProperties));

SystemProperties.addSystemProperties(systemProperties);

Expand All @@ -1493,17 +1495,37 @@ static void populateProperties(CommandLine commandLine, Properties systemPropert

String mavenBuildVersion = CLIReportingUtils.createMavenVersionString(buildProperties);
systemProperties.setProperty("maven.build.version", mavenBuildVersion);
}

private static void setCliProperty(String name, String value, Properties properties) {
properties.setProperty(name, value);

// ----------------------------------------------------------------------
// I'm leaving the setting of system properties here as not to break
// the SystemPropertyProfileActivator. This won't harm embedding. jvz.
// ----------------------------------------------------------------------

System.setProperty(name, value);
StringSearchInterpolator interpolator = new StringSearchInterpolator();
interpolator.addValueSource(new AbstractValueSource(false) {
@Override
public Object getValue(String expression) {
Object val = userProperties.getProperty(expression);
if (val == null) {
val = systemProperties.getProperty(expression);
}
if (val == null) {
val = userSpecifiedProperties.getProperty(expression);
}
return val;
}
});
userSpecifiedProperties.forEach((prop, v) -> {
try {
String name = (String) prop;
String value = interpolator.interpolate((String) v);
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);
}
} catch (InterpolationException e) {
throw new RuntimeException(e);
}
});
}

static class ExitException extends Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,19 @@ public void populatePropertiesOverwrite() throws Exception {
assertThat(request.getUserProperties().getProperty("x"), is("false"));
}

@Test
public void testPropertiesInterpolation() throws Exception {
// Arrange
CliRequest request = new CliRequest(new String[] {"-Dfoo=bar", "-Dval=s${foo}i", "validate"}, null);

// Act
cli.cli(request);
cli.properties(request);

// Assert
assertThat(request.getUserProperties().getProperty("val"), is("sbari"));
}

private MavenProject createMavenProject(String groupId, String artifactId) {
MavenProject project = new MavenProject();
project.setGroupId(groupId);
Expand Down

0 comments on commit c7644da

Please sign in to comment.