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

This commit is contained in:
Slawomir Jaranowski 2023-09-24 21:44:27 +02:00
parent 558dfc9c6e
commit 5c2e671a06
7 changed files with 169 additions and 53 deletions

View File

@ -3007,7 +3007,7 @@
is the location of a file that needs to exist, and if it doesn't, the profile will be 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} will test for the existence of the file and if it is activated. On the other hand, {@code exists} will test for the existence of the file and if it is
there, the profile will be activated.<br> there, the profile will be activated.<br>
Variable interpolation for these file specifications is limited to {@code ${basedir}}, Variable interpolation for these file specifications is limited to {@code ${project.basedir}},
system properties and user properties.]]></description> system properties and user properties.]]></description>
<fields> <fields>
<field> <field>

View File

@ -69,11 +69,7 @@ public class ProfileActivationFilePathInterpolator {
interpolator.addValueSource(new AbstractValueSource(false) { interpolator.addValueSource(new AbstractValueSource(false) {
@Override @Override
public Object getValue(String expression) { public Object getValue(String expression) {
/* if ("basedir".equals(expression) || "project.basedir".equals(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)) {
return basedir.getAbsolutePath(); return basedir.getAbsolutePath();
} }
return null; return null;

View File

@ -37,11 +37,8 @@ import org.codehaus.plexus.interpolation.InterpolationException;
/** /**
* Determines profile activation based on the existence/absence of some file. * Determines profile activation based on the existence/absence of some file.
* File name interpolation support is limited to <code>${basedir}</code> (since Maven 3, * File name interpolation support is limited to <code>${project.basedir}</code>
* see <a href="https://issues.apache.org/jira/browse/MNG-2363">MNG-2363</a>),
* system properties and user properties. * 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.
* *
* @see ActivationFile * @see ActivationFile
* @see org.apache.maven.model.validation.DefaultModelValidator#validateRawModel * @see org.apache.maven.model.validation.DefaultModelValidator#validateRawModel

View File

@ -68,6 +68,7 @@ import org.apache.maven.model.v4.MavenModelVersion;
public class DefaultModelValidator implements ModelValidator { public class DefaultModelValidator implements ModelValidator {
private static final Pattern EXPRESSION_NAME_PATTERN = Pattern.compile("\\$\\{(.+?)}"); private static final Pattern EXPRESSION_NAME_PATTERN = Pattern.compile("\\$\\{(.+?)}");
private static final Pattern EXPRESSION_PROJECT_NAME_PATTERN = Pattern.compile("\\$\\{(project.+?)}");
private static final String ILLEGAL_FS_CHARS = "\\/:\"<>|?*"; private static final String ILLEGAL_FS_CHARS = "\\/:\"<>|?*";
@ -210,8 +211,7 @@ public class DefaultModelValidator implements ModelValidator {
profile); profile);
} }
validate30RawProfileActivation( validate30RawProfileActivation(problems, profile.getActivation(), prefix);
problems, profile.getActivation(), profile.getId(), prefix, "activation", request);
validate20RawDependencies( validate20RawDependencies(
problems, profile.getDependencies(), prefix, "dependencies.dependency.", request); problems, profile.getDependencies(), prefix, "dependencies.dependency.", request);
@ -283,54 +283,41 @@ public class DefaultModelValidator implements ModelValidator {
} }
} }
private void validate30RawProfileActivation( private void validate30RawProfileActivation(ModelProblemCollector problems, Activation activation, String prefix) {
ModelProblemCollector problems, if (activation == null || activation.getFile() == null) {
Activation activation,
String sourceHint,
String prefix,
String fieldName,
ModelBuildingRequest request) {
if (activation == null) {
return; return;
} }
ActivationFile file = activation.getFile(); ActivationFile file = activation.getFile();
if (file != null) { String path;
String path; String location;
boolean missing;
if (file.getExists() != null && !file.getExists().isEmpty()) { if (file.getExists() != null && !file.getExists().isEmpty()) {
path = file.getExists(); path = file.getExists();
missing = false; location = "exists";
} else if (file.getMissing() != null && !file.getMissing().isEmpty()) { } else if (file.getMissing() != null && !file.getMissing().isEmpty()) {
path = file.getMissing(); path = file.getMissing();
missing = true; location = "missing";
} else { } else {
return; return;
} }
if (path.contains("${project.basedir}")) { if (hasProjectExpression(path)) {
addViolation( Matcher matcher = EXPRESSION_PROJECT_NAME_PATTERN.matcher(path);
problems, while (matcher.find()) {
Severity.WARNING, String propertyName = matcher.group(0);
Version.V30, if (!"${project.basedir}".equals(propertyName)) {
prefix + fieldName + (missing ? ".file.missing" : ".file.exists"), addViolation(
null, problems,
"Failed to interpolate file location " + path + " for profile " + sourceHint Severity.WARNING,
+ ": ${project.basedir} expression not supported during profile activation, " Version.V30,
+ "use ${basedir} instead", prefix + "activation.file." + location,
file.getLocation(missing ? "missing" : "exists")); null,
} else if (hasProjectExpression(path)) { "Failed to interpolate file location " + path + ": " + propertyName
addViolation( + " expressions are not supported during profile activation.",
problems, file.getLocation(location));
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"));
} }
} }
} }

View File

@ -814,4 +814,28 @@ class DefaultModelValidatorTest {
SimpleProblemCollector result = validateRaw("raw-model/repository-with-basedir-expression.xml"); SimpleProblemCollector result = validateRaw("raw-model/repository-with-basedir-expression.xml");
assertViolations(result, 0, 0, 0); assertViolations(result, 0, 0, 0);
} }
@Test
void profileActivationWithAllowedExpression() throws Exception {
SimpleProblemCollector result = validateRaw("raw-model/profile-activation-file-with-allowed-expressions.xml");
assertViolations(result, 0, 0, 0);
}
@Test
void profileActivationWithProjectExpression() 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));
}
} }

View File

@ -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>

View File

@ -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>