[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 <olamy@apache.org>
This commit is contained in:
olivier lamy 2020-06-01 12:21:13 +10:00 committed by Olivier Lamy
parent cde15f4ef2
commit 295c205a65
2 changed files with 50 additions and 5 deletions

View File

@ -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,

View File

@ -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() );
}
}
}