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 5d2360bc82c9..6e40aa2243d9 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 String interpolate(String path, ProfileActivationContext context) throws 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 16d6b57b98d0..c11716b1ef50 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 @@ /** * Determines profile activation based on the existence/absence of some file. - * File name interpolation support is limited to ${basedir} (since Maven 3, - * see MNG-2363), + * File name interpolation support is limited to ${project.basedir} * system properties and user properties. - * ${project.basedir} is intentionally not supported as this form would suggest that other - * ${project.*} 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 e44b18c3df5d..47a92ab967e0 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 @@ 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 void validateRawModel(Model m, ModelBuildingRequest request, ModelProblem 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 void validateRawModel(Model m, ModelBuildingRequest request, ModelProblem } } - 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 7c6081aae198..8a7b4fe8b680 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 void testParentVersionRELEASE() throws Exception { "'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 000000000000..a4beb6238f4a --- /dev/null +++ b/maven-model-builder/src/test/resources/poms/validation/raw-model/profile-activation-file-with-allowed-expressions.xml @@ -0,0 +1,64 @@ + + + + 4.0.0 + aid + gid + 0.1 + pom + + + + exists-basedir + + + ${basedir}/test.txt + + + + + missing-basedir + + + ${basedir}/test.txt + + + + + + exists-project-basedir + + + ${project.basedir}/test.txt + + + + + missing-project-basedir + + + ${project.basedir}/test.txt + + + + + + 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 000000000000..65953c3bb9c1 --- /dev/null +++ b/maven-model-builder/src/test/resources/poms/validation/raw-model/profile-activation-file-with-project-expressions.xml @@ -0,0 +1,48 @@ + + + + 4.0.0 + aid + gid + 0.1 + pom + + + + + exists-project-version + + + ${project.version}/test.txt + + + + + missing-project-version + + + ${project.version}/test.txt + + + + + + diff --git a/maven-model/src/main/mdo/maven.mdo b/maven-model/src/main/mdo/maven.mdo index 2bd344e1bf38..6e33f192fd9c 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, exists will test for the existence of the file and if it is there, the profile will be activated.
- Variable interpolation for these file specifications is limited to ${basedir}, + Variable interpolation for these file specifications is limited to ${project.basedir}, system properties and user properties.]]>