1 package org.apache.maven.plugin.ant;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.ByteArrayInputStream;
23 import java.io.File;
24 import java.io.IOException;
25 import java.text.DateFormat;
26 import java.util.ArrayList;
27 import java.util.Date;
28 import java.util.HashMap;
29 import java.util.List;
30 import java.util.Locale;
31 import java.util.Map;
32
33 import javax.xml.parsers.DocumentBuilderFactory;
34
35 import org.apache.maven.artifact.Artifact;
36 import org.apache.maven.model.Plugin;
37 import org.apache.maven.model.ReportPlugin;
38 import org.apache.maven.project.MavenProject;
39 import org.apache.xpath.XPathAPI;
40 import org.codehaus.plexus.util.PathTool;
41 import org.codehaus.plexus.util.StringUtils;
42 import org.codehaus.plexus.util.xml.XMLWriter;
43 import org.codehaus.plexus.util.xml.XmlWriterUtil;
44 import org.w3c.dom.Document;
45 import org.w3c.dom.Node;
46 import org.w3c.dom.NodeList;
47
48
49
50
51
52
53
54 public class AntBuildWriterUtil
55 {
56
57
58
59
60 public static List removeEmptyCompileSourceRoots( List compileSourceRoots )
61 {
62 List newCompileSourceRootsList = new ArrayList();
63 if ( compileSourceRoots != null )
64 {
65
66 for ( Object compileSourceRoot : compileSourceRoots )
67 {
68 String srcDir = (String) compileSourceRoot;
69 if ( new File( srcDir ).exists() )
70 {
71 newCompileSourceRootsList.add( srcDir );
72 }
73 }
74 }
75
76 return newCompileSourceRootsList;
77 }
78
79
80
81
82
83
84
85
86 public static void writeIncludesExcludes( XMLWriter writer, List includes, List excludes )
87 {
88 if ( includes != null )
89 {
90 for ( Object include1 : includes )
91 {
92 String include = (String) include1;
93 writer.startElement( "include" );
94 writer.addAttribute( "name", include );
95 writer.endElement();
96 }
97 }
98 if ( excludes != null )
99 {
100 for ( Object exclude1 : excludes )
101 {
102 String exclude = (String) exclude1;
103 writer.startElement( "exclude" );
104 writer.addAttribute( "name", exclude );
105 writer.endElement();
106 }
107 }
108 }
109
110
111
112
113
114
115 public static void writeHeader( XMLWriter writer )
116 {
117 writeAntVersionHeader( writer );
118
119 XmlWriterUtil.writeCommentLineBreak( writer );
120
121 XmlWriterUtil.writeComment( writer, StringUtils.repeat( "=", 21 ) + " - DO NOT EDIT THIS FILE! - "
122 + StringUtils.repeat( "=", 21 ) );
123
124 XmlWriterUtil.writeCommentLineBreak( writer );
125 XmlWriterUtil.writeComment( writer, " " );
126 XmlWriterUtil.writeComment( writer, "Any modifications will be overwritten." );
127 XmlWriterUtil.writeComment( writer, " " );
128 DateFormat dateFormat = DateFormat.getDateTimeInstance( DateFormat.SHORT, DateFormat.SHORT, Locale.US );
129 XmlWriterUtil.writeComment( writer,
130 "Generated by Maven Ant Plugin on "
131 + dateFormat.format( new Date( System.currentTimeMillis() ) ) );
132 XmlWriterUtil.writeComment( writer, "See: http://maven.apache.org/plugins/maven-ant-plugin/" );
133 XmlWriterUtil.writeComment( writer, " " );
134 XmlWriterUtil.writeCommentLineBreak( writer );
135
136 XmlWriterUtil.writeLineBreak( writer );
137 }
138
139
140
141
142
143
144 public static void writeAntVersionHeader( XMLWriter writer )
145 {
146 XmlWriterUtil.writeCommentText( writer, "Ant build file (http://ant.apache.org/) for Ant 1.6.2 or above.", 0 );
147 }
148
149
150
151
152
153
154
155
156
157 public static void writeAntTask( XMLWriter writer, MavenProject project, String moduleSubPath, String tasks )
158 {
159 writer.startElement( "ant" );
160 writer.addAttribute( "antfile", "build.xml" );
161 writer.addAttribute( "dir", toRelative( project.getBasedir(), moduleSubPath ) );
162 writer.addAttribute( "target", tasks );
163 writer.endElement();
164 }
165
166
167
168
169
170
171
172
173
174 public static void writeJavadocTask( XMLWriter writer, MavenProject project, ArtifactResolverWrapper wrapper )
175 throws IOException
176 {
177 List<String> sources = new ArrayList<String>();
178 for ( Object o : project.getCompileSourceRoots() )
179 {
180 String source = (String) o;
181
182 if ( new File( source ).exists() )
183 {
184 sources.add( source );
185 }
186 }
187
188
189 if ( sources.size() == 0 )
190 {
191 return;
192 }
193
194 writer.startElement( "javadoc" );
195 String sourcepath = getMavenJavadocPluginBasicOption( project, "sourcepath", null );
196 if ( sourcepath == null )
197 {
198 StringBuilder sb = new StringBuilder();
199 String[] compileSourceRoots = sources.toArray( new String[sources.size()] );
200 for ( int i = 0; i < compileSourceRoots.length; i++ )
201 {
202 sb.append( "${maven.build.srcDir." ).append( i ).append( "}" );
203
204 if ( i < ( compileSourceRoots.length - 1 ) )
205 {
206 sb.append( File.pathSeparatorChar );
207 }
208 }
209 writer.addAttribute( "sourcepath", sb.toString() );
210 addWrapAttribute( writer, "javadoc", "packagenames", "*", 3 );
211 }
212 else
213 {
214 writer.addAttribute( "sourcepath", sourcepath );
215 }
216 addWrapAttribute( writer,
217 "javadoc",
218 "destdir",
219 getMavenJavadocPluginBasicOption( project, "destdir",
220 "${maven.reporting.outputDirectory}/apidocs" ), 3 );
221 addWrapAttribute( writer, "javadoc", "extdirs", getMavenJavadocPluginBasicOption( project, "extdirs", null ),
222 3 );
223 addWrapAttribute( writer, "javadoc", "overview", getMavenJavadocPluginBasicOption( project, "overview", null ),
224 3 );
225 addWrapAttribute( writer, "javadoc", "access",
226 getMavenJavadocPluginBasicOption( project, "show", "protected" ), 3 );
227 addWrapAttribute( writer, "javadoc", "old", getMavenJavadocPluginBasicOption( project, "old", "false" ), 3 );
228 addWrapAttribute( writer, "javadoc", "verbose",
229 getMavenJavadocPluginBasicOption( project, "verbose", "false" ), 3 );
230 addWrapAttribute( writer, "javadoc", "locale", getMavenJavadocPluginBasicOption( project, "locale", null ), 3 );
231 addWrapAttribute( writer, "javadoc", "encoding", getMavenJavadocPluginBasicOption( project, "encoding", null ),
232 3 );
233 addWrapAttribute( writer, "javadoc", "version", getMavenJavadocPluginBasicOption( project, "version", "true" ),
234 3 );
235 addWrapAttribute( writer, "javadoc", "use", getMavenJavadocPluginBasicOption( project, "use", "true" ), 3 );
236 addWrapAttribute( writer, "javadoc", "author", getMavenJavadocPluginBasicOption( project, "author", "true" ),
237 3 );
238 addWrapAttribute( writer, "javadoc", "splitindex",
239 getMavenJavadocPluginBasicOption( project, "splitindex", "false" ), 3 );
240 addWrapAttribute( writer, "javadoc", "windowtitle",
241 getMavenJavadocPluginBasicOption( project, "windowtitle", null ), 3 );
242 addWrapAttribute( writer, "javadoc", "nodeprecated",
243 getMavenJavadocPluginBasicOption( project, "nodeprecated", "false" ), 3 );
244 addWrapAttribute( writer, "javadoc", "nodeprecatedlist",
245 getMavenJavadocPluginBasicOption( project, "nodeprecatedlist", "false" ), 3 );
246 addWrapAttribute( writer, "javadoc", "notree", getMavenJavadocPluginBasicOption( project, "notree", "false" ),
247 3 );
248 addWrapAttribute( writer, "javadoc", "noindex",
249 getMavenJavadocPluginBasicOption( project, "noindex", "false" ), 3 );
250 addWrapAttribute( writer, "javadoc", "nohelp", getMavenJavadocPluginBasicOption( project, "nohelp", "false" ),
251 3 );
252 addWrapAttribute( writer, "javadoc", "nonavbar",
253 getMavenJavadocPluginBasicOption( project, "nonavbar", "false" ), 3 );
254 addWrapAttribute( writer, "javadoc", "serialwarn",
255 getMavenJavadocPluginBasicOption( project, "serialwarn", "false" ), 3 );
256 addWrapAttribute( writer, "javadoc", "helpfile", getMavenJavadocPluginBasicOption( project, "helpfile", null ),
257 3 );
258 addWrapAttribute( writer, "javadoc", "stylesheetfile",
259 getMavenJavadocPluginBasicOption( project, "stylesheetfile", null ), 3 );
260 addWrapAttribute( writer, "javadoc", "charset",
261 getMavenJavadocPluginBasicOption( project, "charset", "ISO-8859-1" ), 3 );
262 addWrapAttribute( writer, "javadoc", "docencoding",
263 getMavenJavadocPluginBasicOption( project, "docencoding", null ), 3 );
264 addWrapAttribute( writer, "javadoc", "excludepackagenames",
265 getMavenJavadocPluginBasicOption( project, "excludepackagenames", null ), 3 );
266 addWrapAttribute( writer, "javadoc", "source", getMavenJavadocPluginBasicOption( project, "source", null ), 3 );
267 addWrapAttribute( writer, "javadoc", "linksource",
268 getMavenJavadocPluginBasicOption( project, "linksource", "false" ), 3 );
269 addWrapAttribute( writer, "javadoc", "breakiterator",
270 getMavenJavadocPluginBasicOption( project, "breakiterator", "false" ), 3 );
271 addWrapAttribute( writer, "javadoc", "noqualifier",
272 getMavenJavadocPluginBasicOption( project, "noqualifier", null ), 3 );
273
274 addWrapAttribute( writer, "javadoc", "maxmemory",
275 getMavenJavadocPluginBasicOption( project, "maxmemory", null ), 3 );
276 addWrapAttribute( writer, "javadoc", "additionalparam",
277 getMavenJavadocPluginBasicOption( project, "additionalparam", null ), 3 );
278
279
280 String doctitle = getMavenJavadocPluginBasicOption( project, "doctitle", null );
281 if ( doctitle != null )
282 {
283 writer.startElement( "doctitle" );
284 writer.writeText( "<![CDATA[" + doctitle + "]]>" );
285 writer.endElement();
286 }
287 String header = getMavenJavadocPluginBasicOption( project, "header", null );
288 if ( header != null )
289 {
290 writer.startElement( "header" );
291 writer.writeText( "<![CDATA[" + header + "]]>" );
292 writer.endElement();
293 }
294 String footer = getMavenJavadocPluginBasicOption( project, "footer", null );
295 if ( footer != null )
296 {
297 writer.startElement( "footer" );
298 writer.writeText( "<![CDATA[" + footer + "]]>" );
299 writer.endElement();
300 }
301 String bottom = getMavenJavadocPluginBasicOption( project, "bottom", null );
302 if ( bottom != null )
303 {
304 writer.startElement( "bottom" );
305 writer.writeText( "<![CDATA[" + bottom + "]]>" );
306 writer.endElement();
307 }
308
309 Map[] links = getMavenJavadocPluginOptions( project, "links", null );
310 if ( links != null )
311 {
312 for ( Map link : links )
313 {
314 writer.startElement( "link" );
315 writer.addAttribute( "href", (String) link.get( "link" ) );
316 writer.endElement();
317 }
318 }
319
320 Map[] offlineLinks = getMavenJavadocPluginOptions( project, "offlineLinks", null );
321 if ( offlineLinks != null )
322 {
323 for ( Map offlineLink : offlineLinks )
324 {
325 writer.startElement( "link" );
326 writer.addAttribute( "href", (String) offlineLink.get( "url" ) );
327 addWrapAttribute( writer, "javadoc", "offline", "true", 4 );
328 writer.endElement();
329 }
330 }
331
332 Map[] groups = getMavenJavadocPluginOptions( project, "groups", null );
333 if ( groups != null )
334 {
335 for ( Map group1 : groups )
336 {
337 Map group = (Map) group1.get( "group" );
338 writer.startElement( "group" );
339 writer.addAttribute( "title", (String) group.get( "title" ) );
340 addWrapAttribute( writer, "javadoc", "package", (String) group.get( "package" ), 4 );
341 writer.endElement();
342 }
343 }
344
345
346 String doclet = getMavenJavadocPluginBasicOption( project, "doclet", null );
347 if ( doclet != null )
348 {
349 String docletpath = getMavenJavadocPluginBasicOption( project, "docletpath", null );
350 if ( StringUtils.isNotEmpty( docletpath ) )
351 {
352 writer.startElement( "doclet" );
353 writer.addAttribute( "name", doclet );
354 addWrapAttribute( writer, "javadoc", "path", docletpath, 4 );
355 writer.endElement();
356 }
357 else
358 {
359 Map docletArtifact = getMavenJavadocPluginOption( project, "docletArtifact", null );
360 String path = wrapper.getArtifactAbsolutePath( (String) docletArtifact.get( "groupId" ),
361 (String) docletArtifact.get( "artifactId" ),
362 (String) docletArtifact.get( "version" ) );
363 path = StringUtils.replace( path, wrapper.getLocalRepository().getBasedir(), "${maven.repo.local}" );
364
365 writer.startElement( "doclet" );
366 writer.addAttribute( "name", doclet );
367 addWrapAttribute( writer, "javadoc", "path", path, 4 );
368 writer.endElement();
369 }
370 }
371
372
373 String taglet = getMavenJavadocPluginBasicOption( project, "taglet", null );
374 if ( taglet != null )
375 {
376 String tagletpath = getMavenJavadocPluginBasicOption( project, "tagletpath", null );
377 if ( StringUtils.isNotEmpty( tagletpath ) )
378 {
379 writer.startElement( "taglet" );
380 writer.addAttribute( "name", taglet );
381 addWrapAttribute( writer, "javadoc", "path", tagletpath, 4 );
382 writer.endElement();
383 }
384 else
385 {
386 Map tagletArtifact = getMavenJavadocPluginOption( project, "tagletArtifact", null );
387 String path = wrapper.getArtifactAbsolutePath( (String) tagletArtifact.get( "groupId" ),
388 (String) tagletArtifact.get( "artifactId" ),
389 (String) tagletArtifact.get( "version" ) );
390 path = StringUtils.replace( path, wrapper.getLocalRepository().getBasedir(), "${maven.repo.local}" );
391
392 writer.startElement( "taglet" );
393 writer.addAttribute( "name", taglet );
394 addWrapAttribute( writer, "javadoc", "path", path, 4 );
395 writer.endElement();
396 }
397 }
398
399 Map[] tags = getMavenJavadocPluginOptions( project, "tags", null );
400 if ( tags != null )
401 {
402 for ( Map tag : tags )
403 {
404 Map props = (Map) tag.get( "tag" );
405 writer.startElement( "tag" );
406 writer.addAttribute( "name", (String) props.get( "name" ) );
407 addWrapAttribute( writer, "javadoc", "scope", (String) props.get( "placement" ), 4 );
408 addWrapAttribute( writer, "javadoc", "description", (String) props.get( "head" ), 4 );
409 writer.endElement();
410 }
411 }
412
413 writer.endElement();
414 }
415
416
417
418
419
420
421
422
423 public static void writeJarTask( XMLWriter writer, MavenProject project )
424 throws IOException
425 {
426 writer.startElement( "jar" );
427 writer.addAttribute( "jarfile", "${maven.build.dir}/${maven.build.finalName}.jar" );
428 addWrapAttribute( writer, "jar", "compress",
429 getMavenJarPluginBasicOption( project, "archive//compress", "true" ), 3 );
430 addWrapAttribute( writer, "jar", "index", getMavenJarPluginBasicOption( project, "archive//index", "false" ),
431 3 );
432 if ( getMavenJarPluginBasicOption( project, "archive//manifestFile", null ) != null )
433 {
434 addWrapAttribute( writer, "jar", "manifest",
435 getMavenJarPluginBasicOption( project, "archive//manifestFile", null ), 3 );
436 }
437 addWrapAttribute( writer, "jar", "basedir", "${maven.build.outputDir}", 3 );
438 addWrapAttribute( writer, "jar", "excludes", "**/package.html", 3 );
439 if ( getMavenPluginOption( project, "maven-jar-plugin", "archive//manifest", null ) != null )
440 {
441 writer.startElement( "manifest" );
442 writer.startElement( "attribute" );
443 writer.addAttribute( "name", "Main-Class" );
444 addWrapAttribute( writer, "attribute", "value",
445 getMavenJarPluginBasicOption( project, "archive//manifest//mainClass", null ), 5 );
446 writer.endElement();
447 writer.endElement();
448 }
449 writer.endElement();
450 }
451
452
453
454
455
456
457
458
459
460 public static void writeEarTask( XMLWriter writer, MavenProject project,
461 ArtifactResolverWrapper artifactResolverWrapper )
462 throws IOException
463 {
464 writeCopyLib( writer, project, artifactResolverWrapper, "${maven.build.dir}/${maven.build.finalName}" );
465
466 writer.startElement( "ear" );
467 writer.addAttribute( "destfile", "${maven.build.dir}/${maven.build.finalName}.ear" );
468 addWrapAttribute( writer, "ear", "basedir", "${maven.build.dir}/${maven.build.finalName}", 3 );
469 addWrapAttribute( writer, "ear", "compress",
470 getMavenEarPluginBasicOption( project, "archive//compress", "true" ), 3 );
471 addWrapAttribute( writer, "ear", "includes ", getMavenEarPluginBasicOption( project, "includes", null ), 3 );
472 addWrapAttribute( writer, "ear", "excludes", getMavenEarPluginBasicOption( project, "excludes", null ), 3 );
473 if ( getMavenEarPluginBasicOption( project, "applicationXml", null ) != null )
474 {
475 addWrapAttribute( writer, "ear", "appxml", getMavenEarPluginBasicOption( project, "applicationXml", null ),
476 3 );
477 }
478 else
479 {
480
481 addWrapAttribute( writer, "ear", "appxml", "${maven.build.dir}/application.xml", 3 );
482 }
483 if ( getMavenEarPluginBasicOption( project, "manifestFile", null ) != null )
484 {
485 addWrapAttribute( writer, "ear", "manifest", getMavenEarPluginBasicOption( project, "manifestFile", null ),
486 3 );
487 }
488 writer.endElement();
489 }
490
491
492
493
494
495
496
497
498
499 public static void writeWarTask( XMLWriter writer, MavenProject project,
500 ArtifactResolverWrapper artifactResolverWrapper )
501 throws IOException
502 {
503 String webXml = getMavenWarPluginBasicOption( project, "webXml", "${basedir}/src/main/webapp/WEB-INF/web.xml" );
504 if ( webXml.startsWith( "${basedir}/" ) )
505 {
506 webXml = webXml.substring( "${basedir}/".length() );
507 }
508
509 writeCopyLib( writer, project, artifactResolverWrapper,
510 "${maven.build.dir}/${maven.build.finalName}/WEB-INF/lib" );
511
512 writer.startElement( "war" );
513 writer.addAttribute( "destfile", "${maven.build.dir}/${maven.build.finalName}.war" );
514 addWrapAttribute( writer, "war", "compress",
515 getMavenWarPluginBasicOption( project, "archive//compress", "true" ), 3 );
516 addWrapAttribute( writer, "war", "webxml", webXml, 3 );
517 if ( getMavenWarPluginBasicOption( project, "manifestFile", null ) != null )
518 {
519 addWrapAttribute( writer, "war", "manifest", getMavenWarPluginBasicOption( project, "manifestFile", null ),
520 3 );
521 }
522 writer.startElement( "lib" );
523 writer.addAttribute( "dir", "${maven.build.dir}/${maven.build.finalName}/WEB-INF/lib" );
524 writer.endElement();
525 writer.startElement( "classes" );
526 writer.addAttribute( "dir", "${maven.build.outputDir}" );
527 writer.endElement();
528 writer.startElement( "fileset" );
529 writer.addAttribute( "dir", "src/main/webapp" );
530 addWrapAttribute( writer, "fileset", "excludes", "WEB-INF/web.xml", 4 );
531 writer.endElement();
532 writer.endElement();
533 }
534
535
536
537
538
539
540
541
542
543
544 public static void addWrapAttribute( XMLWriter writer, String tag, String name, String value, int indent )
545 {
546 if ( StringUtils.isEmpty( value ) )
547 {
548 return;
549 }
550
551 if ( indent < 0 )
552 {
553 writer.addAttribute( name, value );
554 }
555 else
556 {
557 writer.addAttribute( "\n" + StringUtils.repeat( " ", ( StringUtils.isEmpty( tag ) ? 0 : tag.length() )
558 + indent * XmlWriterUtil.DEFAULT_INDENTATION_SIZE ) + name, value );
559 }
560 }
561
562
563
564
565
566 public static boolean isPomPackaging( MavenProject mavenProject )
567 {
568 return "pom".equals( mavenProject.getPackaging() );
569 }
570
571
572
573
574
575
576
577 public static boolean isJarPackaging( MavenProject mavenProject )
578 {
579 return "jar".equals( mavenProject.getPackaging() ) || isEjbPackaging( mavenProject ) || isMavenPluginPackaging(
580 mavenProject ) || isBundlePackaging( mavenProject );
581 }
582
583
584
585
586
587 public static boolean isBundlePackaging( MavenProject mavenProject )
588 {
589 return "bundle".equals( mavenProject.getPackaging() );
590 }
591
592
593
594
595
596 public static boolean isEjbPackaging( MavenProject mavenProject )
597 {
598 return "ejb".equals( mavenProject.getPackaging() );
599 }
600
601
602
603
604
605 public static boolean isMavenPluginPackaging( MavenProject mavenProject )
606 {
607 return "maven-plugin".equals( mavenProject.getPackaging() );
608 }
609
610
611
612
613
614 public static boolean isEarPackaging( MavenProject mavenProject )
615 {
616 return "ear".equals( mavenProject.getPackaging() );
617 }
618
619
620
621
622
623 public static boolean isWarPackaging( MavenProject mavenProject )
624 {
625 return "war".equals( mavenProject.getPackaging() );
626 }
627
628
629
630
631
632
633
634
635
636
637 public static String getMavenCompilerPluginBasicOption( MavenProject project, String optionName,
638 String defaultValue )
639 throws IOException
640 {
641 return getMavenPluginBasicOption( project, "maven-compiler-plugin", optionName, defaultValue );
642 }
643
644
645
646
647
648
649
650
651
652
653 public static Map getMavenCompilerPluginOption( MavenProject project, String optionName, String defaultValue )
654 throws IOException
655 {
656 return getMavenPluginOption( project, "maven-compiler-plugin", optionName, defaultValue );
657 }
658
659
660
661
662
663
664
665
666
667
668
669 public static Map[] getMavenCompilerPluginOptions( MavenProject project, String optionName, String defaultValue )
670 throws IOException
671 {
672 return getMavenPluginOptions( project, "maven-compiler-plugin", optionName, defaultValue );
673 }
674
675
676
677
678
679
680
681
682
683
684 public static String getMavenSurefirePluginBasicOption( MavenProject project, String optionName,
685 String defaultValue )
686 throws IOException
687 {
688 return getMavenPluginBasicOption( project, "maven-surefire-plugin", optionName, defaultValue );
689 }
690
691
692
693
694
695
696
697
698
699
700 public static Map getMavenSurefirePluginOption( MavenProject project, String optionName, String defaultValue )
701 throws IOException
702 {
703 return getMavenPluginOption( project, "maven-surefire-plugin", optionName, defaultValue );
704 }
705
706
707
708
709
710
711
712
713
714
715
716 public static Map[] getMavenSurefirePluginOptions( MavenProject project, String optionName, String defaultValue )
717 throws IOException
718 {
719 return getMavenPluginOptions( project, "maven-surefire-plugin", optionName, defaultValue );
720 }
721
722
723
724
725
726
727
728
729
730
731 public static String getMavenJavadocPluginBasicOption( MavenProject project, String optionName,
732 String defaultValue )
733 throws IOException
734 {
735 return getMavenPluginBasicOption( project, "maven-javadoc-plugin", optionName, defaultValue );
736 }
737
738
739
740
741
742
743
744
745
746
747 public static Map getMavenJavadocPluginOption( MavenProject project, String optionName, String defaultValue )
748 throws IOException
749 {
750 return getMavenPluginOption( project, "maven-javadoc-plugin", optionName, defaultValue );
751 }
752
753
754
755
756
757
758
759
760
761
762
763 public static Map[] getMavenJavadocPluginOptions( MavenProject project, String optionName, String defaultValue )
764 throws IOException
765 {
766 return getMavenPluginOptions( project, "maven-javadoc-plugin", optionName, defaultValue );
767 }
768
769
770
771
772
773
774
775
776
777
778 public static String getMavenJarPluginBasicOption( MavenProject project, String optionName, String defaultValue )
779 throws IOException
780 {
781 return getMavenPluginBasicOption( project, "maven-jar-plugin", optionName, defaultValue );
782 }
783
784
785
786
787
788
789
790
791
792
793 public static String getMavenEarPluginBasicOption( MavenProject project, String optionName, String defaultValue )
794 throws IOException
795 {
796 return getMavenPluginBasicOption( project, "maven-ear-plugin", optionName, defaultValue );
797 }
798
799
800
801
802
803
804
805
806
807
808 public static String getMavenWarPluginBasicOption( MavenProject project, String optionName, String defaultValue )
809 throws IOException
810 {
811 return getMavenPluginBasicOption( project, "maven-war-plugin", optionName, defaultValue );
812 }
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841 private static String getMavenPluginBasicOption( MavenProject project, String pluginArtifactId, String optionName,
842 String defaultValue )
843 throws IOException
844 {
845 return (String) getMavenPluginConfigurationsImpl( project, pluginArtifactId, optionName, defaultValue ).get(
846 optionName );
847 }
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877 private static Map getMavenPluginOption( MavenProject project, String pluginArtifactId, String optionName,
878 String defaultValue )
879 throws IOException
880 {
881 return (Map) getMavenPluginConfigurationsImpl( project, pluginArtifactId, optionName, defaultValue ).get(
882 optionName );
883 }
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919 private static Map[] getMavenPluginOptions( MavenProject project, String pluginArtifactId, String optionName,
920 String defaultValue )
921 throws IOException
922 {
923 return (Map[]) getMavenPluginConfigurationsImpl( project, pluginArtifactId, optionName, defaultValue ).get(
924 optionName );
925 }
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975 private static Map getMavenPluginConfigurationsImpl( MavenProject project, String pluginArtifactId,
976 String optionName, String defaultValue )
977 throws IOException
978 {
979 List plugins = new ArrayList();
980 for ( ReportPlugin reportPlugin1 : project.getModel().getReporting().getPlugins() )
981 {
982 plugins.add( reportPlugin1 );
983 }
984 for ( Plugin plugin1 : project.getModel().getBuild().getPlugins() )
985 {
986 plugins.add( plugin1 );
987 }
988 if ( project.getBuild().getPluginManagement() != null )
989 {
990 for ( Plugin plugin : project.getBuild().getPluginManagement().getPlugins() )
991 {
992 plugins.add( plugin );
993 }
994 }
995
996 for ( Object next : plugins )
997 {
998 Object pluginConf = null;
999
1000 if ( next instanceof Plugin )
1001 {
1002 Plugin plugin = (Plugin) next;
1003
1004
1005 if ( !( ( plugin.getGroupId().equals( "org.apache.maven.plugins" ) ) && ( plugin.getArtifactId().equals(
1006 pluginArtifactId ) ) ) )
1007 {
1008 continue;
1009 }
1010
1011 pluginConf = plugin.getConfiguration();
1012 }
1013
1014 if ( next instanceof ReportPlugin )
1015 {
1016 ReportPlugin reportPlugin = (ReportPlugin) next;
1017
1018
1019 if ( !( ( reportPlugin.getGroupId().equals( "org.apache.maven.plugins" ) )
1020 && ( reportPlugin.getArtifactId().equals( pluginArtifactId ) ) ) )
1021 {
1022 continue;
1023 }
1024
1025 pluginConf = reportPlugin.getConfiguration();
1026 }
1027
1028 if ( pluginConf == null )
1029 {
1030 continue;
1031 }
1032
1033 try
1034 {
1035 Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(
1036 new ByteArrayInputStream( pluginConf.toString().getBytes( "UTF-8" ) ) );
1037
1038 NodeList nodeList = XPathAPI.eval( doc, "//configuration/" + optionName ).nodelist();
1039 if ( nodeList.getLength() > 0 )
1040 {
1041 Node optionNode = nodeList.item( 0 );
1042
1043 if ( isList( optionNode ) )
1044 {
1045
1046
1047
1048
1049
1050
1051
1052
1053 Map options = new HashMap();
1054
1055 List optionNames = new ArrayList();
1056 NodeList childs = optionNode.getChildNodes();
1057 for ( int i = 0; i < childs.getLength(); i++ )
1058 {
1059 Node child = childs.item( i );
1060 if ( child.getNodeType() == Node.ELEMENT_NODE )
1061 {
1062 Map<String, Object> option = new HashMap<String, Object>();
1063
1064 if ( isElementContent( child ) )
1065 {
1066 Map<String, String> properties = new HashMap<String, String>();
1067 NodeList childs2 = child.getChildNodes();
1068 if ( childs2.getLength() > 0 )
1069 {
1070 for ( int j = 0; j < childs2.getLength(); j++ )
1071 {
1072 Node child2 = childs2.item( j );
1073 if ( child2.getNodeType() == Node.ELEMENT_NODE )
1074 {
1075 properties.put( child2.getNodeName(), getTextContent( child2 ) );
1076 }
1077 }
1078 option.put( child.getNodeName(), properties );
1079 }
1080 }
1081 else
1082 {
1083 option.put( child.getNodeName(), getTextContent( child ) );
1084 }
1085
1086 optionNames.add( option );
1087 }
1088 }
1089
1090 options.put( optionName, optionNames.toArray( new Map[optionNames.size()] ) );
1091
1092 return options;
1093 }
1094
1095 if ( isElementContent( optionNode ) )
1096 {
1097
1098
1099
1100
1101
1102
1103 Map option = new HashMap();
1104
1105 NodeList childs = optionNode.getChildNodes();
1106 if ( childs.getLength() > 1 )
1107 {
1108 Map parameters = new HashMap();
1109
1110 for ( int i = 0; i < childs.getLength(); i++ )
1111 {
1112 Node child = childs.item( i );
1113 if ( child.getNodeType() == Node.ELEMENT_NODE )
1114 {
1115 parameters.put( child.getNodeName(), getTextContent( child ) );
1116 }
1117 }
1118
1119 option.put( optionName, parameters );
1120 }
1121
1122 return option;
1123 }
1124 else
1125 {
1126
1127
1128
1129 Map option = new HashMap();
1130
1131 option.put( optionName, getTextContent( optionNode ) );
1132
1133 return option;
1134 }
1135 }
1136 }
1137 catch ( Exception e )
1138 {
1139 throw new IOException( "Exception occured: " + e.getMessage() );
1140 }
1141 }
1142
1143 Map properties = new HashMap();
1144 properties.put( optionName, defaultValue );
1145
1146 return properties;
1147 }
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158 private static void writeCopyLib( XMLWriter writer, MavenProject project,
1159 ArtifactResolverWrapper artifactResolverWrapper, String outputDir )
1160 {
1161 writer.startElement( "mkdir" );
1162 writer.addAttribute( "dir", outputDir );
1163 writer.endElement();
1164
1165 if ( project.getArtifacts() != null )
1166 {
1167 for ( Object o : project.getArtifacts() )
1168 {
1169 Artifact artifact = (Artifact) o;
1170
1171 if ( Artifact.SCOPE_COMPILE.equals( artifact.getScope() ) || Artifact.SCOPE_RUNTIME.equals(
1172 artifact.getScope() ) )
1173 {
1174 String path = artifactResolverWrapper.getLocalArtifactPath( artifact );
1175 if ( !new File( path ).isAbsolute() )
1176 {
1177 path = "${maven.repo.local}/" + path;
1178 }
1179
1180 writer.startElement( "copy" );
1181 writer.addAttribute( "file", path );
1182 addWrapAttribute( writer, "copy", "todir", outputDir, 3 );
1183 writer.endElement();
1184 }
1185 }
1186 }
1187 }
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209 private static boolean isList( Node node )
1210 {
1211 if ( node == null )
1212 {
1213 return false;
1214 }
1215
1216 NodeList children = node.getChildNodes();
1217
1218 boolean isList = false;
1219 String lastNodeName = null;
1220 for ( int i = 0; i < children.getLength(); i++ )
1221 {
1222 Node child = children.item( i );
1223 if ( child.getNodeType() == Node.ELEMENT_NODE )
1224 {
1225 isList = isList || ( child.getNodeName().equals( lastNodeName ) );
1226 lastNodeName = child.getNodeName();
1227 }
1228 }
1229 if ( StringUtils.isNotEmpty( lastNodeName ) )
1230 {
1231 isList = isList || lastNodeName.equals( getSingularForm( node.getNodeName() ) );
1232 }
1233
1234 return isList;
1235 }
1236
1237
1238
1239
1240
1241
1242
1243 private static boolean isElementContent( Node node )
1244 {
1245 if ( node == null )
1246 {
1247 return false;
1248 }
1249 NodeList children = node.getChildNodes();
1250 for ( int i = 0; i < children.getLength(); i++ )
1251 {
1252 Node child = children.item( i );
1253 if ( child.getNodeType() == Node.ELEMENT_NODE )
1254 {
1255 return true;
1256 }
1257 }
1258 return false;
1259 }
1260
1261
1262
1263
1264
1265
1266
1267 private static String getTextContent( Node node )
1268 {
1269 StringBuilder buffer = new StringBuilder();
1270 if ( node != null )
1271 {
1272 NodeList children = node.getChildNodes();
1273 for ( int i = 0; i < children.getLength(); i++ )
1274 {
1275 Node child = children.item( i );
1276 if ( child.getNodeType() == Node.TEXT_NODE || child.getNodeType() == Node.CDATA_SECTION_NODE )
1277 {
1278 buffer.append( child.getNodeValue() );
1279 }
1280 }
1281 }
1282 return buffer.toString();
1283 }
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298 static String getSingularForm( String pluralForm )
1299 {
1300 String singularForm = "";
1301 if ( StringUtils.isNotEmpty( pluralForm ) )
1302 {
1303 if ( pluralForm.endsWith( "ies" ) )
1304 {
1305 singularForm = pluralForm.substring( 0, pluralForm.length() - 3 ) + 'y';
1306 }
1307 else if ( pluralForm.endsWith( "ches" ) )
1308 {
1309 singularForm = pluralForm.substring( 0, pluralForm.length() - 2 );
1310 }
1311 else if ( pluralForm.endsWith( "s" ) && pluralForm.length() > 1 )
1312 {
1313 singularForm = pluralForm.substring( 0, pluralForm.length() - 1 );
1314 }
1315 }
1316 return singularForm;
1317 }
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362 static String toRelative( File basedir, String path )
1363 {
1364 String result = null;
1365 if ( new File( path ).isAbsolute() )
1366 {
1367 String pathNormalized = path.replace( '/', File.separatorChar ).replace( '\\', File.separatorChar );
1368 result = PathTool.getRelativeFilePath( basedir.getAbsolutePath(), pathNormalized );
1369 }
1370 if ( result == null )
1371 {
1372 result = path;
1373 }
1374 result = result.replace( '\\', '/' );
1375 if ( result.length() <= 0 || "/".equals( result ) )
1376 {
1377 result = '.' + result;
1378 }
1379 return result;
1380 }
1381
1382 }