You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by sj...@apache.org on 2023/09/26 19:18:53 UTC

[maven] branch MNG-7895-3.9.x updated (34cfc3abbb -> dc2c177bc7)

This is an automated email from the ASF dual-hosted git repository.

sjaranowski pushed a change to branch MNG-7895-3.9.x
in repository https://gitbox.apache.org/repos/asf/maven.git


 discard 34cfc3abbb [MNG-7895] Support ${project.basedir} in file profile activation
     new dc2c177bc7 [MNG-7895] Support ${project.basedir} in file profile activation

This update added new revisions after undoing existing revisions.
That is to say, some revisions that were in the old version of the
branch are not in the new version.  This situation occurs
when a user --force pushes a change and generates a repository
containing something like this:

 * -- * -- B -- O -- O -- O   (34cfc3abbb)
            \
             N -- N -- N   refs/heads/MNG-7895-3.9.x (dc2c177bc7)

You should already have received notification emails for all of the O
revisions, and so the following emails describe only the N revisions
from the common base, B.

Any revisions marked "omit" are not gone; other references still
refer to them.  Any revisions marked "discard" are gone forever.

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:


[maven] 01/01: [MNG-7895] Support ${project.basedir} in file profile activation

Posted by sj...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

sjaranowski pushed a commit to branch MNG-7895-3.9.x
in repository https://gitbox.apache.org/repos/asf/maven.git

commit dc2c177bc7582f9028a37d8b9330fce3e580f019
Author: Slawomir Jaranowski <s....@gmail.com>
AuthorDate: Sun Sep 24 21:44:27 2023 +0200

    [MNG-7895] Support ${project.basedir} in file profile activation
    
    (cherry picked from commit 5c2e671a06721fe07cf414a9377a63652d512be0)
---
 .../ProfileActivationFilePathInterpolator.java     |  6 +-
 .../profile/activation/FileProfileActivator.java   |  5 +-
 .../model/validation/DefaultModelValidator.java    | 73 +++++++++-------------
 .../validation/DefaultModelValidatorTest.java      | 22 +++++++
 ...le-activation-file-with-allowed-expressions.xml | 64 +++++++++++++++++++
 ...le-activation-file-with-project-expressions.xml | 48 ++++++++++++++
 maven-model/src/main/mdo/maven.mdo                 |  2 +-
 7 files changed, 167 insertions(+), 53 deletions(-)

diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/path/ProfileActivationFilePathInterpolator.java b/maven-model-builder/src/main/java/org/apache/maven/model/path/ProfileActivationFilePathInterpolator.java
index 5d2360bc82..6e40aa2243 100644
--- a/maven-model-builder/src/main/java/org/apache/maven/model/path/ProfileActivationFilePathInterpolator.java
+++ b/maven-model-builder/src/main/java/org/apache/maven/model/path/ProfileActivationFilePathInterpolator.java
@@ -66,11 +66,7 @@ public class ProfileActivationFilePathInterpolator {
             interpolator.addValueSource(new AbstractValueSource(false) {
                 @Override
                 public Object getValue(String expression) {
-                    /*
-                     * We intentionally only support ${basedir} and not ${project.basedir} as the latter form
-                     * would suggest that other project.* expressions can be used which is beyond the design.
-                     */
-                    if ("basedir".equals(expression)) {
+                    if ("basedir".equals(expression) || "project.basedir".equals(expression)) {
                         return basedir.getAbsolutePath();
                     }
                     return null;
diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/profile/activation/FileProfileActivator.java b/maven-model-builder/src/main/java/org/apache/maven/model/profile/activation/FileProfileActivator.java
index 16d6b57b98..c11716b1ef 100644
--- a/maven-model-builder/src/main/java/org/apache/maven/model/profile/activation/FileProfileActivator.java
+++ b/maven-model-builder/src/main/java/org/apache/maven/model/profile/activation/FileProfileActivator.java
@@ -38,11 +38,8 @@ import org.codehaus.plexus.util.StringUtils;
 
 /**
  * Determines profile activation based on the existence/absence of some file.
- * File name interpolation support is limited to <code>${basedir}</code> (since Maven 3,
- * see <a href="https://issues.apache.org/jira/browse/MNG-2363">MNG-2363</a>),
+ * File name interpolation support is limited to <code>${project.basedir}</code>
  * system properties and user properties.
- * <code>${project.basedir}</code> is intentionally not supported as this form would suggest that other
- * <code>${project.*}</code> expressions can be used, which is however beyond the design.
  *
  * @author Benjamin Bentmann
  * @see ActivationFile
diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/validation/DefaultModelValidator.java b/maven-model-builder/src/main/java/org/apache/maven/model/validation/DefaultModelValidator.java
index e44b18c3df..47a92ab967 100644
--- a/maven-model-builder/src/main/java/org/apache/maven/model/validation/DefaultModelValidator.java
+++ b/maven-model-builder/src/main/java/org/apache/maven/model/validation/DefaultModelValidator.java
@@ -69,6 +69,7 @@ import org.codehaus.plexus.util.StringUtils;
 public class DefaultModelValidator implements ModelValidator {
 
     private static final Pattern CI_FRIENDLY_EXPRESSION = Pattern.compile("\\$\\{(.+?)}");
+    private static final Pattern EXPRESSION_PROJECT_NAME_PATTERN = Pattern.compile("\\$\\{(project.+?)}");
 
     private static final String ILLEGAL_FS_CHARS = "\\/:\"<>|?*";
 
@@ -196,8 +197,7 @@ public class DefaultModelValidator implements ModelValidator {
                             profile);
                 }
 
-                validate30RawProfileActivation(
-                        problems, profile.getActivation(), profile.getId(), prefix, "activation", request);
+                validate30RawProfileActivation(problems, profile.getActivation(), prefix);
 
                 validate20RawDependencies(
                         problems, profile.getDependencies(), prefix, "dependencies.dependency.", request);
@@ -235,54 +235,41 @@ public class DefaultModelValidator implements ModelValidator {
         }
     }
 
-    private void validate30RawProfileActivation(
-            ModelProblemCollector problems,
-            Activation activation,
-            String sourceHint,
-            String prefix,
-            String fieldName,
-            ModelBuildingRequest request) {
-        if (activation == null) {
+    private void validate30RawProfileActivation(ModelProblemCollector problems, Activation activation, String prefix) {
+        if (activation == null || activation.getFile() == null) {
             return;
         }
 
         ActivationFile file = activation.getFile();
 
-        if (file != null) {
-            String path;
-            boolean missing;
+        String path;
+        String location;
 
-            if (StringUtils.isNotEmpty(file.getExists())) {
-                path = file.getExists();
-                missing = false;
-            } else if (StringUtils.isNotEmpty(file.getMissing())) {
-                path = file.getMissing();
-                missing = true;
-            } else {
-                return;
-            }
+        if (file.getExists() != null && !file.getExists().isEmpty()) {
+            path = file.getExists();
+            location = "exists";
+        } else if (file.getMissing() != null && !file.getMissing().isEmpty()) {
+            path = file.getMissing();
+            location = "missing";
+        } else {
+            return;
+        }
 
-            if (path.contains("${project.basedir}")) {
-                addViolation(
-                        problems,
-                        Severity.WARNING,
-                        Version.V30,
-                        prefix + fieldName + (missing ? ".file.missing" : ".file.exists"),
-                        null,
-                        "Failed to interpolate file location " + path + " for profile " + sourceHint
-                                + ": ${project.basedir} expression not supported during profile activation, "
-                                + "use ${basedir} instead",
-                        file.getLocation(missing ? "missing" : "exists"));
-            } else if (hasProjectExpression(path)) {
-                addViolation(
-                        problems,
-                        Severity.WARNING,
-                        Version.V30,
-                        prefix + fieldName + (missing ? ".file.missing" : ".file.exists"),
-                        null,
-                        "Failed to interpolate file location " + path + " for profile " + sourceHint
-                                + ": ${project.*} expressions are not supported during profile activation",
-                        file.getLocation(missing ? "missing" : "exists"));
+        if (hasProjectExpression(path)) {
+            Matcher matcher = EXPRESSION_PROJECT_NAME_PATTERN.matcher(path);
+            while (matcher.find()) {
+                String propertyName = matcher.group(0);
+                if (!"${project.basedir}".equals(propertyName)) {
+                    addViolation(
+                            problems,
+                            Severity.WARNING,
+                            Version.V30,
+                            prefix + "activation.file." + location,
+                            null,
+                            "Failed to interpolate file location " + path + ": " + propertyName
+                                    + " expressions are not supported during profile activation.",
+                            file.getLocation(location));
+                }
             }
         }
     }
diff --git a/maven-model-builder/src/test/java/org/apache/maven/model/validation/DefaultModelValidatorTest.java b/maven-model-builder/src/test/java/org/apache/maven/model/validation/DefaultModelValidatorTest.java
index 7c6081aae1..8a7b4fe8b6 100644
--- a/maven-model-builder/src/test/java/org/apache/maven/model/validation/DefaultModelValidatorTest.java
+++ b/maven-model-builder/src/test/java/org/apache/maven/model/validation/DefaultModelValidatorTest.java
@@ -747,4 +747,26 @@ public class DefaultModelValidatorTest extends TestCase {
                 "'parent.version' is either LATEST or RELEASE (both of them are being deprecated)",
                 result.getWarnings().get(0));
     }
+
+    public void testProfileActivationWithAllowedExpression() throws Exception {
+        SimpleProblemCollector result = validateRaw("raw-model/profile-activation-file-with-allowed-expressions.xml");
+        assertViolations(result, 0, 0, 0);
+    }
+
+    public void testProfileActivationWithProjectExpression() throws Exception {
+        SimpleProblemCollector result = validateRaw("raw-model/profile-activation-file-with-project-expressions.xml");
+        assertViolations(result, 0, 0, 2);
+
+        assertEquals(
+                "'profiles.profile[exists-project-version].activation.file.exists' "
+                        + "Failed to interpolate file location ${project.version}/test.txt: "
+                        + "${project.version} expressions are not supported during profile activation.",
+                result.getWarnings().get(0));
+
+        assertEquals(
+                "'profiles.profile[missing-project-version].activation.file.missing' "
+                        + "Failed to interpolate file location ${project.version}/test.txt: "
+                        + "${project.version} expressions are not supported during profile activation.",
+                result.getWarnings().get(1));
+    }
 }
diff --git a/maven-model-builder/src/test/resources/poms/validation/raw-model/profile-activation-file-with-allowed-expressions.xml b/maven-model-builder/src/test/resources/poms/validation/raw-model/profile-activation-file-with-allowed-expressions.xml
new file mode 100644
index 0000000000..a4beb6238f
--- /dev/null
+++ b/maven-model-builder/src/test/resources/poms/validation/raw-model/profile-activation-file-with-allowed-expressions.xml
@@ -0,0 +1,64 @@
+<!--
+Licensed to the Apache Software Foundation (ASF) under one
+or more contributor license agreements.  See the NOTICE file
+distributed with this work for additional information
+regarding copyright ownership.  The ASF licenses this file
+to you under the Apache License, Version 2.0 (the
+"License"); you may not use this file except in compliance
+with the License.  You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing,
+software distributed under the License is distributed on an
+"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+KIND, either express or implied.  See the License for the
+specific language governing permissions and limitations
+under the License.
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
+
+  <modelVersion>4.0.0</modelVersion>
+  <artifactId>aid</artifactId>
+  <groupId>gid</groupId>
+  <version>0.1</version>
+  <packaging>pom</packaging>
+
+  <profiles>
+    <profile>
+      <id>exists-basedir</id>
+      <activation>
+        <file>
+          <exists>${basedir}/test.txt</exists>
+        </file>
+      </activation>
+    </profile>
+    <profile>
+      <id>missing-basedir</id>
+      <activation>
+        <file>
+          <missing>${basedir}/test.txt</missing>
+        </file>
+      </activation>
+    </profile>
+
+    <profile>
+      <id>exists-project-basedir</id>
+      <activation>
+        <file>
+          <exists>${project.basedir}/test.txt</exists>
+        </file>
+      </activation>
+    </profile>
+    <profile>
+      <id>missing-project-basedir</id>
+      <activation>
+        <file>
+          <missing>${project.basedir}/test.txt</missing>
+        </file>
+      </activation>
+    </profile>
+
+  </profiles>
+</project>
diff --git a/maven-model-builder/src/test/resources/poms/validation/raw-model/profile-activation-file-with-project-expressions.xml b/maven-model-builder/src/test/resources/poms/validation/raw-model/profile-activation-file-with-project-expressions.xml
new file mode 100644
index 0000000000..65953c3bb9
--- /dev/null
+++ b/maven-model-builder/src/test/resources/poms/validation/raw-model/profile-activation-file-with-project-expressions.xml
@@ -0,0 +1,48 @@
+<!--
+Licensed to the Apache Software Foundation (ASF) under one
+or more contributor license agreements.  See the NOTICE file
+distributed with this work for additional information
+regarding copyright ownership.  The ASF licenses this file
+to you under the Apache License, Version 2.0 (the
+"License"); you may not use this file except in compliance
+with the License.  You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing,
+software distributed under the License is distributed on an
+"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+KIND, either express or implied.  See the License for the
+specific language governing permissions and limitations
+under the License.
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
+
+  <modelVersion>4.0.0</modelVersion>
+  <artifactId>aid</artifactId>
+  <groupId>gid</groupId>
+  <version>0.1</version>
+  <packaging>pom</packaging>
+
+  <profiles>
+
+    <profile>
+      <id>exists-project-version</id>
+      <activation>
+        <file>
+          <exists>${project.version}/test.txt</exists>
+        </file>
+      </activation>
+    </profile>
+    <profile>
+      <id>missing-project-version</id>
+      <activation>
+        <file>
+          <missing>${project.version}/test.txt</missing>
+        </file>
+      </activation>
+    </profile>
+
+  </profiles>
+</project>
diff --git a/maven-model/src/main/mdo/maven.mdo b/maven-model/src/main/mdo/maven.mdo
index 2bd344e1bf..6e33f192fd 100644
--- a/maven-model/src/main/mdo/maven.mdo
+++ b/maven-model/src/main/mdo/maven.mdo
@@ -2934,7 +2934,7 @@
         is the location of a file that needs to exist, and if it doesn't, the profile will be
         activated. On the other hand, <code>exists</code> will test for the existence of the file and if it is
         there, the profile will be activated.<br>
-        Variable interpolation for these file specifications is limited to <code>${basedir}</code>,
+        Variable interpolation for these file specifications is limited to <code>${project.basedir}</code>,
         system properties and user properties.]]></description>
       <fields>
         <field>