36
36
import org .apache .maven .plugin .MojoExecution ;
37
37
import org .apache .maven .plugin .MojoExecution .Source ;
38
38
import org .apache .maven .plugin .MojoExecutionException ;
39
+ import org .apache .maven .plugin .logging .Log ;
39
40
import org .apache .maven .plugins .annotations .Mojo ;
40
41
import org .apache .maven .plugins .annotations .Parameter ;
41
42
import org .apache .maven .project .MavenProject ;
@@ -95,7 +96,7 @@ public class EffectivePomMojo
95
96
96
97
/**
97
98
* Output POM input location as comments.
98
- *
99
+ *
99
100
* @since 3.2.0
100
101
*/
101
102
@ Parameter ( property = "verbose" , defaultValue = "false" )
@@ -105,9 +106,11 @@ public class EffectivePomMojo
105
106
// Public methods
106
107
// ----------------------------------------------------------------------
107
108
108
- /** {@inheritDoc} */
109
+ /**
110
+ * {@inheritDoc}
111
+ */
109
112
public void execute ()
110
- throws MojoExecutionException
113
+ throws MojoExecutionException
111
114
{
112
115
if ( StringUtils .isNotEmpty ( artifact ) )
113
116
{
@@ -213,7 +216,7 @@ private void writeEffectivePom( MavenProject project, XMLWriter writer )
213
216
if ( verbose )
214
217
{
215
218
// 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 ) )
217
220
{
218
221
// xpp3 extended writer not provided by Maven core, use local code
219
222
new EffectiveWriterExOldSupport ().write ( sWriter , pom );
@@ -252,20 +255,20 @@ private static void cleanModel( Model pom )
252
255
private void warnWriteMavenXpp3WriterEx ( Throwable t )
253
256
{
254
257
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 );
256
259
}
257
260
258
- private boolean writeMavenXpp3WriterEx ( Writer writer , Model model )
259
- throws IOException
261
+ private boolean writeMavenXpp3WriterEx ( Writer writer , MavenProject project , Model model )
262
+ throws IOException
260
263
{
261
264
try
262
265
{
263
266
Class <?> mavenXpp3WriterExClass = Class .forName ( "org.apache.maven.model.io.xpp3.MavenXpp3WriterEx" );
264
267
Object mavenXpp3WriterEx = mavenXpp3WriterExClass .getDeclaredConstructor ().newInstance ();
265
268
266
269
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 () ) );
269
272
270
273
Method write = mavenXpp3WriterExClass .getMethod ( "write" , Writer .class , Model .class );
271
274
write .invoke ( mavenXpp3WriterEx , writer , model );
@@ -296,10 +299,56 @@ else if ( e.getTargetException() instanceof RuntimeException )
296
299
return false ;
297
300
}
298
301
299
- private static String toString ( InputLocation location )
302
+ private static String toString ( InputLocation location , Log log )
300
303
{
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
+ }
302
346
347
+ return "" ;
348
+ }
349
+
350
+ private static String toString ( InputSource source )
351
+ {
303
352
String s = source .getModelId (); // by default, display modelId
304
353
305
354
if ( StringUtils .isBlank ( s ) || s .contains ( "[unknown-version]" ) )
@@ -308,16 +357,23 @@ private static String toString( InputLocation location )
308
357
s = source .toString ();
309
358
}
310
359
311
- return '}' + s + ( ( location . getLineNumber () >= 0 ) ? ", line " + location . getLineNumber () : "" ) + ' ' ;
360
+ return s ;
312
361
}
313
362
314
363
private static class InputLocationStringFormatter
315
- extends InputLocation .StringFormatter
364
+ extends InputLocation .StringFormatter
316
365
{
317
366
367
+ private Log log ;
368
+
369
+ public InputLocationStringFormatter ( Log log )
370
+ {
371
+ this .log = log ;
372
+ }
373
+
318
374
public String toString ( InputLocation location )
319
375
{
320
- return EffectivePomMojo .toString ( location );
376
+ return EffectivePomMojo .toString ( location , log );
321
377
}
322
378
323
379
}
@@ -326,7 +382,7 @@ public String toString( InputLocation location )
326
382
* Xpp3 extended writer extension to improve default InputSource display
327
383
*/
328
384
private static class EffectiveWriterExOldSupport
329
- extends MavenXpp3WriterExOldSupport
385
+ extends MavenXpp3WriterExOldSupport
330
386
{
331
387
332
388
@ Override
0 commit comments