From bea3e72cdbf66d114b709823427f278300d784c2 Mon Sep 17 00:00:00 2001 From: Tamas Cservenak Date: Mon, 10 Jun 2024 13:04:53 +0200 Subject: [PATCH] [MNG-8141] Model Builder report issues during build (#1569) And ArtifactDescriptorReader should not "toss away" but report WARNings (as in case of ERROR or FATAL MB throws). Part 1: port ArtifactDescriptorReader to not lose WARNs Part 2: make model validator report Profile ID duplications even in MIN level (as model WARNs) --- https://issues.apache.org/jira/browse/MNG-8141 --- .../impl/model/DefaultModelValidator.java | 17 ++++++++++++- .../DefaultArtifactDescriptorReader.java | 25 ++++++++++++++++++- 2 files changed, 40 insertions(+), 2 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 ce57f6443361..63d356d006d3 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 @@ -328,7 +328,22 @@ public void validateFileModel(Model m, ModelBuilderRequest request, ModelProblem } } - if (request.getValidationLevel() >= ModelBuilderRequest.VALIDATION_LEVEL_MAVEN_2_0) { + if (request.getValidationLevel() == ModelBuilderRequest.VALIDATION_LEVEL_MINIMAL) { + // profiles: they are essential for proper model building (may contribute profiles, dependencies...) + HashSet minProfileIds = new HashSet<>(); + for (org.apache.maven.api.model.Profile profile : m.getProfiles()) { + if (!minProfileIds.add(profile.getId())) { + addViolation( + problems, + Severity.WARNING, + Version.BASE, + "profiles.profile.id", + null, + "Duplicate activation for profile " + profile.getId(), + profile); + } + } + } else if (request.getValidationLevel() >= ModelBuilderRequest.VALIDATION_LEVEL_MAVEN_2_0) { Set modules = new HashSet<>(); for (int i = 0, n = m.getModules().size(); i < n; i++) { String module = m.getModules().get(i); diff --git a/maven-api-impl/src/main/java/org/apache/maven/internal/impl/resolver/DefaultArtifactDescriptorReader.java b/maven-api-impl/src/main/java/org/apache/maven/internal/impl/resolver/DefaultArtifactDescriptorReader.java index d8af5b4bfafe..3cd34c4e0986 100644 --- a/maven-api-impl/src/main/java/org/apache/maven/internal/impl/resolver/DefaultArtifactDescriptorReader.java +++ b/maven-api-impl/src/main/java/org/apache/maven/internal/impl/resolver/DefaultArtifactDescriptorReader.java @@ -20,6 +20,7 @@ import java.util.HashMap; import java.util.LinkedHashSet; +import java.util.List; import java.util.Map; import java.util.Objects; @@ -31,12 +32,14 @@ import org.apache.maven.api.services.ModelBuilder; import org.apache.maven.api.services.ModelBuilderException; import org.apache.maven.api.services.ModelBuilderRequest; +import org.apache.maven.api.services.ModelBuilderResult; import org.apache.maven.api.services.ModelProblem; import org.apache.maven.api.services.ModelRepositoryHolder; import org.apache.maven.api.services.ModelResolver; import org.apache.maven.api.services.ModelResolverException; import org.apache.maven.api.services.ModelSource; import org.apache.maven.internal.impl.InternalSession; +import org.apache.maven.internal.impl.model.ModelProblemUtils; import org.eclipse.aether.RepositoryEvent; import org.eclipse.aether.RepositoryEvent.EventType; import org.eclipse.aether.RepositoryException; @@ -62,6 +65,8 @@ import org.eclipse.aether.resolution.VersionResolutionException; import org.eclipse.aether.resolution.VersionResult; import org.eclipse.aether.transfer.ArtifactNotFoundException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * Default artifact descriptor reader. @@ -77,6 +82,7 @@ public class DefaultArtifactDescriptorReader implements ArtifactDescriptorReader private final ModelBuilder modelBuilder; private final Map artifactRelocationSources; private final ArtifactDescriptorReaderDelegate delegate; + private final Logger logger = LoggerFactory.getLogger(getClass()); @Inject public DefaultArtifactDescriptorReader( @@ -216,7 +222,24 @@ private Model loadPom( .modelCache(DefaultModelCache.newInstance(session, false)) .build(); - model = modelBuilder.build(modelRequest).getEffectiveModel(); + ModelBuilderResult modelResult = modelBuilder.build(modelRequest); + // ModelBuildingEx is thrown only on FATAL and ERROR severities, but we still can have WARNs + // that may lead to unexpected build failure, log them + if (!modelResult.getProblems().isEmpty()) { + List problems = modelResult.getProblems(); + logger.warn( + "{} {} encountered while building the effective model for {}", + problems.size(), + (problems.size() == 1) ? "problem was" : "problems were", + request.getArtifact()); + if (logger.isDebugEnabled()) { + for (ModelProblem problem : problems) { + logger.warn( + "{} @ {}", problem.getMessage(), ModelProblemUtils.formatLocation(problem, null)); + } + } + } + model = modelResult.getEffectiveModel(); } catch (ModelBuilderException e) { for (ModelProblem problem : e.getResult().getProblems()) { if (problem.getException() instanceof ModelResolverException) {