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 2024/10/01 11:02:30 UTC

(maven) branch master updated: Improve model validator wrt modelVersion which is now set in the file model (#1757)

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

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


The following commit(s) were added to refs/heads/master by this push:
     new 0cff3ea22b Improve model validator wrt modelVersion which is now set in the file model (#1757)
0cff3ea22b is described below

commit 0cff3ea22bd8c7368969308304c8eb4ca158a144
Author: Guillaume Nodet <gn...@gmail.com>
AuthorDate: Tue Oct 1 13:02:25 2024 +0200

    Improve model validator wrt modelVersion which is now set in the file model (#1757)
---
 .../internal/impl/model/DefaultModelValidator.java | 30 +++++-----------------
 1 file changed, 6 insertions(+), 24 deletions(-)

diff --git a/maven-api-impl/src/main/java/org/apache/maven/internal/impl/model/DefaultModelValidator.java b/maven-api-impl/src/main/java/org/apache/maven/internal/impl/model/DefaultModelValidator.java
index 65545d1b8e..430ba510f1 100644
--- a/maven-api-impl/src/main/java/org/apache/maven/internal/impl/model/DefaultModelValidator.java
+++ b/maven-api-impl/src/main/java/org/apache/maven/internal/impl/model/DefaultModelValidator.java
@@ -74,8 +74,6 @@ import org.apache.maven.api.services.model.ModelVersionProcessor;
 import org.apache.maven.model.v4.MavenModelVersion;
 import org.apache.maven.model.v4.MavenTransformer;
 
-import static org.apache.maven.internal.impl.model.DefaultModelBuilder.NAMESPACE_PREFIX;
-
 /**
  */
 @Named
@@ -350,6 +348,10 @@ public class DefaultModelValidator implements ModelValidator {
                 }
             }
         } else if (validationLevel >= ModelValidator.VALIDATION_LEVEL_MAVEN_2_0) {
+            validateStringNotEmpty("modelVersion", problems, Severity.ERROR, Version.V20, m.getModelVersion(), m);
+
+            validateModelVersion(problems, m.getModelVersion(), m, VALID_MODEL_VERSIONS);
+
             Set<String> modules = new HashSet<>();
             for (int i = 0, n = m.getModules().size(); i < n; i++) {
                 String module = m.getModules().get(i);
@@ -365,12 +367,6 @@ public class DefaultModelValidator implements ModelValidator {
                 }
             }
             String modelVersion = m.getModelVersion();
-            if (modelVersion == null) {
-                String namespace = m.getNamespaceUri();
-                if (namespace != null && namespace.startsWith(NAMESPACE_PREFIX)) {
-                    modelVersion = namespace.substring(NAMESPACE_PREFIX.length());
-                }
-            }
             if (Objects.equals(modelVersion, ModelBuilder.MODEL_VERSION_4_0_0)) {
                 if (!m.getSubprojects().isEmpty()) {
                     addViolation(
@@ -422,12 +418,6 @@ public class DefaultModelValidator implements ModelValidator {
 
             Severity errOn30 = getSeverity(validationLevel, ModelValidator.VALIDATION_LEVEL_MAVEN_3_0);
 
-            // The file pom may not contain the modelVersion yet, as it may be set later by the
-            // ModelVersionXMLFilter.
-            if (m.getModelVersion() != null && !m.getModelVersion().isEmpty()) {
-                validateModelVersion(problems, m.getModelVersion(), m, VALID_MODEL_VERSIONS);
-            }
-
             validateStringNoExpression("groupId", problems, Severity.WARNING, Version.V20, m.getGroupId(), m);
             if (parent == null) {
                 validateStringNotEmpty("groupId", problems, Severity.FATAL, Version.V20, m.getGroupId(), m);
@@ -562,16 +552,8 @@ public class DefaultModelValidator implements ModelValidator {
     @Override
     public void validateRawModel(
             Model m, int validationLevel, ModelBuilderRequest request, ModelProblemCollector problems) {
-        // [MNG-6074] Maven should produce an error if no model version has been set in a POM file used to build an
-        // effective model.
-        //
-        // As of 3.4, the model version is mandatory even in raw models. The XML element still is optional in the
-        // XML schema and this will not change anytime soon. We do not want to build effective models based on
-        // models without a version starting with 3.4.
-        validateStringNotEmpty("modelVersion", problems, Severity.ERROR, Version.V20, m.getModelVersion(), m);
-
-        validateModelVersion(problems, m.getModelVersion(), m, VALID_MODEL_VERSIONS);
-
+        // Check that the model version is correctly set wrt the model definition, i.e., that the
+        // user does not use an attribute or element that is not available in the modelVersion used.
         String minVersion = new MavenModelVersion().getModelVersion(m);
         if (m.getModelVersion() != null && compareModelVersions(minVersion, m.getModelVersion()) > 0) {
             addViolation(