From 251a316962e0cd9e4e582960026343eda947d3c0 Mon Sep 17 00:00:00 2001 From: Guillaume Nodet Date: Wed, 18 Oct 2023 15:31:36 +0200 Subject: [PATCH] [MNG-7893] Fix exception loading superpom with invalid modelVersion (#1290) Even though this should not be supported, Maven 3.x is always using the 4.0.0 superpom. So if the version is not a supported model version, simply use 4.0.0. Any invalid value will be later verified by the model validator and eventually throw a meaninful exception. --- .../model/building/DefaultModelBuilder.java | 7 ++++++ .../validation/DefaultModelValidator.java | 24 ++++++++++--------- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuilder.java b/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuilder.java index ffde12ea9e..c8ba3ca319 100644 --- a/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuilder.java +++ b/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuilder.java @@ -87,6 +87,7 @@ import org.apache.maven.model.resolution.ModelResolver; import org.apache.maven.model.resolution.UnresolvableModelException; import org.apache.maven.model.resolution.WorkspaceModelResolver; import org.apache.maven.model.superpom.SuperPomProvider; +import org.apache.maven.model.validation.DefaultModelValidator; import org.apache.maven.model.validation.ModelValidator; import org.codehaus.plexus.interpolation.InterpolationException; import org.codehaus.plexus.interpolation.MapBasedValueSource; @@ -747,6 +748,12 @@ public class DefaultModelBuilder implements ModelBuilder { ModelData resultData = new ModelData(request.getModelSource(), inputModel); String superModelVersion = inputModel.getModelVersion() != null ? inputModel.getModelVersion() : "4.0.0"; + if (!DefaultModelValidator.VALID_MODEL_VERSIONS.contains(superModelVersion)) { + // Maven 3.x is always using 4.0.0 version to load the supermodel, so + // do the same when loading a dependency. The model validator will also + // check that field later. + superModelVersion = "4.0.0"; + } ModelData superData = new ModelData(null, getSuperModel(superModelVersion)); // profile activation 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 98aa018719..25f4328cca 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 @@ -24,6 +24,7 @@ import javax.inject.Singleton; import java.io.File; import java.util.Arrays; +import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.List; @@ -67,6 +68,9 @@ import org.apache.maven.model.v4.MavenModelVersion; @Singleton public class DefaultModelValidator implements ModelValidator { + public static final List VALID_MODEL_VERSIONS = + Collections.unmodifiableList(Arrays.asList("4.0.0", "4.1.0")); + private static final Pattern EXPRESSION_NAME_PATTERN = Pattern.compile("\\$\\{(.+?)}"); private static final Pattern EXPRESSION_PROJECT_NAME_PATTERN = Pattern.compile("\\$\\{(project.+?)}"); @@ -147,7 +151,7 @@ public class DefaultModelValidator implements ModelValidator { // 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, "4.0.0", "4.1.0"); + validateModelVersion(problems, m.getModelVersion(), m, VALID_MODEL_VERSIONS); } validateStringNoExpression("groupId", problems, Severity.WARNING, Version.V20, m.getGroupId(), m); @@ -261,7 +265,7 @@ public class DefaultModelValidator implements ModelValidator { // models without a version starting with 3.4. validateStringNotEmpty("modelVersion", problems, Severity.ERROR, Version.V20, m.getModelVersion(), m); - validateModelVersion(problems, m.getModelVersion(), m, "4.0.0", "4.1.0"); + validateModelVersion(problems, m.getModelVersion(), m, VALID_MODEL_VERSIONS); String minVersion = new MavenModelVersion().getModelVersion(m); if (m.getModelVersion() != null && compareModelVersions(minVersion, m.getModelVersion()) > 0) { @@ -1478,14 +1482,12 @@ public class DefaultModelValidator implements ModelValidator { @SuppressWarnings("checkstyle:parameternumber") private boolean validateModelVersion( - ModelProblemCollector problems, String string, InputLocationTracker tracker, String... validVersions) { + ModelProblemCollector problems, String string, InputLocationTracker tracker, List validVersions) { if (string == null || string.length() <= 0) { return true; } - List values = Arrays.asList(validVersions); - - if (values.contains(string)) { + if (validVersions.contains(string)) { return true; } @@ -1504,8 +1506,8 @@ public class DefaultModelValidator implements ModelValidator { Version.V20, "modelVersion", null, - "of '" + string + "' is newer than the versions supported by this version of Maven: " + values - + ". Building this project requires a newer version of Maven.", + "of '" + string + "' is newer than the versions supported by this version of Maven: " + + validVersions + ". Building this project requires a newer version of Maven.", tracker); } else if (olderThanAll) { @@ -1516,8 +1518,8 @@ public class DefaultModelValidator implements ModelValidator { Version.V20, "modelVersion", null, - "of '" + string + "' is older than the versions supported by this version of Maven: " + values - + ". Building this project requires an older version of Maven.", + "of '" + string + "' is older than the versions supported by this version of Maven: " + + validVersions + ". Building this project requires an older version of Maven.", tracker); } else { @@ -1527,7 +1529,7 @@ public class DefaultModelValidator implements ModelValidator { Version.V20, "modelVersion", null, - "must be one of " + values + " but is '" + string + "'.", + "must be one of " + validVersions + " but is '" + string + "'.", tracker); }