23
23
import java .io .InputStream ;
24
24
import java .io .PrintWriter ;
25
25
import java .nio .file .Files ;
26
+ import java .nio .file .Path ;
27
+ import java .nio .file .Paths ;
28
+ import java .nio .file .StandardCopyOption ;
26
29
import java .util .LinkedHashMap ;
27
30
import java .util .Map ;
28
31
import java .util .Properties ;
@@ -162,6 +165,12 @@ void printArtifacts(MavenProject project) throws MojoExecutionException {
162
165
p .println (prefix + "coordinates=" + project .getGroupId () + ':' + project .getArtifactId ());
163
166
}
164
167
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
+
165
174
int n = 0 ;
166
175
Artifact pomArtifact = new DefaultArtifact (
167
176
project .getGroupId (),
@@ -171,13 +180,25 @@ void printArtifacts(MavenProject project) throws MojoExecutionException {
171
180
"pom" ,
172
181
"" ,
173
182
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
+ }
175
196
176
197
artifacts .put (pomArtifact , prefix + n );
177
198
printFile (
178
199
prefix + n ++,
179
200
pomArtifact .getGroupId (),
180
- project .getFile (),
201
+ pomArtifact .getFile (),
181
202
project .getArtifactId () + '-' + project .getVersion () + ".pom" );
182
203
183
204
if (project .getArtifact () == null ) {
@@ -189,6 +210,10 @@ void printArtifacts(MavenProject project) throws MojoExecutionException {
189
210
}
190
211
191
212
for (Artifact attached : project .getAttachedArtifacts ()) {
213
+ if (attached == consumerPom ) {
214
+ // ignore consumer pom
215
+ continue ;
216
+ }
192
217
if (attached .getType ().endsWith (".asc" )) {
193
218
// ignore pgp signatures
194
219
continue ;
0 commit comments