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


##########
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:
   ⚠️ **Partial fix — `deepCopy` clones with non-empty artifacts still NPE.**
   
   The guard short-circuits when `attachedArtifacts()` is empty, which is 
correct for a fresh-unregistered clone. But `deepCopy` explicitly copies 
artifacts from the source project (lines 1407–1409 of `MavenProject.java`):
   
   ```java
   if (!project.mutableProject.attachedArtifacts().isEmpty()) {
       
mutableProject.setAttachedArtifacts(project.mutableProject.attachedArtifacts());
   }
   ```
   
   If the **source** project had attached artifacts, the clone gets a non-empty 
`attachedArtifacts()` list — the `isEmpty()` guard passes through, and 
`mutableProject.project()` NPEs because the clone's `MutableProject` was never 
registered with a session.
   
   This matters during forked lifecycle execution: `MojoExecutor` clones the 
`MavenProject` via `clone()`, and the clone can have non-empty artifacts when 
invoked on a project with previously attached outputs (e.g. shade plugin runs 
before another plugin that calls `getAttachedArtifacts()`).
   
   A complete fix requires either (a) using `projectOrNull()` with a 
session-independent fallback, or (b) initializing the clone's `MutableProject` 
during `deepCopy`:
   
   ```suggestion
           if (mutableProject.attachedArtifacts().isEmpty()) {
               return List.of();
           }
           // session.toArtifact() resolves the path from ArtifactManager
           org.apache.maven.api.Project p = mutableProject.projectOrNull();
           if (p == null) {
               // Unregistered clone (e.g. forked lifecycle): convert artifacts 
without session path resolution
               return mutableProject.attachedArtifacts().stream()
                       .map(a -> 
RepositoryUtils.toArtifact((org.apache.maven.api.ProducedArtifact) a))
                       .toList();
           }
           DefaultProject dp = (DefaultProject) p;
   ```



-- 
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