2424import java .util .Arrays ;
2525import java .util .Collections ;
2626import java .util .List ;
27+ import java .util .Objects ;
2728
2829import org .apache .maven .archiver .MavenArchiveConfiguration ;
2930import org .apache .maven .archiver .MavenArchiver ;
@@ -303,15 +304,24 @@ protected void packageSources(List<MavenProject> theProjects) throws MojoExecuti
303304 }
304305
305306 if (attach ) {
307+ boolean requiresAttach = true ;
306308 for (Artifact attachedArtifact : project .getAttachedArtifacts ()) {
307- if (isAlreadyAttached (attachedArtifact , project , getClassifier ())) {
308- getLog ().error ("We have duplicated artifacts attached." );
309- throw new MojoExecutionException ("Presumably you have configured maven-source-plugin "
310- + "to execute twice in your build. You have to configure a classifier "
311- + "for at least one of them." );
309+ Artifact previouslyAttached = getPreviouslyAttached (attachedArtifact , project , getClassifier ());
310+ if (previouslyAttached != null ) {
311+ if (!outputFile .equals (previouslyAttached .getFile ())) {
312+ getLog ().error ("Artifact already attached to a file " + previouslyAttached .getFile ()
313+ + ": attach to " + outputFile + " should be done with another classifier" );
314+ throw new MojoExecutionException ("Presumably you have configured maven-source-plugin "
315+ + "to execute twice in your build to different output files. "
316+ + "You have to configure a classifier for at least one of them." );
317+ }
318+ requiresAttach = false ;
319+ getLog ().info ("Artifact already attached: ignoring re-attach" );
312320 }
313321 }
314- projectHelper .attachArtifact (project , getType (), getClassifier (), outputFile );
322+ if (requiresAttach ) {
323+ projectHelper .attachArtifact (project , getType (), getClassifier (), outputFile );
324+ }
315325 } else {
316326 getLog ().info ("NOT adding java-sources to attached artifacts list." );
317327 }
@@ -320,12 +330,14 @@ protected void packageSources(List<MavenProject> theProjects) throws MojoExecuti
320330 }
321331 }
322332
323- private boolean isAlreadyAttached (Artifact artifact , MavenProject checkProject , String classifier ) {
333+ private Artifact getPreviouslyAttached (Artifact artifact , MavenProject checkProject , String classifier ) {
324334 return artifact .getType ().equals (getType ())
325- && artifact .getGroupId ().equals (checkProject .getGroupId ())
326- && artifact .getArtifactId ().equals (checkProject .getArtifactId ())
327- && artifact .getVersion ().equals (checkProject .getVersion ())
328- && (artifact .getClassifier () != null ? artifact .getClassifier ().equals (classifier ) : false );
335+ && artifact .getGroupId ().equals (checkProject .getGroupId ())
336+ && artifact .getArtifactId ().equals (checkProject .getArtifactId ())
337+ && artifact .getVersion ().equals (checkProject .getVersion ())
338+ && Objects .equals (artifact .getClassifier (), classifier )
339+ ? artifact
340+ : null ;
329341 }
330342
331343 /**
0 commit comments