From 5184a1179ed63a7cfc380289a65ac9e0053ec2c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herv=C3=A9=20Boutemy?= Date: Wed, 27 Mar 2024 16:26:57 +0100 Subject: [PATCH] [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 @@ Test for multiple attachments of files 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 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..72188a7 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.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 @@ protected void packageSources(List theProjects) throws MojoExecuti } 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 @@ protected void packageSources(List theProjects) throws MojoExecuti } } - 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; } /**