You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by kw...@apache.org on 2024/02/13 18:01:12 UTC

(maven) branch feature/validate-ids-across-settings-and-model created (now 6ca7fe2012)

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

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


      at 6ca7fe2012 [MNG-8050] emit warn in case of repo id clashes between settings and POM

This branch includes the following new commits:

     new 6ca7fe2012 [MNG-8050] emit warn in case of repo id clashes between settings and POM

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



(maven) 01/01: [MNG-8050] emit warn in case of repo id clashes between settings and POM

Posted by kw...@apache.org.
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 6ca7fe2012dcee7d4a7e92858902907f304119e3
Author: Konrad Windszus <kw...@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
---
 .../project/DefaultProjectBuildingHelper.java      |  3 ++
 maven-model-builder/pom.xml                        |  1 +
 .../maven/model/building/DefaultModelBuilder.java  |  1 +
 .../model/validation/DefaultModelValidator.java    | 32 ++++++++++++++++++++++
 .../maven/model/validation/ModelValidator.java     | 21 ++++++++++++++
 5 files changed, 58 insertions(+)

diff --git a/maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuildingHelper.java b/maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuildingHelper.java
index 345ffeb658..12ef8372ed 100644
--- a/maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuildingHelper.java
+++ b/maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuildingHelper.java
@@ -125,6 +125,9 @@ public class DefaultProjectBuildingHelper implements ProjectBuildingHelper {
             for (ArtifactRepository repository : recessiveRepositories) {
                 if (repoIds.add(repository.getId())) {
                     artifactRepositories.add(repository);
+                } else {
+                    // TODO: ERROR or WARNING
+                    
                 }
             }
         }
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.