@@ -95,7 +95,7 @@ public class EffectivePomMojo
95
95
96
96
/**
97
97
* Output POM input location as comments.
98
- *
98
+ *
99
99
* @since 3.2.0
100
100
*/
101
101
@ Parameter ( property = "verbose" , defaultValue = "false" )
@@ -105,9 +105,11 @@ public class EffectivePomMojo
105
105
// Public methods
106
106
// ----------------------------------------------------------------------
107
107
108
- /** {@inheritDoc} */
108
+ /**
109
+ * {@inheritDoc}
110
+ */
109
111
public void execute ()
110
- throws MojoExecutionException
112
+ throws MojoExecutionException
111
113
{
112
114
if ( StringUtils .isNotEmpty ( artifact ) )
113
115
{
@@ -213,7 +215,7 @@ private void writeEffectivePom( MavenProject project, XMLWriter writer )
213
215
if ( verbose )
214
216
{
215
217
// 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 ) )
217
219
{
218
220
// xpp3 extended writer not provided by Maven core, use local code
219
221
new EffectiveWriterExOldSupport ().write ( sWriter , pom );
@@ -252,19 +254,19 @@ private static void cleanModel( Model pom )
252
254
private void warnWriteMavenXpp3WriterEx ( Throwable t )
253
255
{
254
256
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 );
256
258
}
257
259
258
- private boolean writeMavenXpp3WriterEx ( Writer writer , Model model )
259
- throws IOException
260
+ private boolean writeMavenXpp3WriterEx ( Writer writer , MavenProject project , Model model )
261
+ throws IOException
260
262
{
261
263
try
262
264
{
263
265
Class <?> mavenXpp3WriterExClass = Class .forName ( "org.apache.maven.model.io.xpp3.MavenXpp3WriterEx" );
264
266
Object mavenXpp3WriterEx = mavenXpp3WriterExClass .getDeclaredConstructor ().newInstance ();
265
267
266
268
Method setStringFormatter =
267
- mavenXpp3WriterExClass .getMethod ( "setStringFormatter" , InputLocation .StringFormatter .class );
269
+ mavenXpp3WriterExClass .getMethod ( "setStringFormatter" , InputLocation .StringFormatter .class );
268
270
setStringFormatter .invoke ( mavenXpp3WriterEx , new InputLocationStringFormatter () );
269
271
270
272
Method write = mavenXpp3WriterExClass .getMethod ( "write" , Writer .class , Model .class );
@@ -298,8 +300,49 @@ else if ( e.getTargetException() instanceof RuntimeException )
298
300
299
301
private static String toString ( InputLocation location )
300
302
{
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
+ }
302
325
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
+ {
303
346
String s = source .getModelId (); // by default, display modelId
304
347
305
348
if ( StringUtils .isBlank ( s ) || s .contains ( "[unknown-version]" ) )
@@ -308,11 +351,11 @@ private static String toString( InputLocation location )
308
351
s = source .toString ();
309
352
}
310
353
311
- return '}' + s + ( ( location . getLineNumber () >= 0 ) ? ", line " + location . getLineNumber () : "" ) + ' ' ;
354
+ return s ;
312
355
}
313
356
314
357
private static class InputLocationStringFormatter
315
- extends InputLocation .StringFormatter
358
+ extends InputLocation .StringFormatter
316
359
{
317
360
318
361
public String toString ( InputLocation location )
@@ -326,7 +369,7 @@ public String toString( InputLocation location )
326
369
* Xpp3 extended writer extension to improve default InputSource display
327
370
*/
328
371
private static class EffectiveWriterExOldSupport
329
- extends MavenXpp3WriterExOldSupport
372
+ extends MavenXpp3WriterExOldSupport
330
373
{
331
374
332
375
@ Override
0 commit comments