This is an automated email from the ASF dual-hosted git repository.

hboutemy pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-source-plugin.git

commit 7c92bd7a90b89cfca30f676533245fe572a07111
Author: Hervé Boutemy <hbout...@apache.org>
AuthorDate: Wed Mar 27 16:26:57 2024 +0100

    [MSOURCES-140] fail only if re-attach different files
---
 src/it/MSOURCES-121/invoker.properties             |  2 +-
 src/it/MSOURCES-121/pom.xml                        |  5 ++--
 .../plugins/source/AbstractSourceJarMojo.java      | 34 +++++++++++++++-------
 3 files changed, 27 insertions(+), 14 deletions(-)

diff --git a/src/it/MSOURCES-121/invoker.properties 
b/src/it/MSOURCES-121/invoker.properties
index 2668a60..c43b263 100644
--- a/src/it/MSOURCES-121/invoker.properties
+++ b/src/it/MSOURCES-121/invoker.properties
@@ -14,4 +14,4 @@
 # KIND, either express or implied.  See the License for the
 # specific language governing permissions and limitations
 # under the License.
-invoker.buildResult = failure
+invoker.buildResult = success
diff --git a/src/it/MSOURCES-121/pom.xml b/src/it/MSOURCES-121/pom.xml
index f1274f9..26c61ac 100644
--- a/src/it/MSOURCES-121/pom.xml
+++ b/src/it/MSOURCES-121/pom.xml
@@ -29,9 +29,10 @@
 
   <name>Test for multiple attachments of files</name>
        <description>This build should fail based on the duplicate
-   execution with  the same configuration. This will errornously
-   add the classifier/file twice times.
+   execution with the same configuration. This will erroneously
+   add the classifier/file twice.
    MSOURCES-121.
+   update with MSOURCES-141: do not fail but detect and not add twice
   </description>
 
   <properties>
diff --git 
a/src/main/java/org/apache/maven/plugins/source/AbstractSourceJarMojo.java 
b/src/main/java/org/apache/maven/plugins/source/AbstractSourceJarMojo.java
index b3ead72..54d11a5 100644
--- a/src/main/java/org/apache/maven/plugins/source/AbstractSourceJarMojo.java
+++ b/src/main/java/org/apache/maven/plugins/source/AbstractSourceJarMojo.java
@@ -24,6 +24,7 @@ import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
 import java.util.List;
+import java.util.Objects;
 
 import org.apache.maven.archiver.MavenArchiveConfiguration;
 import org.apache.maven.archiver.MavenArchiver;
@@ -303,15 +304,24 @@ public abstract class AbstractSourceJarMojo extends 
AbstractMojo {
             }
 
             if (attach) {
+                boolean requiresAttach = true;
                 for (Artifact attachedArtifact : 
project.getAttachedArtifacts()) {
-                    if (isAlreadyAttached(attachedArtifact, project, 
getClassifier())) {
-                        getLog().error("We have duplicated artifacts 
attached.");
-                        throw new MojoExecutionException("Presumably you have 
configured maven-source-plugin "
-                                + "to execute twice in your build. You have to 
configure a classifier "
-                                + "for at least one of them.");
+                    Artifact previouslyAttached = 
getPreviouslyAttached(attachedArtifact, project, getClassifier());
+                    if (previouslyAttached != null) {
+                        if (!outputFile.equals(previouslyAttached.getFile())) {
+                            getLog().error("Artifact already attached to a 
file " + previouslyAttached.getFile()
+                                    + ": attach to " + outputFile + " should 
be done with another classifier");
+                            throw new MojoExecutionException("Presumably you 
have configured maven-source-plugin "
+                                    + "to execute twice in your build to 
different output files. "
+                                    + "You have to configure a classifier for 
at least one of them.");
+                        }
+                        requiresAttach = false;
+                        getLog().info("Artifact already attached: ignoring 
re-attach");
                     }
                 }
-                projectHelper.attachArtifact(project, getType(), 
getClassifier(), outputFile);
+                if (requiresAttach) {
+                    projectHelper.attachArtifact(project, getType(), 
getClassifier(), outputFile);
+                }
             } else {
                 getLog().info("NOT adding java-sources to attached artifacts 
list.");
             }
@@ -320,12 +330,14 @@ public abstract class AbstractSourceJarMojo extends 
AbstractMojo {
         }
     }
 
-    private boolean isAlreadyAttached(Artifact artifact, MavenProject 
checkProject, String classifier) {
+    private Artifact getPreviouslyAttached(Artifact artifact, MavenProject 
checkProject, String classifier) {
         return artifact.getType().equals(getType())
-                && artifact.getGroupId().equals(checkProject.getGroupId())
-                && 
artifact.getArtifactId().equals(checkProject.getArtifactId())
-                && artifact.getVersion().equals(checkProject.getVersion())
-                && (artifact.getClassifier() != null ? 
artifact.getClassifier().equals(classifier) : false);
+                        && 
artifact.getGroupId().equals(checkProject.getGroupId())
+                        && 
artifact.getArtifactId().equals(checkProject.getArtifactId())
+                        && 
artifact.getVersion().equals(checkProject.getVersion())
+                        && Objects.equals(artifact.getClassifier(), classifier)
+                ? artifact
+                : null;
     }
 
     /**

Reply via email to