This is an automated email from the ASF dual-hosted git repository.

kwin pushed a commit to branch feature/validate-ids-across-settings-and-model
in repository https://gitbox.apache.org/repos/asf/maven.git

commit 8280b80e37b11709d9968b56f31de99273f88b49
Author: Konrad Windszus <k...@apache.org>
AuthorDate: Tue Feb 13 16:51:42 2024 +0100

    [MNG-8050] emit warn in case of repo id clashes between settings and POM
---
 maven-model-builder/pom.xml                        |  1 +
 .../maven/model/building/DefaultModelBuilder.java  |  1 +
 .../model/validation/DefaultModelValidator.java    | 32 ++++++++++++++++++++++
 .../maven/model/validation/ModelValidator.java     | 21 ++++++++++++++
 4 files changed, 55 insertions(+)

diff --git a/maven-model-builder/pom.xml b/maven-model-builder/pom.xml
index f290705404..a81a5ff151 100644
--- a/maven-model-builder/pom.xml
+++ b/maven-model-builder/pom.xml
@@ -159,6 +159,7 @@ under the License.
               
<exclude>org.apache.maven.model.superpom.SuperPomProvider#getSuperModel(java.lang.String):METHOD_RETURN_TYPE_CHANGED</exclude>
               
<exclude>org.apache.maven.model.validation.DefaultModelValidator#validateDependencyVersion(org.apache.maven.model.building.ModelProblemCollector,org.apache.maven.model.Dependency,java.lang.String):METHOD_REMOVED</exclude>
               
<exclude>org.apache.maven.model.validation.ModelValidator#validateFileModel(org.apache.maven.model.Model,org.apache.maven.model.building.ModelBuildingRequest,org.apache.maven.model.building.ModelProblemCollector):METHOD_NEW_DEFAULT</exclude>
+              
<exclude>org.apache.maven.model.validation.ModelValidator#validateExternalProfiles(java.util.List,org.apache.maven.model.Model,org.apache.maven.model.building.ModelBuildingRequest,org.apache.maven.model.building.ModelProblemCollector):METHOD_NEW_DEFAULT</exclude>
             </excludes>
           </parameter>
         </configuration>
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 7d832654a1..6bc41b6b23 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
@@ -737,6 +737,7 @@ public class DefaultModelBuilder implements ModelBuilder {
             profileInjector.injectProfile(inputModel, activeProfile, request, 
problems);
         }
 
+        modelValidator.validateExternalProfiles(activeExternalProfiles, 
inputModel, request, problems);
         for (Profile activeProfile : activeExternalProfiles) {
             profileInjector.injectProfile(inputModel, activeProfile, request, 
problems);
         }
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 25f4328cca..201586d210 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
@@ -30,9 +30,11 @@ import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Objects;
+import java.util.Optional;
 import java.util.Set;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
+import java.util.stream.Collectors;
 
 import org.apache.maven.api.model.Activation;
 import org.apache.maven.api.model.ActivationFile;
@@ -576,6 +578,36 @@ public class DefaultModelValidator implements 
ModelValidator {
         }
     }
 
+    @Override
+    public void validateExternalProfiles(
+            List<org.apache.maven.model.Profile> activeExternalProfiles,
+            Model ma,
+            ModelBuildingRequest request,
+            ModelProblemCollector problems) {
+        org.apache.maven.api.model.Model m = ma.getDelegate();
+        // check for id clashes in repositories
+        for (Profile profile : activeExternalProfiles.stream()
+                .map(org.apache.maven.model.Profile::getDelegate)
+                .collect(Collectors.toList())) {
+            for (Repository repository : profile.getRepositories()) {
+                Optional<Repository> clashingPomRepository = 
m.getRepositories().stream()
+                        .filter(r -> r.getId().equals(repository.getId()))
+                        .findFirst();
+                if (clashingPomRepository.isPresent()) {
+                    addViolation(
+                            problems,
+                            Severity.WARNING,
+                            Version.V40, // ?
+                            "pom repository",
+                            "?",
+                            "is overwritten by the repository with same id 
from external profile with id "
+                                    + profile.getId(),
+                            clashingPomRepository.get());
+                }
+            }
+        }
+    }
+
     private void validate20RawDependencies(
             ModelProblemCollector problems,
             List<Dependency> dependencies,
diff --git 
a/maven-model-builder/src/main/java/org/apache/maven/model/validation/ModelValidator.java
 
b/maven-model-builder/src/main/java/org/apache/maven/model/validation/ModelValidator.java
index 2817d95fc6..bb87370e0e 100644
--- 
a/maven-model-builder/src/main/java/org/apache/maven/model/validation/ModelValidator.java
+++ 
b/maven-model-builder/src/main/java/org/apache/maven/model/validation/ModelValidator.java
@@ -18,7 +18,10 @@
  */
 package org.apache.maven.model.validation;
 
+import java.util.List;
+
 import org.apache.maven.model.Model;
+import org.apache.maven.model.Profile;
 import org.apache.maven.model.building.ModelBuildingRequest;
 import org.apache.maven.model.building.ModelProblemCollector;
 
@@ -49,6 +52,24 @@ public interface ModelValidator {
      */
     void validateRawModel(Model model, ModelBuildingRequest request, 
ModelProblemCollector problems);
 
+    /**
+     * Checks the specified (raw) model for clashes with the passed active 
external profiles. The raw model is the
+     * file model + buildpom filter transformation and has not been subjected 
to inheritance, interpolation or profile/default injection.
+     *
+     * @param activeExternalProfiles the active profiles coming from external 
sources (settings.xml), must not be {@code null}
+     * @param model The model to validate, must not be {@code null}.
+     * @param request The model building request that holds further settings, 
must not be {@code null}.
+     * @param problems The container used to collect problems that were 
encountered, must not be {@code null}.
+     * @since 4.0.0
+     */
+    default void validateExternalProfiles(
+            List<Profile> activeExternalProfiles,
+            Model model,
+            ModelBuildingRequest request,
+            ModelProblemCollector problems) {
+        // do nothing
+    }
+
     /**
      * Checks the specified (effective) model for missing or invalid values. 
The effective model is fully assembled and
      * has undergone inheritance, interpolation and other model operations.

Reply via email to