Skip to content

Commit

Permalink
[MSOURCES-140] fail only if re-attach different files
Browse files Browse the repository at this point in the history
  • Loading branch information
hboutemy committed Mar 28, 2024
1 parent 7626998 commit 1405ec8
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/it/MSOURCES-121/invoker.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 3 additions & 2 deletions src/it/MSOURCES-121/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -303,15 +304,24 @@ protected void packageSources(List<MavenProject> 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.");
}
Expand All @@ -320,12 +330,14 @@ protected void packageSources(List<MavenProject> 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;
}

/**
Expand Down

0 comments on commit 1405ec8

Please sign in to comment.