[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.
This commit is contained in:
Guillaume Nodet 2023-10-18 15:31:36 +02:00 committed by GitHub
parent 2f99c66f86
commit 251a316962
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 11 deletions

View File

@ -87,6 +87,7 @@
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 @@ private Model readEffectiveModel(
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

View File

@ -24,6 +24,7 @@
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 @@
@Singleton
public class DefaultModelValidator implements ModelValidator {
public static final List<String> 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 void validateFileModel(Model ma, ModelBuildingRequest request, ModelProbl
// 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 void validateRawModel(Model ma, ModelBuildingRequest request, ModelProble
// 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 @@ private boolean validateEnum(
@SuppressWarnings("checkstyle:parameternumber")
private boolean validateModelVersion(
ModelProblemCollector problems, String string, InputLocationTracker tracker, String... validVersions) {
ModelProblemCollector problems, String string, InputLocationTracker tracker, List<String> validVersions) {
if (string == null || string.length() <= 0) {
return true;
}
List<String> values = Arrays.asList(validVersions);
if (values.contains(string)) {
if (validVersions.contains(string)) {
return true;
}
@ -1504,8 +1506,8 @@ private boolean validateModelVersion(
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 @@ private boolean validateModelVersion(
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 @@ private boolean validateModelVersion(
Version.V20,
"modelVersion",
null,
"must be one of " + values + " but is '" + string + "'.",
"must be one of " + validVersions + " but is '" + string + "'.",
tracker);
}