You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by ol...@apache.org on 2021/03/04 01:33:51 UTC
[maven] branch maven-3.6.x updated: [MNG-5868] No duplicate
artifacts in attached artifacts if attached artifacts already contains the
artifact remove it and add the new one
This is an automated email from the ASF dual-hosted git repository.
olamy pushed a commit to branch maven-3.6.x
in repository https://gitbox.apache.org/repos/asf/maven.git
The following commit(s) were added to refs/heads/maven-3.6.x by this push:
new 9fbd3cf [MNG-5868] No duplicate artifacts in attached artifacts if attached artifacts already contains the artifact remove it and add the new one
9fbd3cf is described below
commit 9fbd3cfa82aa5aa3d45fe0c2891d70b904f5fe34
Author: olivier lamy <ol...@apache.org>
AuthorDate: Mon Jun 1 12:21:13 2020 +1000
[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 <ol...@apache.org>
---
.../org/apache/maven/project/MavenProject.java | 24 ++++++++++++++---
.../java/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 ceb39e2..db0f4a9 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<RemoteRepository> remotePluginRepositories;
- private List<Artifact> attachedArtifacts;
+ private List<Artifact> 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<Artifact> 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 ab073a6..1360d8e 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() );
+ }
+ }
+
}