From 33010af039761c0cb751df9024c7500af7eafd23 Mon Sep 17 00:00:00 2001 From: Guillaume Nodet Date: Fri, 7 Jun 2024 09:31:02 +0200 Subject: [PATCH] Move modelVersion inference to model builder so that it is also effective on consumer models (#1566) --- .../internal/impl/model/BuildModelTransformer.java | 13 ------------- .../internal/impl/model/DefaultModelBuilder.java | 6 ++++++ 2 files changed, 6 insertions(+), 13 deletions(-) diff --git a/maven-api-impl/src/main/java/org/apache/maven/internal/impl/model/BuildModelTransformer.java b/maven-api-impl/src/main/java/org/apache/maven/internal/impl/model/BuildModelTransformer.java index cc48498afe..c1ff4d8efa 100644 --- a/maven-api-impl/src/main/java/org/apache/maven/internal/impl/model/BuildModelTransformer.java +++ b/maven-api-impl/src/main/java/org/apache/maven/internal/impl/model/BuildModelTransformer.java @@ -44,28 +44,15 @@ import org.apache.maven.api.services.ModelTransformerContext; @Singleton public class BuildModelTransformer implements ModelTransformer { - public static final String NAMESPACE_PREFIX = "http://maven.apache.org/POM/"; - @Override public Model transform(ModelTransformerContext context, Model model, Path path) { Model.Builder builder = Model.newBuilder(model); - handleModelVersion(context, model, path, builder); handleParent(context, model, path, builder); handleReactorDependencies(context, model, path, builder); handleCiFriendlyVersion(context, model, path, builder); return builder.build(); } - // - // Infer modelVersion from namespace URI - // - void handleModelVersion(ModelTransformerContext context, Model model, Path pomFile, Model.Builder builder) { - String namespace = model.getNamespaceUri(); - if (model.getModelVersion() == null && namespace != null && namespace.startsWith(NAMESPACE_PREFIX)) { - builder.modelVersion(namespace.substring(NAMESPACE_PREFIX.length())); - } - } - // // Infer parent information // diff --git a/maven-api-impl/src/main/java/org/apache/maven/internal/impl/model/DefaultModelBuilder.java b/maven-api-impl/src/main/java/org/apache/maven/internal/impl/model/DefaultModelBuilder.java index b66f04d6d9..0ea2430097 100644 --- a/maven-api-impl/src/main/java/org/apache/maven/internal/impl/model/DefaultModelBuilder.java +++ b/maven-api-impl/src/main/java/org/apache/maven/internal/impl/model/DefaultModelBuilder.java @@ -99,6 +99,7 @@ import org.slf4j.LoggerFactory; @Singleton public class DefaultModelBuilder implements ModelBuilder { + public static final String NAMESPACE_PREFIX = "http://maven.apache.org/POM/"; private static final String RAW = "raw"; private static final String FILE = "file"; private static final String IMPORT = "import"; @@ -749,6 +750,11 @@ public class DefaultModelBuilder implements ModelBuilder { } } + String namespace = rawModel.getNamespaceUri(); + if (rawModel.getModelVersion() == null && namespace != null && namespace.startsWith(NAMESPACE_PREFIX)) { + rawModel = rawModel.withModelVersion(namespace.substring(NAMESPACE_PREFIX.length())); + } + modelValidator.validateRawModel(rawModel, request, problems); if (hasFatalErrors(problems)) {