Skip to content

Commit 307fd9a

Browse files
committed
[MPH-183] Print managed dependency hierarchy in effective pom
1 parent 5bc2958 commit 307fd9a

File tree

5 files changed

+142
-17
lines changed

5 files changed

+142
-17
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?xml version='1.0' encoding='UTF-8'?>
2+
3+
<!--
4+
Licensed to the Apache Software Foundation (ASF) under one
5+
or more contributor license agreements. See the NOTICE file
6+
distributed with this work for additional information
7+
regarding copyright ownership. The ASF licenses this file
8+
to you under the Apache License, Version 2.0 (the
9+
"License"); you may not use this file except in compliance
10+
with the License. You may obtain a copy of the License at
11+
12+
http://www.apache.org/licenses/LICENSE-2.0
13+
14+
Unless required by applicable law or agreed to in writing,
15+
software distributed under the License is distributed on an
16+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17+
KIND, either express or implied. See the License for the
18+
specific language governing permissions and limitations
19+
under the License.
20+
-->
21+
22+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
23+
<modelVersion>4.0.0</modelVersion>
24+
25+
<groupId>org.apache.maven.plugins.help.it</groupId>
26+
<artifactId>mph183-org-bom</artifactId>
27+
<version>1.2</version>
28+
<packaging>pom</packaging>
29+
30+
<properties>
31+
<mph183-boot.version>1.0</mph183-boot.version>
32+
</properties>
33+
34+
<dependencyManagement>
35+
<dependencies>
36+
<dependency>
37+
<groupId>org.apache.maven.plugins.help.it</groupId>
38+
<artifactId>mph183-boot-bom</artifactId>
39+
<version>${mph183-boot.version}</version>
40+
<type>pom</type>
41+
<scope>import</scope>
42+
</dependency>
43+
</dependencies>
44+
</dependencyManagement>
45+
</project>

src/it/projects/effective-pom-with-bom/invoker.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,5 @@
1616
# under the License.
1717

1818
invoker.goals = ${project.groupId}:${project.artifactId}:${project.version}:effective-pom
19+
20+
invoker.maven.version = 4.0.0-alpha-1+

src/it/projects/effective-pom-with-bom/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@
3030
<dependencies>
3131
<dependency>
3232
<groupId>org.apache.maven.plugins.help.it</groupId>
33-
<artifactId>mph183-boot-bom</artifactId>
34-
<version>1.0</version>
33+
<artifactId>mph183-org-bom</artifactId>
34+
<version>1.2</version>
3535
<type>pom</type>
3636
<scope>import</scope>
3737
</dependency>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
String content = new File( basedir, "result.txt" ).text
21+
22+
assert content.contains("<!-- org.apache.maven.plugins.help.it:mph183-tool-bom:2.1, line 35 via org.apache.maven.plugins.help.it:mph183-boot-bom:1.0 via org.apache.maven.plugins.help.it:mph183-org-bom:1.2 -->")

src/main/java/org/apache/maven/plugins/help/EffectivePomMojo.java

Lines changed: 71 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import org.apache.maven.plugin.MojoExecution;
3737
import org.apache.maven.plugin.MojoExecution.Source;
3838
import org.apache.maven.plugin.MojoExecutionException;
39+
import org.apache.maven.plugin.logging.Log;
3940
import org.apache.maven.plugins.annotations.Mojo;
4041
import org.apache.maven.plugins.annotations.Parameter;
4142
import org.apache.maven.project.MavenProject;
@@ -95,7 +96,7 @@ public class EffectivePomMojo
9596

9697
/**
9798
* Output POM input location as comments.
98-
*
99+
*
99100
* @since 3.2.0
100101
*/
101102
@Parameter( property = "verbose", defaultValue = "false" )
@@ -105,9 +106,11 @@ public class EffectivePomMojo
105106
// Public methods
106107
// ----------------------------------------------------------------------
107108

108-
/** {@inheritDoc} */
109+
/**
110+
* {@inheritDoc}
111+
*/
109112
public void execute()
110-
throws MojoExecutionException
113+
throws MojoExecutionException
111114
{
112115
if ( StringUtils.isNotEmpty( artifact ) )
113116
{
@@ -213,7 +216,7 @@ private void writeEffectivePom( MavenProject project, XMLWriter writer )
213216
if ( verbose )
214217
{
215218
// try to use Maven core-provided xpp3 extended writer (available since Maven 3.6.1)
216-
if ( ! writeMavenXpp3WriterEx( sWriter, pom ) )
219+
if ( !writeMavenXpp3WriterEx( sWriter, project, pom ) )
217220
{
218221
// xpp3 extended writer not provided by Maven core, use local code
219222
new EffectiveWriterExOldSupport().write( sWriter, pom );
@@ -252,20 +255,20 @@ private static void cleanModel( Model pom )
252255
private void warnWriteMavenXpp3WriterEx( Throwable t )
253256
{
254257
getLog().warn( "Unexpected exception while running Maven Model Extended Writer, "
255-
+ "falling back to old internal implementation.", t );
258+
+ "falling back to old internal implementation.", t );
256259
}
257260

258-
private boolean writeMavenXpp3WriterEx( Writer writer, Model model )
259-
throws IOException
261+
private boolean writeMavenXpp3WriterEx( Writer writer, MavenProject project, Model model )
262+
throws IOException
260263
{
261264
try
262265
{
263266
Class<?> mavenXpp3WriterExClass = Class.forName( "org.apache.maven.model.io.xpp3.MavenXpp3WriterEx" );
264267
Object mavenXpp3WriterEx = mavenXpp3WriterExClass.getDeclaredConstructor().newInstance();
265268

266269
Method setStringFormatter =
267-
mavenXpp3WriterExClass.getMethod( "setStringFormatter", InputLocation.StringFormatter.class );
268-
setStringFormatter.invoke( mavenXpp3WriterEx, new InputLocationStringFormatter() );
270+
mavenXpp3WriterExClass.getMethod( "setStringFormatter", InputLocation.StringFormatter.class );
271+
setStringFormatter.invoke( mavenXpp3WriterEx, new InputLocationStringFormatter( getLog() ) );
269272

270273
Method write = mavenXpp3WriterExClass.getMethod( "write", Writer.class, Model.class );
271274
write.invoke( mavenXpp3WriterEx, writer, model );
@@ -296,10 +299,56 @@ else if ( e.getTargetException() instanceof RuntimeException )
296299
return false;
297300
}
298301

299-
private static String toString( InputLocation location )
302+
private static String toString( InputLocation location, Log log )
300303
{
301-
InputSource source = location.getSource();
304+
String source = toString( location.getSource() );
305+
String hierarchy = toHierarchyString( location.getSource(), log );
306+
307+
return '}' + source + ( ( location.getLineNumber() >= 0 ) ? ", line " + location.getLineNumber() : "" )
308+
+ hierarchy + ' ';
309+
}
310+
311+
private static String toHierarchyString( InputSource source, Log log )
312+
{
313+
StringBuilder sb = new StringBuilder();
314+
315+
try
316+
{
317+
// InputSource#getImportedBy() added in maven-model 4.0.0-alpha-1
318+
Method getImportedBy = InputSource.class.getDeclaredMethod( "getImportedBy" );
319+
InputSource importedBy = (InputSource) getImportedBy.invoke( source );
320+
321+
while ( importedBy != null )
322+
{
323+
sb.append( " via " ).append( toString( importedBy ) );
324+
importedBy = (InputSource) getImportedBy.invoke( importedBy );
325+
}
326+
327+
return sb.toString();
328+
}
329+
catch ( NoSuchMethodException e )
330+
{
331+
log.debug( "Unable to print hierarchy of imports, falling back to printing the source POM without "
332+
+ "import information. This feature is available in Maven 4.0.0+." );
333+
}
334+
catch ( SecurityException | IllegalArgumentException | IllegalAccessException | ClassCastException e )
335+
{
336+
log.warn( "Unexpected exception while fetching hierarchy of imports, falling back to printing the "
337+
+ "source POM without import information.", e );
338+
}
339+
catch ( InvocationTargetException e )
340+
{
341+
if ( e.getTargetException() instanceof RuntimeException )
342+
{
343+
throw (RuntimeException) e.getTargetException();
344+
}
345+
}
302346

347+
return "";
348+
}
349+
350+
private static String toString( InputSource source )
351+
{
303352
String s = source.getModelId(); // by default, display modelId
304353

305354
if ( StringUtils.isBlank( s ) || s.contains( "[unknown-version]" ) )
@@ -308,16 +357,23 @@ private static String toString( InputLocation location )
308357
s = source.toString();
309358
}
310359

311-
return '}' + s + ( ( location.getLineNumber() >= 0 ) ? ", line " + location.getLineNumber() : "" ) + ' ';
360+
return s;
312361
}
313362

314363
private static class InputLocationStringFormatter
315-
extends InputLocation.StringFormatter
364+
extends InputLocation.StringFormatter
316365
{
317366

367+
private Log log;
368+
369+
public InputLocationStringFormatter( Log log )
370+
{
371+
this.log = log;
372+
}
373+
318374
public String toString( InputLocation location )
319375
{
320-
return EffectivePomMojo.toString( location );
376+
return EffectivePomMojo.toString( location, log );
321377
}
322378

323379
}
@@ -326,7 +382,7 @@ public String toString( InputLocation location )
326382
* Xpp3 extended writer extension to improve default InputSource display
327383
*/
328384
private static class EffectiveWriterExOldSupport
329-
extends MavenXpp3WriterExOldSupport
385+
extends MavenXpp3WriterExOldSupport
330386
{
331387

332388
@Override

0 commit comments

Comments
 (0)