Skip to content

Commit 2333232

Browse files
committed
[MPH-183] Print managed dependency hierarchy in effective pom
1 parent 3e258a4 commit 2333232

File tree

4 files changed

+124
-14
lines changed

4 files changed

+124
-14
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/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: 55 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public class EffectivePomMojo
9595

9696
/**
9797
* Output POM input location as comments.
98-
*
98+
*
9999
* @since 3.2.0
100100
*/
101101
@Parameter( property = "verbose", defaultValue = "false" )
@@ -105,9 +105,11 @@ public class EffectivePomMojo
105105
// Public methods
106106
// ----------------------------------------------------------------------
107107

108-
/** {@inheritDoc} */
108+
/**
109+
* {@inheritDoc}
110+
*/
109111
public void execute()
110-
throws MojoExecutionException
112+
throws MojoExecutionException
111113
{
112114
if ( StringUtils.isNotEmpty( artifact ) )
113115
{
@@ -213,7 +215,7 @@ private void writeEffectivePom( MavenProject project, XMLWriter writer )
213215
if ( verbose )
214216
{
215217
// try to use Maven core-provided xpp3 extended writer (available since Maven 3.6.1)
216-
if ( ! writeMavenXpp3WriterEx( sWriter, pom ) )
218+
if ( !writeMavenXpp3WriterEx( sWriter, project, pom ) )
217219
{
218220
// xpp3 extended writer not provided by Maven core, use local code
219221
new EffectiveWriterExOldSupport().write( sWriter, pom );
@@ -252,19 +254,19 @@ private static void cleanModel( Model pom )
252254
private void warnWriteMavenXpp3WriterEx( Throwable t )
253255
{
254256
getLog().warn( "Unexpected exception while running Maven Model Extended Writer, "
255-
+ "falling back to old internal implementation.", t );
257+
+ "falling back to old internal implementation.", t );
256258
}
257259

258-
private boolean writeMavenXpp3WriterEx( Writer writer, Model model )
259-
throws IOException
260+
private boolean writeMavenXpp3WriterEx( Writer writer, MavenProject project, Model model )
261+
throws IOException
260262
{
261263
try
262264
{
263265
Class<?> mavenXpp3WriterExClass = Class.forName( "org.apache.maven.model.io.xpp3.MavenXpp3WriterEx" );
264266
Object mavenXpp3WriterEx = mavenXpp3WriterExClass.getDeclaredConstructor().newInstance();
265267

266268
Method setStringFormatter =
267-
mavenXpp3WriterExClass.getMethod( "setStringFormatter", InputLocation.StringFormatter.class );
269+
mavenXpp3WriterExClass.getMethod( "setStringFormatter", InputLocation.StringFormatter.class );
268270
setStringFormatter.invoke( mavenXpp3WriterEx, new InputLocationStringFormatter() );
269271

270272
Method write = mavenXpp3WriterExClass.getMethod( "write", Writer.class, Model.class );
@@ -298,8 +300,49 @@ else if ( e.getTargetException() instanceof RuntimeException )
298300

299301
private static String toString( InputLocation location )
300302
{
301-
InputSource source = location.getSource();
303+
String source = toString( location.getSource() );
304+
String hierarchy = toHierarchyString( location.getSource() );
305+
306+
return '}' + source + ( ( location.getLineNumber() >= 0 ) ? ", line " + location.getLineNumber() : "" )
307+
+ hierarchy + ' ';
308+
}
309+
310+
private static String toHierarchyString( InputSource source )
311+
{
312+
StringBuilder sb = new StringBuilder();
313+
314+
try
315+
{
316+
// InputSource#getImportedBy() added in maven-model 4.0.0-alpha-1
317+
Method getImportedBy = InputSource.class.getDeclaredMethod( "getImportedBy" );
318+
InputSource importedBy = (InputSource) getImportedBy.invoke( source );
319+
320+
while ( importedBy != null )
321+
{
322+
sb.append( " via " ).append( toString( importedBy ) );
323+
importedBy = (InputSource) getImportedBy.invoke( importedBy );
324+
}
302325

326+
return sb.toString();
327+
}
328+
catch ( NoSuchMethodException | SecurityException | IllegalArgumentException | IllegalAccessException
329+
| ClassCastException e )
330+
{
331+
// Just continue without hierarchy
332+
}
333+
catch ( InvocationTargetException e )
334+
{
335+
if ( e.getTargetException() instanceof RuntimeException )
336+
{
337+
throw (RuntimeException) e.getTargetException();
338+
}
339+
}
340+
341+
return "";
342+
}
343+
344+
private static String toString( InputSource source )
345+
{
303346
String s = source.getModelId(); // by default, display modelId
304347

305348
if ( StringUtils.isBlank( s ) || s.contains( "[unknown-version]" ) )
@@ -308,11 +351,11 @@ private static String toString( InputLocation location )
308351
s = source.toString();
309352
}
310353

311-
return '}' + s + ( ( location.getLineNumber() >= 0 ) ? ", line " + location.getLineNumber() : "" ) + ' ';
354+
return s;
312355
}
313356

314357
private static class InputLocationStringFormatter
315-
extends InputLocation.StringFormatter
358+
extends InputLocation.StringFormatter
316359
{
317360

318361
public String toString( InputLocation location )
@@ -326,7 +369,7 @@ public String toString( InputLocation location )
326369
* Xpp3 extended writer extension to improve default InputSource display
327370
*/
328371
private static class EffectiveWriterExOldSupport
329-
extends MavenXpp3WriterExOldSupport
372+
extends MavenXpp3WriterExOldSupport
330373
{
331374

332375
@Override

0 commit comments

Comments
 (0)