[MNG-5868] Adding serval times the same artifact via MavenProjectHelper (attachArtifact) does not produce a failure
o Updated to restore the behaviour consensus had been reached in MNG-5387. o Kept 'MavenProject.getAttachedArtifacts' to return an unmodifiable list. Project: http://git-wip-us.apache.org/repos/asf/maven/repo Commit: http://git-wip-us.apache.org/repos/asf/maven/commit/5f048234 Tree: http://git-wip-us.apache.org/repos/asf/maven/tree/5f048234 Diff: http://git-wip-us.apache.org/repos/asf/maven/diff/5f048234 Branch: refs/heads/master Commit: 5f048234ff44dbf70fcad9f17834c64866f452e1 Parents: 536350f Author: Christian Schulte <schu...@apache.org> Authored: Fri Dec 18 00:27:26 2015 +0100 Committer: Christian Schulte <schu...@apache.org> Committed: Fri Dec 18 00:30:02 2015 +0100 ---------------------------------------------------------------------- .../project/DefaultMavenProjectHelper.java | 7 ---- .../org/apache/maven/project/MavenProject.java | 39 +++++++++++++++----- 2 files changed, 29 insertions(+), 17 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/maven/blob/5f048234/maven-core/src/main/java/org/apache/maven/project/DefaultMavenProjectHelper.java ---------------------------------------------------------------------- diff --git a/maven-core/src/main/java/org/apache/maven/project/DefaultMavenProjectHelper.java b/maven-core/src/main/java/org/apache/maven/project/DefaultMavenProjectHelper.java index 2cce9f6..223e920 100644 --- a/maven-core/src/main/java/org/apache/maven/project/DefaultMavenProjectHelper.java +++ b/maven-core/src/main/java/org/apache/maven/project/DefaultMavenProjectHelper.java @@ -91,13 +91,6 @@ public class DefaultMavenProjectHelper attachArtifact( project, artifact ); } - /** - * Add an attached artifact or replace the file for an existing artifact. - * - * @see MavenProject#addAttachedArtifact(org.apache.maven.artifact.Artifact) - * @param project project reference. - * @param artifact artifact to add or replace. - */ public void attachArtifact( MavenProject project, Artifact artifact ) { project.addAttachedArtifact( artifact ); http://git-wip-us.apache.org/repos/asf/maven/blob/5f048234/maven-core/src/main/java/org/apache/maven/project/MavenProject.java ---------------------------------------------------------------------- diff --git a/maven-core/src/main/java/org/apache/maven/project/MavenProject.java b/maven-core/src/main/java/org/apache/maven/project/MavenProject.java index 9c936e1..8337834 100644 --- a/maven-core/src/main/java/org/apache/maven/project/MavenProject.java +++ b/maven-core/src/main/java/org/apache/maven/project/MavenProject.java @@ -909,19 +909,38 @@ public class MavenProject } /** - * 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. + * Adds or replaces an artifact. * - * @param artifact the artifact to add or replace. - * @throws DuplicateArtifactAttachmentException + * @param artifact The artifact to add or replace. + * + * @deprecated Please use {@link MavenProjectHelper} + * @see https://issues.apache.org/jira/browse/MNG-5868 + * @see https://issues.apache.org/jira/browse/MNG-5387 + * @see https://issues.apache.org/jira/browse/MNG-4013 + * @see https://issues.apache.org/jira/browse/MNG-3119 */ + @Deprecated public void addAttachedArtifact( Artifact artifact ) - throws DuplicateArtifactAttachmentException { - getAttachedArtifacts().add( artifact ); + getAttachedArtifacts(); + assert this.attachedArtifacts != null : "Unexpected missing attached artifacts."; + + boolean replaced = false; + for ( int i = 0, s0 = this.attachedArtifacts.size(); i < s0; i++ ) + { + final Artifact a = this.attachedArtifacts.get( i ); + + if ( a.equals( artifact ) ) + { + this.attachedArtifacts.set( i, artifact ); + replaced = true; + } + } + + if ( !replaced ) + { + this.attachedArtifacts.add( artifact ); + } } public List<Artifact> getAttachedArtifacts() @@ -930,7 +949,7 @@ public class MavenProject { attachedArtifacts = new ArrayList<>(); } - return attachedArtifacts; + return Collections.unmodifiableList( attachedArtifacts ); } public Xpp3Dom getGoalConfiguration( String pluginGroupId, String pluginArtifactId, String executionId,