20
20
21
21
import java .io .IOException ;
22
22
import java .io .StringWriter ;
23
- import java .io .Writer ;
24
- import java .lang .reflect .InvocationTargetException ;
25
- import java .lang .reflect .Method ;
26
23
import java .util .Collections ;
27
24
import java .util .List ;
28
25
import java .util .Properties ;
31
28
import org .apache .maven .model .InputSource ;
32
29
import org .apache .maven .model .Model ;
33
30
import org .apache .maven .model .io .xpp3 .MavenXpp3Writer ;
34
- import org .apache .maven .model .io .xpp3 .MavenXpp3WriterExOldSupport ;
31
+ import org .apache .maven .model .io .xpp3 .MavenXpp3WriterEx ;
35
32
import org .apache .maven .plugin .MojoExecution ;
36
33
import org .apache .maven .plugin .MojoExecution .Source ;
37
34
import org .apache .maven .plugin .MojoExecutionException ;
43
40
import org .codehaus .plexus .util .xml .PrettyPrintXMLWriter ;
44
41
import org .codehaus .plexus .util .xml .XMLWriter ;
45
42
import org .codehaus .plexus .util .xml .XmlWriterUtil ;
46
- import org .codehaus .plexus .util .xml .Xpp3Dom ;
47
- import org .codehaus .plexus .util .xml .pull .XmlSerializer ;
48
43
49
44
/**
50
45
* Displays the effective POM as an XML for this build, with the active profiles factored in, or a specified artifact.
@@ -187,11 +182,9 @@ private void writeEffectivePom(MavenProject project, XMLWriter writer) throws Mo
187
182
StringWriter sWriter = new StringWriter ();
188
183
try {
189
184
if (verbose ) {
190
- // try to use Maven core-provided xpp3 extended writer (available since Maven 3.6.1)
191
- if (!writeMavenXpp3WriterEx (sWriter , pom )) {
192
- // xpp3 extended writer not provided by Maven core, use local code
193
- new EffectiveWriterExOldSupport ().write (sWriter , pom );
194
- }
185
+ MavenXpp3WriterEx mavenXpp3WriterEx = new MavenXpp3WriterEx ();
186
+ mavenXpp3WriterEx .setStringFormatter (new InputLocationStringFormatter ());
187
+ mavenXpp3WriterEx .write (sWriter , pom );
195
188
} else {
196
189
new MavenXpp3Writer ().write (sWriter , pom );
197
190
}
@@ -218,81 +211,18 @@ private static void cleanModel(Model pom) {
218
211
pom .setProperties (properties );
219
212
}
220
213
221
- private void warnWriteMavenXpp3WriterEx (Throwable t ) {
222
- getLog ().warn (
223
- "Unexpected exception while running Maven Model Extended Writer, "
224
- + "falling back to old internal implementation." ,
225
- t );
226
- }
227
-
228
- private boolean writeMavenXpp3WriterEx (Writer writer , Model model ) throws IOException {
229
- try {
230
- Class <?> mavenXpp3WriterExClass = Class .forName ("org.apache.maven.model.io.xpp3.MavenXpp3WriterEx" );
231
- Object mavenXpp3WriterEx =
232
- mavenXpp3WriterExClass .getDeclaredConstructor ().newInstance ();
233
-
234
- Method setStringFormatter =
235
- mavenXpp3WriterExClass .getMethod ("setStringFormatter" , InputLocation .StringFormatter .class );
236
- setStringFormatter .invoke (mavenXpp3WriterEx , new InputLocationStringFormatter ());
237
-
238
- Method write = mavenXpp3WriterExClass .getMethod ("write" , Writer .class , Model .class );
239
- write .invoke (mavenXpp3WriterEx , writer , model );
240
-
241
- return true ;
242
- } catch (ClassNotFoundException e ) {
243
- // MavenXpp3WriterEx not available in running Maven version
244
- } catch (NoSuchMethodException
245
- | SecurityException
246
- | IllegalArgumentException
247
- | IllegalAccessException
248
- | InstantiationException e ) {
249
- warnWriteMavenXpp3WriterEx (e );
250
- } catch (InvocationTargetException e ) {
251
- if (e .getTargetException () instanceof IOException ) {
252
- throw (IOException ) e .getTargetException ();
253
- } else if (e .getTargetException () instanceof RuntimeException ) {
254
- throw (RuntimeException ) e .getTargetException ();
255
- }
256
- warnWriteMavenXpp3WriterEx (e );
257
- }
258
- return false ;
259
- }
260
-
261
- private static String toString (InputLocation location ) {
262
- InputSource source = location .getSource ();
263
-
264
- String s = source .getModelId (); // by default, display modelId
265
-
266
- if (StringUtils .isBlank (s ) || s .contains ("[unknown-version]" )) {
267
- // unless it is blank or does not provide version information
268
- s = source .toString ();
269
- }
270
-
271
- return '}' + s + ((location .getLineNumber () >= 0 ) ? ", line " + location .getLineNumber () : "" ) + ' ' ;
272
- }
273
-
274
214
private static class InputLocationStringFormatter extends InputLocation .StringFormatter {
275
-
215
+ @ Override
276
216
public String toString (InputLocation location ) {
277
- return EffectivePomMojo .toString (location );
278
- }
279
- }
217
+ InputSource source = location .getSource ();
280
218
281
- /**
282
- * Xpp3 extended writer extension to improve default InputSource display
283
- */
284
- private static class EffectiveWriterExOldSupport extends MavenXpp3WriterExOldSupport {
219
+ String s = source .getLocation (); // by default, display the path
285
220
286
- @ Override
287
- public String toString (InputLocation location ) {
288
- return EffectivePomMojo .toString (location );
289
- }
221
+ if (s == null || s .trim ().isEmpty ()) {
222
+ s = source .getModelId ();
223
+ }
290
224
291
- @ Override
292
- protected void writeXpp3DomToSerializer (Xpp3Dom dom , XmlSerializer serializer ) throws IOException {
293
- // default method uses Xpp3Dom input location tracking, not available in older Maven versions
294
- // use old Xpp3Dom serialization, without input location tracking
295
- dom .writeToSerializer (null , serializer );
225
+ return '}' + s + ((location .getLineNumber () >= 0 ) ? ", line " + location .getLineNumber () : "" ) + ' ' ;
296
226
}
297
227
}
298
228
}
0 commit comments