Skip to content

Commit

Permalink
[MARTIFACT-49] Supports the build/consumer feature (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet authored and hboutemy committed Sep 19, 2023
1 parent 90281bc commit e5b7f8c
Showing 1 changed file with 27 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
import java.io.InputStream;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Properties;
Expand Down Expand Up @@ -162,6 +165,12 @@ void printArtifacts(MavenProject project) throws MojoExecutionException {
p.println(prefix + "coordinates=" + project.getGroupId() + ':' + project.getArtifactId());
}

// detect Maven 4 consumer POM transient attachment
Artifact consumerPom = project.getAttachedArtifacts().stream()
.filter(a -> "pom".equals(a.getType()) && "consumer".equals(a.getClassifier()))
.findAny()
.orElse(null);

int n = 0;
Artifact pomArtifact = new DefaultArtifact(
project.getGroupId(),
Expand All @@ -171,13 +180,25 @@ void printArtifacts(MavenProject project) throws MojoExecutionException {
"pom",
"",
artifactHandlerManager.getArtifactHandler("pom"));
pomArtifact.setFile(project.getFile());
if (consumerPom != null) {
// Maven 4 transient consumer POM attachment is published as the POM, overrides build POM, see
// https://github.com/apache/maven/blob/c79a7a02721f0f9fd7e202e99f60b593461ba8cc/maven-core/src/main/java/org/apache/maven/internal/transformation/ConsumerPomArtifactTransformer.java#L130-L155
try {
Path pomFile = Files.createTempFile(Paths.get(project.getBuild().getDirectory()), "consumer-", ".pom");
Files.copy(consumerPom.getFile().toPath(), pomFile, StandardCopyOption.REPLACE_EXISTING);
pomArtifact.setFile(pomFile.toFile());
} catch (IOException e) {
p.println("Error processing consumer POM: " + e);
}
} else {
pomArtifact.setFile(project.getFile());
}

artifacts.put(pomArtifact, prefix + n);
printFile(
prefix + n++,
pomArtifact.getGroupId(),
project.getFile(),
pomArtifact.getFile(),
project.getArtifactId() + '-' + project.getVersion() + ".pom");

if (project.getArtifact() == null) {
Expand All @@ -189,6 +210,10 @@ void printArtifacts(MavenProject project) throws MojoExecutionException {
}

for (Artifact attached : project.getAttachedArtifacts()) {
if (attached == consumerPom) {
// ignore consumer pom
continue;
}
if (attached.getType().endsWith(".asc")) {
// ignore pgp signatures
continue;
Expand Down

0 comments on commit e5b7f8c

Please sign in to comment.