Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve model validator wrt modelVersion which is now set in the file model #1757

Merged
merged 1 commit into from
Oct 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,6 @@
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
Expand Down Expand Up @@ -350,6 +348,10 @@ public void validateFileModel(
}
}
} 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);
Expand All @@ -365,12 +367,6 @@ public void validateFileModel(
}
}
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(
Expand Down Expand Up @@ -422,12 +418,6 @@ public void validateFileModel(

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);
Expand Down Expand Up @@ -562,16 +552,8 @@ public void validateFileModel(
@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(
Expand Down