[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. o Updated to log an information message when an existing artifact got replaced. Project: http://git-wip-us.apache.org/repos/asf/maven/repo Commit: http://git-wip-us.apache.org/repos/asf/maven/commit/7d42255e Tree: http://git-wip-us.apache.org/repos/asf/maven/tree/7d42255e Diff: http://git-wip-us.apache.org/repos/asf/maven/diff/7d42255e Branch: refs/heads/MNG-5868 Commit: 7d42255e6c24e185b37e31416260001eac157bc5 Parents: a3cdfbb Author: Christian Schulte <[email protected]> Authored: Thu Dec 17 22:43:47 2015 +0100 Committer: Christian Schulte <[email protected]> Committed: Tue Jan 31 22:52:38 2017 +0100 ---------------------------------------------------------------------- .../project/DefaultMavenProjectHelper.java | 13 +++---- .../org/apache/maven/project/MavenProject.java | 39 +++++++++++++++----- 2 files changed, 35 insertions(+), 17 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/maven/blob/7d42255e/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..54c15bb 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,16 +91,15 @@ 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 ) { + final int size = project.getAttachedArtifacts().size(); project.addAttachedArtifact( artifact ); + + if ( project.getAttachedArtifacts().size() == size && this.getLogger().isInfoEnabled() ) + { + this.getLogger().info( String.format( "Replaced artifact %s.", artifact ) ); + } } public void addResource( MavenProject project, String resourceDirectory, List<String> includes, http://git-wip-us.apache.org/repos/asf/maven/blob/7d42255e/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 952622f..53ee74d 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,
