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

gnodet pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven.git


The following commit(s) were added to refs/heads/master by this push:
     new c736368755 Add a classifier-aware artifact map (fix #6773) (#13672)
c736368755 is described below

commit c736368755a1a36f6c5a1b7c90c9df4b70124cfd
Author: Arturo Bernal <[email protected]>
AuthorDate: Thu Nov 5 15:00:06 2026 +0100

    Add a classifier-aware artifact map (fix #6773) (#13672)
    
    * Add a classifier-aware artifact map (fix #6773)
    
    artifactMapByVersionlessId keys only by groupId:artifactId, so two
    artifacts that differ only by classifier collide and one of them
    becomes unreachable through project.getArtifactMap().
    
    Moving the new logic to compat/maven-artifact was wrong: that module
    is the frozen Maven 3.x compatibility shim, not a place for new 4.x
    capabilities. Added ArtifactMaps, a small package-private helper local
    to MavenProject, and wired it into
    MavenProject.getArtifactMapByDependencyConflictId(). The existing
    getArtifactMap() and its callers are untouched.
    
    MavenProject.java is already at the checkstyle FileLength ceiling
    (2000 lines), so the Javadoc on the new public method stays to a
    single @see + @since line; the full documentation lives on the
    ArtifactMaps helper instead.
    
    * Address review: fix Javadoc, add caching for 
getArtifactMapByDependencyConflictId
    
    - Replace @see shorthand with full Javadoc including description, @return 
and @since
    - Add artifactMapByDependencyConflictId field to cache the result 
(consistent with getArtifactMap())
    - Invalidate cache in setArtifacts(), setResolvedArtifacts() and 
setArtifactFilter()
    
    * Fix Checkstyle FileLength violation: reduce MavenProject.java to 2000 
lines
    
    Trim decorative blank lines and verbose comment banner to stay within the
    2000-line Checkstyle limit introduced by the new caching field and Javadoc.
    
    ---------
    
    Co-authored-by: Guillaume Nodet <[email protected]>
---
 .../apache/maven/artifact/ArtifactUtilsTest.java   | 17 ++++++++
 .../org/apache/maven/project/ArtifactMaps.java     | 50 ++++++++++++++++++++++
 .../org/apache/maven/project/MavenProject.java     | 34 ++++++++-------
 .../org/apache/maven/project/MavenProjectTest.java | 24 +++++++++++
 4 files changed, 110 insertions(+), 15 deletions(-)

diff --git 
a/compat/maven-artifact/src/test/java/org/apache/maven/artifact/ArtifactUtilsTest.java
 
b/compat/maven-artifact/src/test/java/org/apache/maven/artifact/ArtifactUtilsTest.java
index 740bdee410..ab69eb0e8a 100644
--- 
a/compat/maven-artifact/src/test/java/org/apache/maven/artifact/ArtifactUtilsTest.java
+++ 
b/compat/maven-artifact/src/test/java/org/apache/maven/artifact/ArtifactUtilsTest.java
@@ -75,4 +75,21 @@ void testArtifactMapByVersionlessIdOrdering() throws 
Exception {
         assertNotNull(map);
         assertEquals(list, new ArrayList<>(map.values()));
     }
+
+    /**
+     * Two artifacts with the same groupId and artifactId but different 
classifiers collide under the same
+     * versionless key, so only one of them is reachable (MNG-5552).
+     */
+    @Test
+    void testArtifactMapByVersionlessIdCollidesOnClassifier() {
+        Artifact jar = new DefaultArtifact(
+                "group", "artifact", VersionRange.createFromVersion("1.0"), 
"compile", "jar", "", null);
+        Artifact testsJar = new DefaultArtifact(
+                "group", "artifact", VersionRange.createFromVersion("1.0"), 
"test", "jar", "tests", null);
+
+        Map<String, Artifact> map = 
ArtifactUtils.artifactMapByVersionlessId(List.of(jar, testsJar));
+
+        assertEquals(1, map.size());
+        assertEquals(testsJar, map.get("group:artifact"));
+    }
 }
diff --git 
a/impl/maven-core/src/main/java/org/apache/maven/project/ArtifactMaps.java 
b/impl/maven-core/src/main/java/org/apache/maven/project/ArtifactMaps.java
new file mode 100644
index 0000000000..073b51eb94
--- /dev/null
+++ b/impl/maven-core/src/main/java/org/apache/maven/project/ArtifactMaps.java
@@ -0,0 +1,50 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.maven.project;
+
+import java.util.Collection;
+import java.util.LinkedHashMap;
+import java.util.Map;
+
+import org.apache.maven.artifact.Artifact;
+
+/**
+ * Builds lookup maps of {@link Artifact} for {@link MavenProject}.
+ *
+ * @since 4.1.0
+ */
+final class ArtifactMaps {
+
+    private ArtifactMaps() {}
+
+    /**
+     * Builds a map of artifacts keyed by {@link 
Artifact#getDependencyConflictId()}
+     * (groupId:artifactId:type[:classifier]). Unlike {@link 
MavenProject#getArtifactMap()}, this disambiguates
+     * artifacts that share the same groupId and artifactId but differ by 
classifier, such as a jar and its
+     * "tests" or "sources" classified companion, which would otherwise 
collide under the same key (MNG-5552).
+     *
+     * @param artifacts the artifacts to build the map from
+     * @return a mutable map keyed by dependency conflict id; if two artifacts 
share the same key the last one wins
+     */
+    static Map<String, Artifact> byDependencyConflictId(Collection<Artifact> 
artifacts) {
+        Map<String, Artifact> map = new LinkedHashMap<>();
+        artifacts.forEach(artifact -> 
map.put(artifact.getDependencyConflictId(), artifact));
+        return map;
+    }
+}
diff --git 
a/impl/maven-core/src/main/java/org/apache/maven/project/MavenProject.java 
b/impl/maven-core/src/main/java/org/apache/maven/project/MavenProject.java
index fbe4751c6d..4d386355fb 100644
--- a/impl/maven-core/src/main/java/org/apache/maven/project/MavenProject.java
+++ b/impl/maven-core/src/main/java/org/apache/maven/project/MavenProject.java
@@ -168,6 +168,7 @@ public class MavenProject implements Cloneable {
 
     // calculated.
     private Map<String, Artifact> artifactMap;
+    private Map<String, Artifact> artifactMapByDependencyConflictId;
 
     private Model originalModel;
 
@@ -881,8 +882,8 @@ public void addLicense(License license) {
 
     public void setArtifacts(Set<Artifact> artifacts) {
         this.artifacts = artifacts;
-        // flush the calculated artifactMap
         artifactMap = null;
+        artifactMapByDependencyConflictId = null;
     }
 
     /**
@@ -917,9 +918,22 @@ public Map<String, Artifact> getArtifactMap() {
         return artifactMap;
     }
 
+    /**
+     * Like {@link #getArtifactMap()}, but keyed by {@link 
org.apache.maven.artifact.Artifact#getDependencyConflictId()}
+     * (groupId:artifactId:type[:classifier]), which disambiguates artifacts 
that share the same groupId:artifactId
+     * but differ by classifier (MNG-5552).
+     * @return a mutable map keyed by dependency conflict id; if two artifacts 
share the same key the last one wins
+     * @since 4.1.0
+     */
+    public Map<String, Artifact> getArtifactMapByDependencyConflictId() {
+        if (artifactMapByDependencyConflictId == null) {
+            artifactMapByDependencyConflictId = 
ArtifactMaps.byDependencyConflictId(getArtifacts());
+        }
+        return artifactMapByDependencyConflictId;
+    }
+
     public void setPluginArtifacts(Set<Artifact> pluginArtifacts) {
         this.pluginArtifacts = pluginArtifacts;
-
         this.pluginArtifactMap = null;
     }
 
@@ -931,7 +945,6 @@ public Map<String, Artifact> getPluginArtifactMap() {
         if (pluginArtifactMap == null) {
             pluginArtifactMap = 
ArtifactUtils.artifactMapByVersionlessId(getPluginArtifacts());
         }
-
         return pluginArtifactMap;
     }
 
@@ -1513,6 +1526,7 @@ public void setResolvedArtifacts(Set<Artifact> artifacts) 
{
         this.resolvedArtifacts = (artifacts != null) ? artifacts : 
Collections.emptySet();
         this.artifacts = null;
         this.artifactMap = null;
+        this.artifactMapByDependencyConflictId = null;
     }
 
     /**
@@ -1527,6 +1541,7 @@ public void setArtifactFilter(ArtifactFilter 
artifactFilter) {
         this.artifactFilter = artifactFilter;
         this.artifacts = null;
         this.artifactMap = null;
+        this.artifactMapByDependencyConflictId = null;
     }
 
     /**
@@ -1553,15 +1568,7 @@ public void addLifecyclePhase(String lifecyclePhase) {
     }
 
     // 
----------------------------------------------------------------------------------------------------------------
-    //
-    //
-    // D E P R E C A T E D
-    //
-    //
-    // 
----------------------------------------------------------------------------------------------------------------
-    //
-    // Everything below will be removed for Maven 4.0.0
-    //
+    // D E P R E C A T E D  -  Everything below will be removed for Maven 4.0.0
     // 
----------------------------------------------------------------------------------------------------------------
 
     private ProjectBuildingRequest projectBuilderConfiguration;
@@ -1840,7 +1847,6 @@ public Reporting getReporting() {
     @Deprecated
     public void setReportArtifacts(Set<Artifact> reportArtifacts) {
         this.reportArtifacts = reportArtifacts;
-
         reportArtifactMap = null;
     }
 
@@ -1854,7 +1860,6 @@ public Map<String, Artifact> getReportArtifactMap() {
         if (reportArtifactMap == null) {
             reportArtifactMap = 
ArtifactUtils.artifactMapByVersionlessId(getReportArtifacts());
         }
-
         return reportArtifactMap;
     }
 
@@ -1874,7 +1879,6 @@ public Map<String, Artifact> getExtensionArtifactMap() {
         if (extensionArtifactMap == null) {
             extensionArtifactMap = 
ArtifactUtils.artifactMapByVersionlessId(getExtensionArtifacts());
         }
-
         return extensionArtifactMap;
     }
 
diff --git 
a/impl/maven-core/src/test/java/org/apache/maven/project/MavenProjectTest.java 
b/impl/maven-core/src/test/java/org/apache/maven/project/MavenProjectTest.java
index 71b7c68f99..c8ba00af4f 100644
--- 
a/impl/maven-core/src/test/java/org/apache/maven/project/MavenProjectTest.java
+++ 
b/impl/maven-core/src/test/java/org/apache/maven/project/MavenProjectTest.java
@@ -23,9 +23,12 @@
 import java.nio.file.Path;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 
 import org.apache.maven.api.Language;
 import org.apache.maven.api.ProjectScope;
+import org.apache.maven.artifact.Artifact;
+import org.apache.maven.artifact.DefaultArtifact;
 import org.apache.maven.impl.DefaultSourceRoot;
 import org.apache.maven.model.DependencyManagement;
 import org.apache.maven.model.Model;
@@ -265,6 +268,27 @@ void pluginManagementShouldReturnNotNull() {
         assertNotNull(project.getPluginManagement());
     }
 
+    /**
+     * Two artifacts with the same groupId and artifactId but different 
classifiers collide in
+     * {@link MavenProject#getArtifactMap()}, so only one of them is reachable 
(MNG-5552).
+     * {@link MavenProject#getArtifactMapByDependencyConflictId()} keeps both.
+     */
+    @Test
+    void getArtifactMapByDependencyConflictIdKeepsBothClassifiers() {
+        Artifact jar = new DefaultArtifact("group", "artifact", "1.0", 
"compile", "jar", "", null);
+        Artifact testsJar = new DefaultArtifact("group", "artifact", "1.0", 
"test", "jar", "tests", null);
+
+        MavenProject project = new MavenProject();
+        project.setArtifacts(Set.of(jar, testsJar));
+
+        assertEquals(1, project.getArtifactMap().size());
+
+        Map<String, Artifact> byConflictId = 
project.getArtifactMapByDependencyConflictId();
+        assertEquals(2, byConflictId.size());
+        assertEquals(jar, byConflictId.get("group:artifact:jar"));
+        assertEquals(testsJar, byConflictId.get("group:artifact:jar:tests"));
+    }
+
     private void assertNoNulls(List<String> elements) {
         assertFalse(elements.contains(null), "Expected " + elements + " to not 
contain " + null);
     }

Reply via email to