From bc9e7eb687be7cec8916d03a43ebe07b79cc7562 Mon Sep 17 00:00:00 2001 From: olivier lamy Date: Mon, 1 Jun 2020 12:21:13 +1000 Subject: [PATCH] [MNG-5868] No duplicate artifacts in attached artifacts if attached artifacts already contains the artifact remove it and add the new one Signed-off-by: olivier lamy --- .../apache/maven/project/MavenProject.java | 24 +++++++++++--- .../org/apache/maven/DefaultMavenTest.java | 31 ++++++++++++++++++- 2 files changed, 50 insertions(+), 5 deletions(-) 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 896d6413cb..3cf7bd1428 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 @@ -72,6 +72,8 @@ import org.codehaus.plexus.classworlds.realm.ClassRealm; import org.codehaus.plexus.util.xml.Xpp3Dom; import org.eclipse.aether.graph.DependencyFilter; import org.eclipse.aether.repository.RemoteRepository; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * The concern of the project is provide runtime values based on the model. @@ -90,6 +92,9 @@ import org.eclipse.aether.repository.RemoteRepository; public class MavenProject implements Cloneable { + + private static final Logger LOGGER = LoggerFactory.getLogger( MavenProject.class ); + public static final String EMPTY_PROJECT_GROUP_ID = "unknown"; public static final String EMPTY_PROJECT_ARTIFACT_ID = "empty-project"; @@ -122,7 +127,7 @@ public class MavenProject private List remotePluginRepositories; - private List attachedArtifacts; + private List attachedArtifacts = new ArrayList<>(); private MavenProject executionProject; @@ -921,12 +926,23 @@ public class MavenProject * coordinates. * * @param artifact the artifact to add or replace. - * @throws DuplicateArtifactAttachmentException + * @deprecated Please use {@link MavenProjectHelper} + * @throws DuplicateArtifactAttachmentException will never happen but leave it for backward compatibility */ public void addAttachedArtifact( Artifact artifact ) throws DuplicateArtifactAttachmentException { - getAttachedArtifacts().add( artifact ); + // if already there we remove it and add again + int index = attachedArtifacts.indexOf( artifact ); + if ( index >= 0 ) + { + LOGGER.warn( "artifact {} already attached, replace previous instance", artifact ); + attachedArtifacts.set( index, artifact ); + } + else + { + attachedArtifacts.add( artifact ); + } } public List getAttachedArtifacts() @@ -935,7 +951,7 @@ public class MavenProject { attachedArtifacts = new ArrayList<>(); } - return attachedArtifacts; + return Collections.unmodifiableList( attachedArtifacts ); } public Xpp3Dom getGoalConfiguration( String pluginGroupId, String pluginArtifactId, String executionId, diff --git a/maven-core/src/test/java/org/apache/maven/DefaultMavenTest.java b/maven-core/src/test/java/org/apache/maven/DefaultMavenTest.java index ab073a6324..1360d8e623 100644 --- a/maven-core/src/test/java/org/apache/maven/DefaultMavenTest.java +++ b/maven-core/src/test/java/org/apache/maven/DefaultMavenTest.java @@ -1,7 +1,14 @@ package org.apache.maven; +import org.apache.maven.artifact.Artifact; +import org.apache.maven.artifact.DefaultArtifact; import org.apache.maven.execution.MavenExecutionRequest; import org.apache.maven.execution.MavenExecutionResult; +import org.apache.maven.project.MavenProject; +import org.apache.maven.project.MavenProjectHelper; + +import java.io.File; +import java.nio.file.Files; import static java.util.Arrays.asList; @@ -23,7 +30,9 @@ import static java.util.Arrays.asList; * specific language governing permissions and limitations * under the License. */ -public class DefaultMavenTest extends AbstractCoreMavenComponentTestCase{ +public class DefaultMavenTest + extends AbstractCoreMavenComponentTestCase +{ public void testThatErrorDuringProjectDependencyGraphCreationAreStored() throws Exception @@ -42,4 +51,24 @@ public class DefaultMavenTest extends AbstractCoreMavenComponentTestCase{ return "src/test/projects/default-maven"; } + + public void testMavenProjectNoDuplicateArtifacts() + throws Exception + { + MavenProjectHelper mavenProjectHelper = lookup( MavenProjectHelper.class ); + MavenProject mavenProject = new MavenProject(); + mavenProject.setArtifact( new DefaultArtifact( "g", "a", "1.0", Artifact.SCOPE_TEST, "jar", "", null ) ); + File artifactFile = Files.createTempFile( "foo", "tmp").toFile(); + try + { + mavenProjectHelper.attachArtifact( mavenProject, "sources", artifactFile ); + assertEquals( 1, mavenProject.getAttachedArtifacts().size() ); + mavenProjectHelper.attachArtifact( mavenProject, "sources", artifactFile ); + assertEquals( 1, mavenProject.getAttachedArtifacts().size() ); + } finally + { + Files.deleteIfExists( artifactFile.toPath() ); + } + } + }