Skip to content

Commit e5b7f8c

Browse files
gnodethboutemy
authored andcommitted
[MARTIFACT-49] Supports the build/consumer feature (#18)
1 parent 90281bc commit e5b7f8c

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
lines changed

src/main/java/org/apache/maven/plugins/artifact/buildinfo/BuildInfoWriter.java

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
import java.io.InputStream;
2424
import java.io.PrintWriter;
2525
import java.nio.file.Files;
26+
import java.nio.file.Path;
27+
import java.nio.file.Paths;
28+
import java.nio.file.StandardCopyOption;
2629
import java.util.LinkedHashMap;
2730
import java.util.Map;
2831
import java.util.Properties;
@@ -162,6 +165,12 @@ void printArtifacts(MavenProject project) throws MojoExecutionException {
162165
p.println(prefix + "coordinates=" + project.getGroupId() + ':' + project.getArtifactId());
163166
}
164167

168+
// detect Maven 4 consumer POM transient attachment
169+
Artifact consumerPom = project.getAttachedArtifacts().stream()
170+
.filter(a -> "pom".equals(a.getType()) && "consumer".equals(a.getClassifier()))
171+
.findAny()
172+
.orElse(null);
173+
165174
int n = 0;
166175
Artifact pomArtifact = new DefaultArtifact(
167176
project.getGroupId(),
@@ -171,13 +180,25 @@ void printArtifacts(MavenProject project) throws MojoExecutionException {
171180
"pom",
172181
"",
173182
artifactHandlerManager.getArtifactHandler("pom"));
174-
pomArtifact.setFile(project.getFile());
183+
if (consumerPom != null) {
184+
// Maven 4 transient consumer POM attachment is published as the POM, overrides build POM, see
185+
// https://github.com/apache/maven/blob/c79a7a02721f0f9fd7e202e99f60b593461ba8cc/maven-core/src/main/java/org/apache/maven/internal/transformation/ConsumerPomArtifactTransformer.java#L130-L155
186+
try {
187+
Path pomFile = Files.createTempFile(Paths.get(project.getBuild().getDirectory()), "consumer-", ".pom");
188+
Files.copy(consumerPom.getFile().toPath(), pomFile, StandardCopyOption.REPLACE_EXISTING);
189+
pomArtifact.setFile(pomFile.toFile());
190+
} catch (IOException e) {
191+
p.println("Error processing consumer POM: " + e);
192+
}
193+
} else {
194+
pomArtifact.setFile(project.getFile());
195+
}
175196

176197
artifacts.put(pomArtifact, prefix + n);
177198
printFile(
178199
prefix + n++,
179200
pomArtifact.getGroupId(),
180-
project.getFile(),
201+
pomArtifact.getFile(),
181202
project.getArtifactId() + '-' + project.getVersion() + ".pom");
182203

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

191212
for (Artifact attached : project.getAttachedArtifacts()) {
213+
if (attached == consumerPom) {
214+
// ignore consumer pom
215+
continue;
216+
}
192217
if (attached.getType().endsWith(".asc")) {
193218
// ignore pgp signatures
194219
continue;

0 commit comments

Comments
 (0)