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


##########
impl/maven-core/src/main/java/org/apache/maven/internal/impl/DefaultProjectManager.java:
##########
@@ -85,9 +88,8 @@ public Optional<Path> getPath(@Nonnull Project project) {
     @Override
     public Collection<ProducedArtifact> getAttachedArtifacts(@Nonnull Project 
project) {
         requireNonNull(project, "project" + " cannot be null");
-        Collection<ProducedArtifact> attached = map(
-                getMavenProject(project).getAttachedArtifacts(),
-                a -> getSession(project).getArtifact(ProducedArtifact.class, 
RepositoryUtils.toArtifact(a)));
+        Collection<ProducedArtifact> attached =
+                map(getMutableProject(project).attachedArtifacts(), a -> 
(ProducedArtifact) a);

Review Comment:
   _Fully automatic review from Claude Code_
   
   Fixed in 5567c3d. Added an `instanceof` guard with an explicit 
`IllegalStateException` instead of the bare cast.



##########
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.
+     * Add or replace an artifact.
      *
      * @param artifact the artifact to add or replace.
      * @deprecated Please use {@link MavenProjectHelper}
      * @throws DuplicateArtifactAttachmentException will never happen but 
leave it for backward compatibility
      */
     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<>();
-        }
-        return Collections.unmodifiableList(attachedArtifacts);
+        // session.toArtifact() resolves the path from ArtifactManager
+        DefaultProject dp = (DefaultProject) mutableProject.project();

Review Comment:
   _Fully automatic review from Claude Code_
   
   Fixed in 5567c3d. Added an early return for the empty-list case before 
accessing `mutableProject.project()`, so cloned projects with no attached 
artifacts don't NPE.



##########
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.
+     * Add or replace an artifact.

Review Comment:
   _Fully automatic review from Claude Code_
   
   Fixed in 5567c3d. Restored the behavioral notes about the never-thrown 
exception and replacement semantics in the javadoc.



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