From 7e6c586bfb742d46b45af6f11921bb299ebee6a7 Mon Sep 17 00:00:00 2001 From: Tamas Cservenak Date: Sun, 21 Apr 2024 16:03:43 +0200 Subject: [PATCH 1/4] [MNG-8081] Profile property activation interpolation --- https://issues.apache.org/jira/browse/MNG-8081 --- .../DefaultProfileActivationContext.java | 17 +++++++ .../profile/ProfileActivationContext.java | 7 +++ .../activation/PropertyProfileActivator.java | 1 + .../PropertyProfileActivatorTest.java | 48 +++++++++++++++++++ 4 files changed, 73 insertions(+) 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 fa3ed33b581..b18a3e99ed3 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,17 @@ public DefaultProfileActivationContext setProjectProperties(Properties projectPr return this; } + + @Override + public String interpolate(String value) { + RegexBasedInterpolator interpolator = new RegexBasedInterpolator(); + interpolator.addValueSource(new MapBasedValueSource(userProperties)); + interpolator.addValueSource(new MapBasedValueSource(projectProperties)); + interpolator.addValueSource(new MapBasedValueSource(systemProperties)); + try { + return interpolator.interpolate(value); + } catch (InterpolationException e) { + throw new IllegalArgumentException(e); + } + } } 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 e4d48154973..ce9b8a57ca2 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 @@ -79,4 +79,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); } 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 8fff2563b75..2f504e48553 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 @@ -82,6 +82,7 @@ public boolean isActive(Profile profile, ProfileActivationContext context, Model reverseValue = true; propValue = propValue.substring(1); } + propValue = context.interpolate(propValue); // we have a value, so it has to match the system value... boolean result = propValue.equals(sysValue); 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 bc827bdb218..625d69781c0 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} => key + + 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} => key + + Properties userProperties = newProperties("prop", "key"); + Properties systemProperties = newProperties("value", "anotherKey"); + + assertActivation(true, profile, newContext(userProperties, systemProperties)); + } } From 82d3eefb44da17a3cffe8476a94ebef66c281886 Mon Sep 17 00:00:00 2001 From: Tamas Cservenak Date: Sun, 21 Apr 2024 16:09:42 +0200 Subject: [PATCH 2/4] Fix comments --- .../profile/activation/PropertyProfileActivatorTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 625d69781c0..16b228304b4 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 @@ -185,7 +185,7 @@ public void testWithValueInterpolationNegation() throws Exception { public void testWithValueInterpolationMismatch() throws Exception { Profile profile = newProfile("prop", "${value}"); // prop => key - // ${value} => key + // ${value} => anotherKey Properties userProperties = newProperties("prop", "key"); Properties systemProperties = newProperties("value", "anotherKey"); @@ -196,7 +196,7 @@ public void testWithValueInterpolationMismatch() throws Exception { public void testWithValueInterpolationMismatchNegation() throws Exception { Profile profile = newProfile("prop", "!${value}"); // prop => key - // ${value} => key + // ${value} => anotherKey Properties userProperties = newProperties("prop", "key"); Properties systemProperties = newProperties("value", "anotherKey"); From ece2fb6e2ba266ec2fc277dbeca2ef0a8ced04c2 Mon Sep 17 00:00:00 2001 From: Tamas Cservenak Date: Sun, 21 Apr 2024 16:14:15 +0200 Subject: [PATCH 3/4] Sort out error handling --- .../DefaultProfileActivationContext.java | 8 ++------ .../profile/ProfileActivationContext.java | 4 +++- .../activation/PropertyProfileActivator.java | 18 +++++++++++++++++- 3 files changed, 22 insertions(+), 8 deletions(-) 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 b18a3e99ed3..dda76e4255c 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 @@ -211,15 +211,11 @@ public DefaultProfileActivationContext setProjectProperties(Properties projectPr } @Override - public String interpolate(String value) { + 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)); - try { - return interpolator.interpolate(value); - } catch (InterpolationException e) { - throw new IllegalArgumentException(e); - } + 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 ce9b8a57ca2..f1baf0b3523 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. * @@ -85,5 +87,5 @@ public interface ProfileActivationContext { * * @since 3.9.7 */ - String interpolate(String value); + 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 2f504e48553..452ae094932 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,7 +83,7 @@ public boolean isActive(Profile profile, ProfileActivationContext context, Model reverseValue = true; propValue = propValue.substring(1); } - propValue = context.interpolate(propValue); + propValue = interpolate(profile, property, propValue, context, problems); // we have a value, so it has to match the system value... boolean result = propValue.equals(sysValue); @@ -110,4 +111,19 @@ 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(""))); + } + } } From 4aaa101b3719421c5253ee54755136be9f048583 Mon Sep 17 00:00:00 2001 From: Tamas Cservenak Date: Sun, 21 Apr 2024 16:16:22 +0200 Subject: [PATCH 4/4] Badabum --- .../model/profile/activation/PropertyProfileActivator.java | 4 ++++ 1 file changed, 4 insertions(+) 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 452ae094932..03c9b1aa7ee 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 @@ -84,6 +84,9 @@ public boolean isActive(Profile profile, ProfileActivationContext context, Model 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); @@ -124,6 +127,7 @@ private String interpolate( 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; } } }