[MNG-8211] Fail the build if project effective version has expression (#1673)

As this is almost always source of confusion. If feature is used and there is no proper value set, fail the build, as users for sure does not plan to deploy artifacts with version `${revision}` (or any expression in project.version).

Still, to not be disruptive, the old behaviour can be achieved by setting `maven.build.allowExpressionInEffectiveProjectVersion`=true project property.

---

https://issues.apache.org/jira/browse/MNG-8211
This commit is contained in:
Tamas Cservenak 2024-08-22 12:54:24 +02:00 committed by GitHub
parent 20e6f6a8f7
commit 6668da3219
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 36 additions and 0 deletions

View File

@ -78,6 +78,8 @@ import org.apache.maven.model.v4.MavenTransformer;
@Named
@Singleton
public class DefaultModelValidator implements ModelValidator {
public static final String BUILD_ALLOW_EXPRESSION_IN_EFFECTIVE_PROJECT_VERSION =
"maven.build.allowExpressionInEffectiveProjectVersion";
public static final List<String> VALID_MODEL_VERSIONS =
Collections.unmodifiableList(Arrays.asList("4.0.0", "4.1.0"));
@ -699,6 +701,22 @@ public class DefaultModelValidator implements ModelValidator {
validateBannedCharacters(
EMPTY, "version", problems, errOn31, Version.V20, m.getVersion(), null, m, ILLEGAL_VERSION_CHARS);
validate20ProperSnapshotVersion("version", problems, errOn31, Version.V20, m.getVersion(), null, m);
if (hasExpression(m.getVersion())) {
Severity versionExpressionSeverity = Severity.ERROR;
if (m.getProperties() != null
&& Boolean.parseBoolean(
m.getProperties().get(BUILD_ALLOW_EXPRESSION_IN_EFFECTIVE_PROJECT_VERSION))) {
versionExpressionSeverity = Severity.WARNING;
}
addViolation(
problems,
versionExpressionSeverity,
Version.V20,
"version",
null,
"must be a constant version but is '" + m.getVersion() + "'.",
m);
}
Build build = m.getBuild();
if (build != null) {

View File

@ -80,6 +80,8 @@ import org.apache.maven.model.v4.MavenTransformer;
@Named
@Singleton
public class DefaultModelValidator implements ModelValidator {
public static final String BUILD_ALLOW_EXPRESSION_IN_EFFECTIVE_PROJECT_VERSION =
"maven.build.allowExpressionInEffectiveProjectVersion";
public static final List<String> VALID_MODEL_VERSIONS =
Collections.unmodifiableList(Arrays.asList("4.0.0", "4.1.0"));
@ -691,6 +693,22 @@ public class DefaultModelValidator implements ModelValidator {
validateBannedCharacters(
EMPTY, "version", problems, errOn31, Version.V20, m.getVersion(), null, m, ILLEGAL_VERSION_CHARS);
validate20ProperSnapshotVersion("version", problems, errOn31, Version.V20, m.getVersion(), null, m);
if (hasExpression(m.getVersion())) {
Severity versionExpressionSeverity = Severity.ERROR;
if (m.getProperties() != null
&& Boolean.parseBoolean(
m.getProperties().get(BUILD_ALLOW_EXPRESSION_IN_EFFECTIVE_PROJECT_VERSION))) {
versionExpressionSeverity = Severity.WARNING;
}
addViolation(
problems,
versionExpressionSeverity,
Version.V20,
"version",
null,
"must be a constant version but is '" + m.getVersion() + "'.",
m);
}
Build build = m.getBuild();
if (build != null) {