Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
import java.util.Map;
import java.util.Properties;

import org.codehaus.plexus.interpolation.InterpolationException;
import org.codehaus.plexus.interpolation.MapBasedValueSource;
import org.codehaus.plexus.interpolation.RegexBasedInterpolator;

import static java.util.stream.Collectors.collectingAndThen;
import static java.util.stream.Collectors.toMap;

Expand Down Expand Up @@ -205,4 +209,13 @@ public DefaultProfileActivationContext setProjectProperties(Properties projectPr

return this;
}

@Override
public String interpolate(String value) throws InterpolationException {
RegexBasedInterpolator interpolator = new RegexBasedInterpolator();
interpolator.addValueSource(new MapBasedValueSource(userProperties));
interpolator.addValueSource(new MapBasedValueSource(projectProperties));
interpolator.addValueSource(new MapBasedValueSource(systemProperties));
return interpolator.interpolate(value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import java.util.List;
import java.util.Map;

import org.codehaus.plexus.interpolation.InterpolationException;

/**
* Describes the environmental context used to determine the activation status of profiles.
*
Expand Down Expand Up @@ -79,4 +81,11 @@ public interface ProfileActivationContext {
* @return The project properties, never {@code null}.
*/
Map<String, String> getProjectProperties();

/**
* Interpolates value from this context.
*
* @since 3.9.7
*/
String interpolate(String value) throws InterpolationException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.apache.maven.model.building.ModelProblemCollector;
import org.apache.maven.model.building.ModelProblemCollectorRequest;
import org.apache.maven.model.profile.ProfileActivationContext;
import org.codehaus.plexus.interpolation.InterpolationException;
import org.codehaus.plexus.util.StringUtils;

/**
Expand Down Expand Up @@ -82,6 +83,10 @@ public boolean isActive(Profile profile, ProfileActivationContext context, Model
reverseValue = true;
propValue = propValue.substring(1);
}
propValue = interpolate(profile, property, propValue, context, problems);
if (propValue == null) {
return false;
}

// we have a value, so it has to match the system value...
boolean result = propValue.equals(sysValue);
Expand Down Expand Up @@ -109,4 +114,20 @@ public boolean presentInConfig(Profile profile, ProfileActivationContext context
}
return true;
}

private String interpolate(
Profile profile,
ActivationProperty property,
String value,
ProfileActivationContext context,
ModelProblemCollector problems) {
try {
return context.interpolate(value);
} catch (InterpolationException e) {
problems.add(new ModelProblemCollectorRequest(Severity.ERROR, Version.BASE)
.setMessage("The property value could not be interpolated in the profile " + profile.getId())
.setLocation(property.getLocation("")));
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,52 @@ public void testWithValue_UserPropertyDominantOverSystemProperty() throws Except

assertActivation(false, profile, newContext(props2, props1));
}

public void testWithValueInterpolation() throws Exception {
Profile profile = newProfile("prop", "${value}");
// prop => key
// ${value} => key

Properties userProperties = newProperties("prop", "key");
Properties systemProperties = newProperties("value", "key");

assertActivation(true, profile, newContext(userProperties, systemProperties));
assertActivation(false, profile, newContext(new Properties(), systemProperties));
assertActivation(false, profile, newContext(userProperties, new Properties()));
}

public void testWithValueInterpolationNegation() throws Exception {
Profile profile = newProfile("prop", "!${value}");
// prop => key
// ${value} => key

Properties userProperties = newProperties("prop", "key");
Properties systemProperties = newProperties("value", "key");

assertActivation(false, profile, newContext(userProperties, systemProperties));
assertActivation(true, profile, newContext(new Properties(), systemProperties));
assertActivation(true, profile, newContext(userProperties, new Properties()));
}

public void testWithValueInterpolationMismatch() throws Exception {
Profile profile = newProfile("prop", "${value}");
// prop => key
// ${value} => anotherKey

Properties userProperties = newProperties("prop", "key");
Properties systemProperties = newProperties("value", "anotherKey");

assertActivation(false, profile, newContext(userProperties, systemProperties));
}

public void testWithValueInterpolationMismatchNegation() throws Exception {
Profile profile = newProfile("prop", "!${value}");
// prop => key
// ${value} => anotherKey

Properties userProperties = newProperties("prop", "key");
Properties systemProperties = newProperties("value", "anotherKey");

assertActivation(true, profile, newContext(userProperties, systemProperties));
}
}