diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/profile/DefaultProfileActivationContext.java b/maven-model-builder/src/main/java/org/apache/maven/model/profile/DefaultProfileActivationContext.java index fa3ed33b5818..dda76e4255c6 100644 --- a/maven-model-builder/src/main/java/org/apache/maven/model/profile/DefaultProfileActivationContext.java +++ b/maven-model-builder/src/main/java/org/apache/maven/model/profile/DefaultProfileActivationContext.java @@ -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; @@ -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); + } } diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/profile/ProfileActivationContext.java b/maven-model-builder/src/main/java/org/apache/maven/model/profile/ProfileActivationContext.java index e4d48154973a..f1baf0b3523e 100644 --- a/maven-model-builder/src/main/java/org/apache/maven/model/profile/ProfileActivationContext.java +++ b/maven-model-builder/src/main/java/org/apache/maven/model/profile/ProfileActivationContext.java @@ -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. * @@ -79,4 +81,11 @@ public interface ProfileActivationContext { * @return The project properties, never {@code null}. */ Map getProjectProperties(); + + /** + * Interpolates value from this context. + * + * @since 3.9.7 + */ + String interpolate(String value) throws InterpolationException; } diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/profile/activation/PropertyProfileActivator.java b/maven-model-builder/src/main/java/org/apache/maven/model/profile/activation/PropertyProfileActivator.java index 8fff2563b752..03c9b1aa7ee5 100644 --- a/maven-model-builder/src/main/java/org/apache/maven/model/profile/activation/PropertyProfileActivator.java +++ b/maven-model-builder/src/main/java/org/apache/maven/model/profile/activation/PropertyProfileActivator.java @@ -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; /** @@ -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); @@ -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; + } + } } diff --git a/maven-model-builder/src/test/java/org/apache/maven/model/profile/activation/PropertyProfileActivatorTest.java b/maven-model-builder/src/test/java/org/apache/maven/model/profile/activation/PropertyProfileActivatorTest.java index bc827bdb218b..16b228304b4d 100644 --- a/maven-model-builder/src/test/java/org/apache/maven/model/profile/activation/PropertyProfileActivatorTest.java +++ b/maven-model-builder/src/test/java/org/apache/maven/model/profile/activation/PropertyProfileActivatorTest.java @@ -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)); + } }