Repository: maven Updated Branches: refs/heads/master 56543e464 -> 020e35816
[MNG-5868] Adding serval times the same artifact via MavenProjectHelper (attachArtifact) does not produce a failure o Updated method 'MavenProject.getAttachedArtifacts' to return an unmodifiable list. o Updated method 'MavenProject.addAttachedArtifact' to throw a 'DuplicateArtifactAttachmentException', if the same artifact already is attached. o Updated all 'attachArtifact' methods of interface 'MavenProjectHelper' to also throw a 'DuplicateArtifactAttachmentException', if the same artifact already is attached. This commit reduces the chances of 'hacking' the list of attached artifacts in unspecified ways. Such 'hacks' will now lead to 'RuntimeException's and will no longer work. Depending on what this leads to, plugins need to be updated and new methods may need to be added to 'MavenProjectHelper' for any use-cases no longer supported. Reverting this commit to stay compatible to unspecified behaviour should be the last option considered. Project: http://git-wip-us.apache.org/repos/asf/maven/repo Commit: http://git-wip-us.apache.org/repos/asf/maven/commit/020e3581 Tree: http://git-wip-us.apache.org/repos/asf/maven/tree/020e3581 Diff: http://git-wip-us.apache.org/repos/asf/maven/diff/020e3581 Branch: refs/heads/master Commit: 020e35816f184c10c3f87f103336fed4516f7af6 Parents: 56543e4 Author: Christian Schulte <[email protected]> Authored: Thu Dec 17 22:43:47 2015 +0100 Committer: Christian Schulte <[email protected]> Committed: Thu Dec 17 22:43:47 2015 +0100 ---------------------------------------------------------------------- .../project/DefaultMavenProjectHelper.java | 7 --- .../org/apache/maven/project/MavenProject.java | 66 +++++++++++++++++--- .../maven/project/MavenProjectHelper.java | 12 +++- 3 files changed, 65 insertions(+), 20 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/maven/blob/020e3581/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/020e3581/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..5710250 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 @@ -260,7 +260,7 @@ public class MavenProject /** * Sets project {@code file} without changing project {@code basedir}. - * + * * @since 3.2.4 */ public void setPomFile( File file ) @@ -909,19 +909,65 @@ 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 an artifact to the list of attached artifacts. + * + * @param artifact The artifact to add. + * + * @throws DuplicateArtifactAttachmentException if the same artifact already is attached to this project. * - * @param artifact the artifact to add or replace. - * @throws DuplicateArtifactAttachmentException + * @see #isArtifactAttached(org.apache.maven.artifact.Artifact) + * @see #getAttachedArtifacts() + * + * @deprecated Please use {@link MavenProjectHelper} to attach artifacts to a project. */ + @Deprecated public void addAttachedArtifact( Artifact artifact ) throws DuplicateArtifactAttachmentException { - getAttachedArtifacts().add( artifact ); + if ( artifact == null ) + { + throw new NullPointerException( "artifact" ); + } + +// MNG-5868: The following is the former Javadoc comment of this method. I added method 'isArtifactAttached' to provide +// a way for people to test for a possible 'DuplicateArtifactAttachmentException' and updated this method to +// throw that exception. Regarding the former comment: "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." is not what +// this method did before the update. It just added duplicate artifacts to the list of attached artifacts. + +// Former Javadoc comment: +// ----------------------- +// 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. + + if ( this.isArtifactAttached( artifact ) ) + { + throw new DuplicateArtifactAttachmentException( this, artifact ); + } + + this.attachedArtifacts.add( artifact ); + } + + /** + * Tests a given artifact to be contained in the list of attached artifacts. + * + * @param artifact The artifact to test. + * + * @return {@code true}, if the list of attached artifacts contains {@code artifact}; {@code false}, else. + * + * @since 3.4 + */ + public boolean isArtifactAttached( final Artifact artifact ) + { + if ( artifact == null ) + { + throw new NullPointerException( "artifact" ); + } + + return this.getAttachedArtifacts().contains( artifact ); } public List<Artifact> getAttachedArtifacts() @@ -930,7 +976,7 @@ public class MavenProject { attachedArtifacts = new ArrayList<>(); } - return attachedArtifacts; + return Collections.unmodifiableList( attachedArtifacts ); } public Xpp3Dom getGoalConfiguration( String pluginGroupId, String pluginArtifactId, String executionId, http://git-wip-us.apache.org/repos/asf/maven/blob/020e3581/maven-core/src/main/java/org/apache/maven/project/MavenProjectHelper.java ---------------------------------------------------------------------- diff --git a/maven-core/src/main/java/org/apache/maven/project/MavenProjectHelper.java b/maven-core/src/main/java/org/apache/maven/project/MavenProjectHelper.java index 0b54c00..73b3f8f 100644 --- a/maven-core/src/main/java/org/apache/maven/project/MavenProjectHelper.java +++ b/maven-core/src/main/java/org/apache/maven/project/MavenProjectHelper.java @@ -34,16 +34,20 @@ public interface MavenProjectHelper * @param project project reference. * @param artifactFile artifact file. * @param artifactClassifier artifact classifier. + * @throws DuplicateArtifactAttachmentException if the same artifact already is attached to this project. */ - void attachArtifact( MavenProject project, File artifactFile, String artifactClassifier ); + void attachArtifact( MavenProject project, File artifactFile, String artifactClassifier ) + throws DuplicateArtifactAttachmentException; /** * * See {@link #attachArtifact(MavenProject, String, String, java.io.File)}, but with classifier set to null. * @param project project reference. * @param artifactType artifact type. * @param artifactFile arrifact file. + * @throws DuplicateArtifactAttachmentException if the same artifact already is attached to this project. */ - void attachArtifact( MavenProject project, String artifactType, File artifactFile ); + void attachArtifact( MavenProject project, String artifactType, File artifactFile ) + throws DuplicateArtifactAttachmentException; /** * Add or replace an artifact to the current project. @@ -51,8 +55,10 @@ public interface MavenProjectHelper * @param artifactType the type (e.g. jar) or null. * @param artifactClassifier the classifier or null. * @param artifactFile the file for the artifact. + * @throws DuplicateArtifactAttachmentException if the same artifact already is attached to this project. */ - void attachArtifact( MavenProject project, String artifactType, String artifactClassifier, File artifactFile ); + void attachArtifact( MavenProject project, String artifactType, String artifactClassifier, File artifactFile ) + throws DuplicateArtifactAttachmentException; /** * Add a resource directory to the project.
