gnodet commented on code in PR #14133:
URL: https://github.com/apache/maven/pull/14133#discussion_r4772327193


##########
impl/maven-core/src/main/java/org/apache/maven/project/MavenProject.java:
##########
@@ -1087,37 +1088,28 @@ public Map<String, List<String>> 
getInjectedProfileIds() {
     }
 
     /**
-     * Add or replace an artifact. This method is now deprecated. Use the 
@{MavenProjectHelper} to attach artifacts to a
-     * project. In spite of the 'throws' declaration on this API, this method 
has never thrown an exception since Maven
-     * 3.0.x. Historically, it logged and ignored a second addition of the 
same g/a/v/c/t. Now it replaces the file for
-     * the artifact, so that plugins (e.g. shade) can change the pathname of 
the file for a particular set of
-     * coordinates.
-     *
-     * @param artifact the artifact to add or replace.
-     * @deprecated Please use {@link MavenProjectHelper}
-     * @throws DuplicateArtifactAttachmentException will never happen but 
leave it for backward compatibility
+     * Add or replace an artifact. A second attachment with the same 
coordinates replaces the previous one.
+     * @deprecated use {@link MavenProjectHelper}; never actually throws the 
declared exception
      */
     public void addAttachedArtifact(Artifact artifact) throws 
DuplicateArtifactAttachmentException {
-        // if already there we remove it and add again
-        int index = attachedArtifacts.indexOf(artifact);
-        if (index >= 0) {
+        DefaultProject dp = (DefaultProject) mutableProject.project();
+        org.apache.maven.api.Artifact v4 = dp.getSession()
+                .getArtifact(org.apache.maven.api.ProducedArtifact.class, 
RepositoryUtils.toArtifact(artifact));
+        if (mutableProject.addAttachedArtifact(v4)) {
             LOGGER.warn("artifact '{}' already attached, replacing previous 
instance", artifact);
-            attachedArtifacts.set(index, artifact);
-        } else {
-            attachedArtifacts.add(artifact);
         }
     }
 
-    /**
-     * Returns a read-only list of the attached artifacts to this project.
-     *
-     * @return the attached artifacts of this project
-     */
+    /** Returns a read-only list of the attached artifacts to this project. */
     public List<Artifact> getAttachedArtifacts() {
-        if (attachedArtifacts == null) {
-            attachedArtifacts = new ArrayList<>();
+        if (mutableProject.attachedArtifacts().isEmpty()) {

Review Comment:
   _Claude Code on behalf of Guillaume Nodet_
   
   Addressed in 5ed6460. The current `getAttachedArtifacts()` uses 
`projectOrNull()` with a three-tier session-independent fallback:
   
   1. **v3 cache hit** — returns the original v3 artifact (covers `deepCopy` 
clones since the cache is copied)
   2. **Session available** (`p != null`) — resolves from ArtifactManager
   3. **No session** (`p == null`) — extracts the aether artifact directly from 
`DefaultArtifact`, or constructs one from coordinates
   
   So unregistered clones (forked lifecycle) no longer NPE.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to