Skip to content

Commit 1405ec8

Browse files
committed
[MSOURCES-140] fail only if re-attach different files
1 parent 7626998 commit 1405ec8

File tree

3 files changed

+27
-14
lines changed

3 files changed

+27
-14
lines changed

src/it/MSOURCES-121/invoker.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@
1414
# KIND, either express or implied. See the License for the
1515
# specific language governing permissions and limitations
1616
# under the License.
17-
invoker.buildResult = failure
17+
invoker.buildResult = success

src/it/MSOURCES-121/pom.xml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,10 @@
2929

3030
<name>Test for multiple attachments of files</name>
3131
<description>This build should fail based on the duplicate
32-
execution with the same configuration. This will errornously
33-
add the classifier/file twice times.
32+
execution with the same configuration. This will erroneously
33+
add the classifier/file twice.
3434
MSOURCES-121.
35+
update with MSOURCES-141: do not fail but detect and not add twice
3536
</description>
3637

3738
<properties>

src/main/java/org/apache/maven/plugins/source/AbstractSourceJarMojo.java

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.Arrays;
2525
import java.util.Collections;
2626
import java.util.List;
27+
import java.util.Objects;
2728

2829
import org.apache.maven.archiver.MavenArchiveConfiguration;
2930
import 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

Comments
 (0)