This is an automated email from the ASF dual-hosted git repository. michaelo pushed a commit to branch maven-3.8.x in repository https://gitbox.apache.org/repos/asf/maven.git
commit 9fa71ff3cd0ac88317a39405fc250a146b6d3270 Author: Slawomir Jaranowski <[email protected]> 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 | 7 +- .../model/validation/DefaultModelValidator.java | 76 ++++++++++------------ .../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 | 4 +- 7 files changed, 175 insertions(+), 52 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 c2f815b7f7..10694485a6 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 @@ -73,11 +73,7 @@ public class ProfileActivationFilePathInterpolator @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(); } 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 923ffd2eb3..05dceffff7 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 @@ -39,11 +39,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>), - * System properties and request 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. + * File name interpolation support is limited to <code>${project.basedir}</code>, + * system properties and user properties. * * @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 b19c707843..4305971826 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 @@ -72,6 +72,7 @@ public class DefaultModelValidator { 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 = "\\/:\"<>|?*"; @@ -192,8 +193,7 @@ public class DefaultModelValidator "must be unique but found duplicate profile with id " + profile.getId(), profile ); } - validate30RawProfileActivation( problems, profile.getActivation(), profile.getId(), - prefix, "activation", request ); + validate30RawProfileActivation( problems, profile.getActivation(), prefix ); validate20RawDependencies( problems, profile.getDependencies(), prefix, "dependencies.dependency.", request ); @@ -226,53 +226,49 @@ public class DefaultModelValidator } } - private void validate30RawProfileActivation( ModelProblemCollector problems, Activation activation, - String sourceHint, String prefix, String fieldName, - ModelBuildingRequest request ) + private void validate30RawProfileActivation( ModelProblemCollector problems, Activation activation, String prefix ) { - if ( activation == null ) - { + 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 ( StringUtils.isNotEmpty( file.getExists() ) ) + { + path = file.getExists(); + location = "exists"; + } + else if ( StringUtils.isNotEmpty( file.getMissing() ) ) + { + 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 ) ) + if ( hasProjectExpression( path ) ) + { + Matcher matcher = EXPRESSION_PROJECT_NAME_PATTERN.matcher( path ); + while ( matcher.find() ) { - 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" ) ); + 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 c5f92c7a14..529e84ce9a 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 @@ -815,4 +815,26 @@ public class DefaultModelValidatorTest assertViolations( result, 0, 0, 1 ); assertEquals( "'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 5d65ef9974..dd4e12b7df 100644 --- a/maven-model/src/main/mdo/maven.mdo +++ b/maven-model/src/main/mdo/maven.mdo @@ -2934,8 +2934,8 @@ 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>, - System properties and request properties.]]></description> + Variable interpolation for these file specifications is limited to <code>${project.basedir}</code>, + system properties and user properties.]]></description> <fields> <field> <name>missing</name>
