Skip to content

Commit 6668da3

Browse files
authored
[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
1 parent 20e6f6a commit 6668da3

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

maven-api-impl/src/main/java/org/apache/maven/internal/impl/model/DefaultModelValidator.java

+18
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@
7878
@Named
7979
@Singleton
8080
public class DefaultModelValidator implements ModelValidator {
81+
public static final String BUILD_ALLOW_EXPRESSION_IN_EFFECTIVE_PROJECT_VERSION =
82+
"maven.build.allowExpressionInEffectiveProjectVersion";
8183

8284
public static final List<String> VALID_MODEL_VERSIONS =
8385
Collections.unmodifiableList(Arrays.asList("4.0.0", "4.1.0"));
@@ -699,6 +701,22 @@ public void validateEffectiveModel(Model m, ModelBuilderRequest request, ModelPr
699701
validateBannedCharacters(
700702
EMPTY, "version", problems, errOn31, Version.V20, m.getVersion(), null, m, ILLEGAL_VERSION_CHARS);
701703
validate20ProperSnapshotVersion("version", problems, errOn31, Version.V20, m.getVersion(), null, m);
704+
if (hasExpression(m.getVersion())) {
705+
Severity versionExpressionSeverity = Severity.ERROR;
706+
if (m.getProperties() != null
707+
&& Boolean.parseBoolean(
708+
m.getProperties().get(BUILD_ALLOW_EXPRESSION_IN_EFFECTIVE_PROJECT_VERSION))) {
709+
versionExpressionSeverity = Severity.WARNING;
710+
}
711+
addViolation(
712+
problems,
713+
versionExpressionSeverity,
714+
Version.V20,
715+
"version",
716+
null,
717+
"must be a constant version but is '" + m.getVersion() + "'.",
718+
m);
719+
}
702720

703721
Build build = m.getBuild();
704722
if (build != null) {

maven-model-builder/src/main/java/org/apache/maven/model/validation/DefaultModelValidator.java

+18
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@
8080
@Named
8181
@Singleton
8282
public class DefaultModelValidator implements ModelValidator {
83+
public static final String BUILD_ALLOW_EXPRESSION_IN_EFFECTIVE_PROJECT_VERSION =
84+
"maven.build.allowExpressionInEffectiveProjectVersion";
8385

8486
public static final List<String> VALID_MODEL_VERSIONS =
8587
Collections.unmodifiableList(Arrays.asList("4.0.0", "4.1.0"));
@@ -691,6 +693,22 @@ public void validateEffectiveModel(Model ma, ModelBuildingRequest request, Model
691693
validateBannedCharacters(
692694
EMPTY, "version", problems, errOn31, Version.V20, m.getVersion(), null, m, ILLEGAL_VERSION_CHARS);
693695
validate20ProperSnapshotVersion("version", problems, errOn31, Version.V20, m.getVersion(), null, m);
696+
if (hasExpression(m.getVersion())) {
697+
Severity versionExpressionSeverity = Severity.ERROR;
698+
if (m.getProperties() != null
699+
&& Boolean.parseBoolean(
700+
m.getProperties().get(BUILD_ALLOW_EXPRESSION_IN_EFFECTIVE_PROJECT_VERSION))) {
701+
versionExpressionSeverity = Severity.WARNING;
702+
}
703+
addViolation(
704+
problems,
705+
versionExpressionSeverity,
706+
Version.V20,
707+
"version",
708+
null,
709+
"must be a constant version but is '" + m.getVersion() + "'.",
710+
m);
711+
}
694712

695713
Build build = m.getBuild();
696714
if (build != null) {

0 commit comments

Comments
 (0)