Skip to content

Commit c482de8

Browse files
authored
[MNG-7851] Improve error message when modelVersion is 4.0 (#1210)
Improve DefaultModelValidator.compareModelVersions to be able to compare versions which different numbers of segments, allowing it to compare "4", "4.0", and "4.0.0" to each other for example. Signed-off-by: Craig Andrews <[email protected]>
1 parent 67b8c07 commit c482de8

File tree

3 files changed

+38
-6
lines changed

3 files changed

+38
-6
lines changed

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

+4-6
Original file line numberDiff line numberDiff line change
@@ -1449,16 +1449,14 @@ private static int compareModelVersions(String first, String second) {
14491449
// we use a dedicated comparator because we control our model version scheme.
14501450
String[] firstSegments = StringUtils.split(first, ".");
14511451
String[] secondSegments = StringUtils.split(second, ".");
1452-
for (int i = 0; i < Math.min(firstSegments.length, secondSegments.length); i++) {
1453-
int result = Long.valueOf(firstSegments[i]).compareTo(Long.valueOf(secondSegments[i]));
1452+
for (int i = 0; i < Math.max(firstSegments.length, secondSegments.length); i++) {
1453+
int result = Long.valueOf(i < firstSegments.length ? firstSegments[i] : "0")
1454+
.compareTo(Long.valueOf(i < secondSegments.length ? secondSegments[i] : "0"));
14541455
if (result != 0) {
14551456
return result;
14561457
}
14571458
}
1458-
if (firstSegments.length == secondSegments.length) {
1459-
return 0;
1460-
}
1461-
return firstSegments.length > secondSegments.length ? -1 : 1;
1459+
return 0;
14621460
}
14631461

14641462
@SuppressWarnings("checkstyle:parameternumber")

maven-model-builder/src/test/java/org/apache/maven/model/validation/DefaultModelValidatorTest.java

+9
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,15 @@ public void testBadModelVersion() throws Exception {
117117
assertTrue(result.getFatals().get(0).contains("modelVersion"));
118118
}
119119

120+
public void testModelVersion40() throws Exception {
121+
SimpleProblemCollector result =
122+
validateRaw("modelVersion-4_0.xml", ModelBuildingRequest.VALIDATION_LEVEL_STRICT);
123+
124+
assertViolations(result, 0, 1, 0);
125+
126+
assertTrue(result.getErrors().get(0).contains("'modelVersion' must be one of"));
127+
}
128+
120129
public void testMissingArtifactId() throws Exception {
121130
SimpleProblemCollector result = validate("missing-artifactId-pom.xml");
122131

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<!--
2+
Licensed to the Apache Software Foundation (ASF) under one
3+
or more contributor license agreements. See the NOTICE file
4+
distributed with this work for additional information
5+
regarding copyright ownership. The ASF licenses this file
6+
to you under the Apache License, Version 2.0 (the
7+
"License"); you may not use this file except in compliance
8+
with the License. You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing,
13+
software distributed under the License is distributed on an
14+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
KIND, either express or implied. See the License for the
16+
specific language governing permissions and limitations
17+
under the License.
18+
-->
19+
20+
<project>
21+
<modelVersion>4.0</modelVersion>
22+
<groupId>foo</groupId>
23+
<artifactId>bar</artifactId>
24+
<version>0.1</version>
25+
</project>

0 commit comments

Comments
 (0)