@@ -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