You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by gn...@apache.org on 2023/08/29 10:06:18 UTC

[maven] branch maven-3.9.x updated: [MNG-7851] Improve error message when modelVersion is 4.0 (#1210)

This is an automated email from the ASF dual-hosted git repository.

gnodet pushed a commit to branch maven-3.9.x
in repository https://gitbox.apache.org/repos/asf/maven.git


The following commit(s) were added to refs/heads/maven-3.9.x by this push:
     new c482de86c1 [MNG-7851] Improve error message when modelVersion is 4.0 (#1210)
c482de86c1 is described below

commit c482de86c11919e7ccf66ddde6101a290958bb36
Author: Craig Andrews <ca...@integralblue.com>
AuthorDate: Tue Aug 29 06:06:11 2023 -0400

    [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 <ca...@integralblue.com>
---
 .../model/validation/DefaultModelValidator.java    | 10 ++++-----
 .../validation/DefaultModelValidatorTest.java      |  9 ++++++++
 .../resources/poms/validation/modelVersion-4_0.xml | 25 ++++++++++++++++++++++
 3 files changed, 38 insertions(+), 6 deletions(-)

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 7049f5c211..e44b18c3df 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
@@ -1449,16 +1449,14 @@ public class DefaultModelValidator implements ModelValidator {
         // we use a dedicated comparator because we control our model version scheme.
         String[] firstSegments = StringUtils.split(first, ".");
         String[] secondSegments = StringUtils.split(second, ".");
-        for (int i = 0; i < Math.min(firstSegments.length, secondSegments.length); i++) {
-            int result = Long.valueOf(firstSegments[i]).compareTo(Long.valueOf(secondSegments[i]));
+        for (int i = 0; i < Math.max(firstSegments.length, secondSegments.length); i++) {
+            int result = Long.valueOf(i < firstSegments.length ? firstSegments[i] : "0")
+                    .compareTo(Long.valueOf(i < secondSegments.length ? secondSegments[i] : "0"));
             if (result != 0) {
                 return result;
             }
         }
-        if (firstSegments.length == secondSegments.length) {
-            return 0;
-        }
-        return firstSegments.length > secondSegments.length ? -1 : 1;
+        return 0;
     }
 
     @SuppressWarnings("checkstyle:parameternumber")
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 e1419f4671..7c6081aae1 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
@@ -117,6 +117,15 @@ public class DefaultModelValidatorTest extends TestCase {
         assertTrue(result.getFatals().get(0).contains("modelVersion"));
     }
 
+    public void testModelVersion40() throws Exception {
+        SimpleProblemCollector result =
+                validateRaw("modelVersion-4_0.xml", ModelBuildingRequest.VALIDATION_LEVEL_STRICT);
+
+        assertViolations(result, 0, 1, 0);
+
+        assertTrue(result.getErrors().get(0).contains("'modelVersion' must be one of"));
+    }
+
     public void testMissingArtifactId() throws Exception {
         SimpleProblemCollector result = validate("missing-artifactId-pom.xml");
 
diff --git a/maven-model-builder/src/test/resources/poms/validation/modelVersion-4_0.xml b/maven-model-builder/src/test/resources/poms/validation/modelVersion-4_0.xml
new file mode 100644
index 0000000000..544331b1f1
--- /dev/null
+++ b/maven-model-builder/src/test/resources/poms/validation/modelVersion-4_0.xml
@@ -0,0 +1,25 @@
+<!--
+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>
+  <modelVersion>4.0</modelVersion>
+  <groupId>foo</groupId>
+  <artifactId>bar</artifactId>
+  <version>0.1</version>
+</project>