1 | |
package org.apache.maven.plugin.checkstyle; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import com.puppycrawl.tools.checkstyle.Checker; |
23 | |
import com.puppycrawl.tools.checkstyle.ConfigurationLoader; |
24 | |
import com.puppycrawl.tools.checkstyle.DefaultConfiguration; |
25 | |
import com.puppycrawl.tools.checkstyle.DefaultLogger; |
26 | |
import com.puppycrawl.tools.checkstyle.ModuleFactory; |
27 | |
import com.puppycrawl.tools.checkstyle.PackageNamesLoader; |
28 | |
import com.puppycrawl.tools.checkstyle.PropertiesExpander; |
29 | |
import com.puppycrawl.tools.checkstyle.XMLLogger; |
30 | |
import com.puppycrawl.tools.checkstyle.api.AuditListener; |
31 | |
import com.puppycrawl.tools.checkstyle.api.CheckstyleException; |
32 | |
import com.puppycrawl.tools.checkstyle.api.Configuration; |
33 | |
import com.puppycrawl.tools.checkstyle.api.FilterSet; |
34 | |
import com.puppycrawl.tools.checkstyle.api.SeverityLevel; |
35 | |
import com.puppycrawl.tools.checkstyle.filters.SuppressionsLoader; |
36 | |
import org.apache.maven.artifact.DependencyResolutionRequiredException; |
37 | |
import org.apache.maven.doxia.tools.SiteTool; |
38 | |
import org.apache.maven.model.ReportPlugin; |
39 | |
import org.apache.maven.plugin.MojoExecutionException; |
40 | |
import org.apache.maven.project.MavenProject; |
41 | |
import org.apache.maven.reporting.AbstractMavenReport; |
42 | |
import org.apache.maven.reporting.MavenReportException; |
43 | |
import org.apache.velocity.VelocityContext; |
44 | |
import org.apache.velocity.context.Context; |
45 | |
import org.apache.velocity.exception.ResourceNotFoundException; |
46 | |
import org.apache.velocity.exception.VelocityException; |
47 | |
import org.codehaus.doxia.site.renderer.SiteRenderer; |
48 | |
import org.codehaus.plexus.resource.ResourceManager; |
49 | |
import org.codehaus.plexus.resource.loader.FileResourceCreationException; |
50 | |
import org.codehaus.plexus.resource.loader.FileResourceLoader; |
51 | |
import org.codehaus.plexus.util.FileUtils; |
52 | |
import org.codehaus.plexus.util.PathTool; |
53 | |
import org.codehaus.plexus.util.StringInputStream; |
54 | |
import org.codehaus.plexus.util.StringOutputStream; |
55 | |
import org.codehaus.plexus.util.StringUtils; |
56 | |
import org.codehaus.plexus.velocity.VelocityComponent; |
57 | |
|
58 | |
import java.io.File; |
59 | |
import java.io.FileInputStream; |
60 | |
import java.io.FileNotFoundException; |
61 | |
import java.io.FileOutputStream; |
62 | |
import java.io.IOException; |
63 | |
import java.io.OutputStream; |
64 | |
import java.net.MalformedURLException; |
65 | |
import java.net.URL; |
66 | |
import java.net.URLClassLoader; |
67 | |
import java.util.ArrayList; |
68 | |
import java.util.Calendar; |
69 | |
import java.util.Collections; |
70 | |
import java.util.HashMap; |
71 | |
import java.util.Iterator; |
72 | |
import java.util.List; |
73 | |
import java.util.Locale; |
74 | |
import java.util.Map; |
75 | |
import java.util.Properties; |
76 | |
import java.util.ResourceBundle; |
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | 20 | public class CheckstyleReport |
88 | |
extends AbstractMavenReport |
89 | |
{ |
90 | |
private static final String PLUGIN_RESOURCES = "org/apache/maven/plugin/checkstyle"; |
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
private static final Map FORMAT_TO_CONFIG_LOCATION; |
96 | |
|
97 | |
static |
98 | |
{ |
99 | 2 | Map fmt2Cfg = new HashMap(); |
100 | |
|
101 | 2 | fmt2Cfg.put( "sun", "config/sun_checks.xml" ); |
102 | 2 | fmt2Cfg.put( "turbine", "config/turbine_checks.xml" ); |
103 | 2 | fmt2Cfg.put( "avalon", "config/avalon_checks.xml" ); |
104 | 2 | fmt2Cfg.put( "maven", "config/maven_checks.xml" ); |
105 | |
|
106 | 2 | FORMAT_TO_CONFIG_LOCATION = Collections.unmodifiableMap( fmt2Cfg ); |
107 | |
} |
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
private boolean skip; |
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
private File outputDirectory; |
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
private boolean enableRulesSummary; |
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
private boolean enableSeveritySummary; |
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
private boolean enableFilesSummary; |
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | |
private boolean enableRSS; |
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
private String includes; |
166 | |
|
167 | |
|
168 | |
|
169 | |
|
170 | |
|
171 | |
|
172 | |
|
173 | |
private String excludes; |
174 | |
|
175 | |
|
176 | |
|
177 | |
|
178 | |
|
179 | |
|
180 | |
|
181 | |
|
182 | |
|
183 | |
|
184 | |
|
185 | |
|
186 | |
|
187 | |
|
188 | |
|
189 | |
|
190 | |
|
191 | |
|
192 | |
|
193 | |
|
194 | |
|
195 | |
|
196 | |
|
197 | |
|
198 | |
|
199 | |
|
200 | |
|
201 | |
|
202 | |
|
203 | |
|
204 | |
|
205 | |
|
206 | |
|
207 | |
|
208 | |
|
209 | |
private String configLocation; |
210 | |
|
211 | |
|
212 | |
|
213 | |
|
214 | |
|
215 | |
|
216 | |
|
217 | |
|
218 | |
private String format; |
219 | |
|
220 | |
|
221 | |
|
222 | |
|
223 | |
|
224 | |
|
225 | |
|
226 | |
|
227 | |
|
228 | |
|
229 | |
|
230 | |
|
231 | |
|
232 | |
|
233 | |
|
234 | |
|
235 | |
|
236 | |
|
237 | |
|
238 | |
|
239 | |
|
240 | |
|
241 | |
|
242 | |
private String propertiesLocation; |
243 | |
|
244 | |
|
245 | |
|
246 | |
|
247 | |
|
248 | |
|
249 | |
|
250 | |
|
251 | |
private File propertiesFile; |
252 | |
|
253 | |
|
254 | |
|
255 | |
|
256 | |
|
257 | |
|
258 | |
|
259 | |
|
260 | |
private URL propertiesURL; |
261 | |
|
262 | |
|
263 | |
|
264 | |
|
265 | |
|
266 | |
|
267 | |
private String propertyExpansion; |
268 | |
|
269 | |
|
270 | |
|
271 | |
|
272 | |
|
273 | |
|
274 | |
|
275 | |
|
276 | |
|
277 | |
|
278 | |
|
279 | |
|
280 | |
|
281 | |
|
282 | |
|
283 | |
|
284 | |
|
285 | |
|
286 | |
|
287 | |
|
288 | |
|
289 | |
|
290 | |
|
291 | |
|
292 | |
|
293 | |
|
294 | |
private String headerLocation; |
295 | |
|
296 | |
|
297 | |
|
298 | |
|
299 | |
|
300 | |
|
301 | |
|
302 | |
|
303 | |
|
304 | |
private File headerFile; |
305 | |
|
306 | |
|
307 | |
|
308 | |
|
309 | |
|
310 | |
|
311 | |
private String cacheFile; |
312 | |
|
313 | |
|
314 | |
|
315 | |
|
316 | |
|
317 | |
|
318 | |
|
319 | |
private File useFile; |
320 | |
|
321 | |
|
322 | |
|
323 | |
|
324 | |
|
325 | |
|
326 | |
|
327 | |
|
328 | |
|
329 | |
protected SiteTool siteTool; |
330 | |
|
331 | |
|
332 | |
|
333 | |
|
334 | |
|
335 | |
|
336 | |
|
337 | |
|
338 | |
|
339 | |
|
340 | |
|
341 | |
|
342 | |
|
343 | |
|
344 | |
|
345 | |
|
346 | |
|
347 | |
|
348 | |
|
349 | |
|
350 | |
|
351 | |
private String suppressionsLocation; |
352 | |
|
353 | |
|
354 | |
|
355 | |
|
356 | |
|
357 | |
|
358 | |
|
359 | |
|
360 | |
private String suppressionsFileExpression; |
361 | |
|
362 | |
|
363 | |
|
364 | |
|
365 | |
|
366 | |
|
367 | |
|
368 | |
|
369 | |
|
370 | |
|
371 | |
|
372 | |
private String suppressionsFile; |
373 | |
|
374 | |
|
375 | |
|
376 | |
|
377 | |
|
378 | |
|
379 | |
|
380 | |
|
381 | |
|
382 | |
private File outputFile; |
383 | |
|
384 | |
|
385 | |
|
386 | |
|
387 | |
|
388 | |
|
389 | |
|
390 | |
private String outputFileFormat; |
391 | |
|
392 | |
|
393 | |
|
394 | |
|
395 | |
|
396 | |
|
397 | |
|
398 | |
|
399 | |
|
400 | |
|
401 | |
|
402 | |
|
403 | |
|
404 | |
|
405 | |
|
406 | |
|
407 | |
|
408 | |
|
409 | |
private String packageNamesLocation; |
410 | |
|
411 | |
|
412 | |
|
413 | |
|
414 | |
|
415 | |
|
416 | |
|
417 | |
|
418 | |
private String packageNamesFile; |
419 | |
|
420 | |
|
421 | |
|
422 | |
|
423 | |
|
424 | |
|
425 | |
private boolean failsOnError; |
426 | |
|
427 | |
|
428 | |
|
429 | |
|
430 | |
|
431 | |
|
432 | |
|
433 | |
private File sourceDirectory; |
434 | |
|
435 | |
|
436 | |
|
437 | |
|
438 | |
|
439 | |
|
440 | |
|
441 | |
|
442 | |
private File testSourceDirectory; |
443 | |
|
444 | |
|
445 | |
|
446 | |
|
447 | |
|
448 | |
|
449 | |
|
450 | |
private boolean includeTestSourceDirectory; |
451 | |
|
452 | |
|
453 | |
|
454 | |
|
455 | |
|
456 | |
|
457 | |
|
458 | |
|
459 | |
private MavenProject project; |
460 | |
|
461 | |
|
462 | |
|
463 | |
|
464 | |
|
465 | |
|
466 | |
private boolean consoleOutput; |
467 | |
|
468 | |
|
469 | |
|
470 | |
|
471 | |
|
472 | |
|
473 | |
|
474 | |
|
475 | |
private boolean linkXRef; |
476 | |
|
477 | |
|
478 | |
|
479 | |
|
480 | |
|
481 | |
|
482 | |
private File xrefLocation; |
483 | |
|
484 | |
|
485 | |
|
486 | |
|
487 | |
|
488 | |
|
489 | |
|
490 | |
|
491 | |
|
492 | |
private String encoding; |
493 | |
|
494 | |
|
495 | |
|
496 | |
|
497 | |
|
498 | |
|
499 | |
private SiteRenderer siteRenderer; |
500 | |
|
501 | |
|
502 | |
|
503 | |
|
504 | |
|
505 | |
|
506 | |
|
507 | |
private VelocityComponent velocityComponent; |
508 | |
|
509 | 2 | private static final File[] EMPTY_FILE_ARRAY = new File[0]; |
510 | |
|
511 | |
private StringOutputStream stringOutputStream; |
512 | |
|
513 | |
|
514 | |
|
515 | |
|
516 | |
|
517 | |
|
518 | |
private ResourceManager locator; |
519 | |
|
520 | |
|
521 | |
|
522 | |
|
523 | |
public String getName( Locale locale ) |
524 | |
{ |
525 | 24 | return getBundle( locale ).getString( "report.checkstyle.name" ); |
526 | |
} |
527 | |
|
528 | |
|
529 | |
|
530 | |
|
531 | |
public String getDescription( Locale locale ) |
532 | |
{ |
533 | 0 | return getBundle( locale ).getString( "report.checkstyle.description" ); |
534 | |
} |
535 | |
|
536 | |
|
537 | |
|
538 | |
|
539 | |
protected String getOutputDirectory() |
540 | |
{ |
541 | 48 | return outputDirectory.getAbsolutePath(); |
542 | |
} |
543 | |
|
544 | |
|
545 | |
|
546 | |
|
547 | |
protected MavenProject getProject() |
548 | |
{ |
549 | 52 | return project; |
550 | |
} |
551 | |
|
552 | |
|
553 | |
|
554 | |
|
555 | |
protected SiteRenderer getSiteRenderer() |
556 | |
{ |
557 | 36 | return siteRenderer; |
558 | |
} |
559 | |
|
560 | |
|
561 | |
|
562 | |
|
563 | |
public void executeReport( Locale locale ) |
564 | |
throws MavenReportException |
565 | |
{ |
566 | 20 | if ( !skip ) |
567 | |
{ |
568 | 20 | mergeDeprecatedInfo(); |
569 | |
|
570 | 20 | locator.addSearchPath( FileResourceLoader.ID, project.getFile().getParentFile().getAbsolutePath() ); |
571 | 20 | locator.addSearchPath( "url", "" ); |
572 | |
|
573 | 20 | locator.setOutputDirectory( new File( project.getBuild().getDirectory() ) ); |
574 | |
|
575 | 20 | if ( !canGenerateReport() ) |
576 | |
{ |
577 | 2 | getLog().info( "Source directory does not exist - skipping report." ); |
578 | 2 | return; |
579 | |
} |
580 | |
|
581 | |
|
582 | |
|
583 | |
|
584 | |
|
585 | |
|
586 | |
|
587 | |
|
588 | 18 | ClassLoader currentClassLoader = Thread.currentThread().getContextClassLoader(); |
589 | |
|
590 | |
try |
591 | |
{ |
592 | |
|
593 | |
|
594 | |
|
595 | 22 | ClassLoader checkstyleClassLoader = PackageNamesLoader.class.getClassLoader(); |
596 | 18 | Thread.currentThread().setContextClassLoader( checkstyleClassLoader ); |
597 | |
|
598 | |
|
599 | 18 | String configFile = getConfigFile(); |
600 | 18 | Properties overridingProperties = getOverridingProperties(); |
601 | |
ModuleFactory moduleFactory; |
602 | |
Configuration config; |
603 | |
CheckstyleResults results; |
604 | |
|
605 | 18 | moduleFactory = getModuleFactory(); |
606 | |
|
607 | 18 | config = ConfigurationLoader.loadConfiguration( configFile, |
608 | |
new PropertiesExpander( overridingProperties ) ); |
609 | 18 | String effectiveEncoding = |
610 | |
StringUtils.isNotEmpty( encoding ) ? encoding : System.getProperty( "file.encoding", "UTF-8" ); |
611 | 18 | if ( StringUtils.isEmpty( encoding ) ) |
612 | |
{ |
613 | 18 | getLog().warn( |
614 | |
"File encoding has not been set, using platform encoding " + effectiveEncoding |
615 | |
+ ", i.e. build is platform dependent!" ); |
616 | |
} |
617 | 18 | Configuration[] modules = config.getChildren(); |
618 | 84 | for ( int i = 0; i < modules.length; i++ ) |
619 | |
{ |
620 | 66 | Configuration module = modules[i]; |
621 | 66 | if ( "TreeWalker".equals( module.getName() ) |
622 | |
|| "com.puppycrawl.tools.checkstyle.TreeWalker".equals( module.getName() ) ) |
623 | |
{ |
624 | 18 | if ( module instanceof DefaultConfiguration ) |
625 | |
{ |
626 | 18 | ( (DefaultConfiguration) module ).addAttribute( "charset", effectiveEncoding ); |
627 | |
} |
628 | |
else |
629 | |
{ |
630 | 0 | getLog().warn( "Failed to configure file encoding on module " + module ); |
631 | |
} |
632 | |
} |
633 | |
} |
634 | |
|
635 | 18 | results = executeCheckstyle( config, moduleFactory ); |
636 | |
|
637 | 14 | ResourceBundle bundle = getBundle( locale ); |
638 | 14 | generateReportStatics(); |
639 | 14 | generateMainReport( results, config, moduleFactory, bundle ); |
640 | 14 | if ( enableRSS ) |
641 | |
{ |
642 | 14 | generateRSS( results ); |
643 | |
} |
644 | |
|
645 | |
} |
646 | 0 | catch ( CheckstyleException e ) |
647 | |
{ |
648 | 0 | throw new MavenReportException( "Failed during checkstyle configuration", e ); |
649 | |
} |
650 | |
finally |
651 | |
{ |
652 | |
|
653 | 18 | Thread.currentThread().setContextClassLoader( currentClassLoader ); |
654 | 14 | } |
655 | |
} |
656 | 14 | } |
657 | |
|
658 | |
private void generateReportStatics() |
659 | |
throws MavenReportException |
660 | |
{ |
661 | 14 | ReportResource rresource = new ReportResource( PLUGIN_RESOURCES, outputDirectory ); |
662 | |
try |
663 | |
{ |
664 | 14 | rresource.copy( "images/rss.png" ); |
665 | |
} |
666 | 0 | catch ( IOException e ) |
667 | |
{ |
668 | 0 | throw new MavenReportException( "Unable to copy static resources.", e ); |
669 | 14 | } |
670 | 14 | } |
671 | |
|
672 | |
private void generateRSS( CheckstyleResults results ) |
673 | |
throws MavenReportException |
674 | |
{ |
675 | 14 | VelocityTemplate vtemplate = new VelocityTemplate( velocityComponent, PLUGIN_RESOURCES ); |
676 | 14 | vtemplate.setLog( getLog() ); |
677 | |
|
678 | 14 | Context context = new VelocityContext(); |
679 | 14 | context.put( "results", results ); |
680 | 14 | context.put( "project", project ); |
681 | 14 | context.put( "copyright", getCopyright() ); |
682 | 14 | context.put( "levelInfo", SeverityLevel.INFO ); |
683 | 14 | context.put( "levelWarning", SeverityLevel.WARNING ); |
684 | 14 | context.put( "levelError", SeverityLevel.ERROR ); |
685 | 14 | context.put( "stringutils", new StringUtils() ); |
686 | |
|
687 | |
try |
688 | |
{ |
689 | 14 | vtemplate.generate( outputDirectory.getPath() + "/checkstyle.rss", "checkstyle-rss.vm", context ); |
690 | |
} |
691 | 0 | catch ( ResourceNotFoundException e ) |
692 | |
{ |
693 | 0 | throw new MavenReportException( "Unable to find checkstyle-rss.vm resource.", e ); |
694 | |
} |
695 | 0 | catch ( MojoExecutionException e ) |
696 | |
{ |
697 | 0 | throw new MavenReportException( "Unable to generate checkstyle.rss.", e ); |
698 | |
} |
699 | 0 | catch ( VelocityException e ) |
700 | |
{ |
701 | 0 | throw new MavenReportException( "Unable to generate checkstyle.rss.", e ); |
702 | |
} |
703 | 0 | catch ( IOException e ) |
704 | |
{ |
705 | 0 | throw new MavenReportException( "Unable to generate checkstyle.rss.", e ); |
706 | 14 | } |
707 | 14 | } |
708 | |
|
709 | |
private String getCopyright() |
710 | |
{ |
711 | |
String copyright; |
712 | 14 | int currentYear = Calendar.getInstance().get( Calendar.YEAR ); |
713 | 14 | if ( StringUtils.isNotEmpty( project.getInceptionYear() ) |
714 | |
&& !String.valueOf( currentYear ).equals( project.getInceptionYear() ) ) |
715 | |
{ |
716 | 14 | copyright = project.getInceptionYear() + " - " + currentYear; |
717 | |
} |
718 | |
else |
719 | |
{ |
720 | 0 | copyright = String.valueOf( currentYear ); |
721 | |
} |
722 | |
|
723 | 14 | if ( ( project.getOrganization() != null ) && StringUtils.isNotEmpty( project.getOrganization().getName() ) ) |
724 | |
{ |
725 | 14 | copyright = copyright + " " + project.getOrganization().getName(); |
726 | |
} |
727 | 14 | return copyright; |
728 | |
} |
729 | |
|
730 | |
private void generateMainReport( CheckstyleResults results, Configuration config, ModuleFactory moduleFactory, |
731 | |
ResourceBundle bundle ) |
732 | |
{ |
733 | 14 | CheckstyleReportGenerator generator = new CheckstyleReportGenerator( getSink(), bundle, project.getBasedir(), siteTool ); |
734 | |
|
735 | 14 | generator.setLog( getLog() ); |
736 | 14 | generator.setEnableRulesSummary( enableRulesSummary ); |
737 | 14 | generator.setEnableSeveritySummary( enableSeveritySummary ); |
738 | 14 | generator.setEnableFilesSummary( enableFilesSummary ); |
739 | 14 | generator.setEnableRSS( enableRSS ); |
740 | 14 | generator.setCheckstyleConfig( config ); |
741 | 14 | generator.setCheckstyleModuleFactory( moduleFactory ); |
742 | 14 | if ( linkXRef ) |
743 | |
{ |
744 | 14 | String relativePath = PathTool.getRelativePath( getOutputDirectory(), xrefLocation.getAbsolutePath() ); |
745 | 14 | if ( StringUtils.isEmpty( relativePath ) ) |
746 | |
{ |
747 | 14 | relativePath = "."; |
748 | |
} |
749 | 14 | relativePath = relativePath + "/" + xrefLocation.getName(); |
750 | 14 | if ( xrefLocation.exists() ) |
751 | |
{ |
752 | |
|
753 | |
|
754 | 2 | generator.setXrefLocation( relativePath ); |
755 | |
} |
756 | |
else |
757 | |
{ |
758 | |
|
759 | 12 | for ( Iterator reports = getProject().getReportPlugins().iterator(); reports.hasNext(); ) |
760 | |
{ |
761 | 12 | ReportPlugin report = (ReportPlugin) reports.next(); |
762 | |
|
763 | 12 | String artifactId = report.getArtifactId(); |
764 | 12 | if ( "maven-jxr-plugin".equals( artifactId ) || "jxr-maven-plugin".equals( artifactId ) ) |
765 | |
{ |
766 | 12 | generator.setXrefLocation( relativePath ); |
767 | |
} |
768 | |
} |
769 | |
} |
770 | |
|
771 | 14 | if ( generator.getXrefLocation() == null ) |
772 | |
{ |
773 | 0 | getLog().warn( "Unable to locate Source XRef to link to - DISABLED" ); |
774 | |
} |
775 | |
} |
776 | 14 | generator.generateReport( results ); |
777 | 14 | } |
778 | |
|
779 | |
|
780 | |
|
781 | |
|
782 | |
|
783 | |
|
784 | |
|
785 | |
private void mergeDeprecatedInfo() |
786 | |
{ |
787 | 20 | if ( "config/sun_checks.xml".equals( configLocation ) && !"sun".equals( format ) ) |
788 | |
{ |
789 | 2 | configLocation = (String) FORMAT_TO_CONFIG_LOCATION.get( format ); |
790 | |
} |
791 | |
|
792 | 20 | if ( StringUtils.isEmpty( propertiesLocation ) ) |
793 | |
{ |
794 | 20 | if ( propertiesFile != null ) |
795 | |
{ |
796 | 0 | propertiesLocation = propertiesFile.getPath(); |
797 | |
} |
798 | 20 | else if ( propertiesURL != null ) |
799 | |
{ |
800 | 0 | propertiesLocation = propertiesURL.toExternalForm(); |
801 | |
} |
802 | |
} |
803 | |
|
804 | 20 | if ( "LICENSE.txt".equals( headerLocation ) ) |
805 | |
{ |
806 | 2 | File defaultHeaderFile = new File( project.getBasedir(), "LICENSE.txt" ); |
807 | 2 | if ( !defaultHeaderFile.equals( headerFile ) ) |
808 | |
{ |
809 | 2 | headerLocation = headerFile.getPath(); |
810 | |
} |
811 | |
} |
812 | |
|
813 | 20 | if ( StringUtils.isEmpty( suppressionsLocation ) ) |
814 | |
{ |
815 | 20 | suppressionsLocation = suppressionsFile; |
816 | |
} |
817 | |
|
818 | 20 | if ( StringUtils.isEmpty( packageNamesLocation ) ) |
819 | |
{ |
820 | 20 | packageNamesLocation = packageNamesFile; |
821 | |
} |
822 | 20 | } |
823 | |
|
824 | |
private CheckstyleResults executeCheckstyle( Configuration config, ModuleFactory moduleFactory ) |
825 | |
throws MavenReportException, CheckstyleException |
826 | |
{ |
827 | |
File[] files; |
828 | |
try |
829 | |
{ |
830 | 18 | files = getFilesToProcess( includes, excludes ); |
831 | |
} |
832 | 0 | catch ( IOException e ) |
833 | |
{ |
834 | 0 | throw new MavenReportException( "Error getting files to process", e ); |
835 | 18 | } |
836 | |
|
837 | 18 | FilterSet filterSet = getSuppressions(); |
838 | |
|
839 | 18 | Checker checker = new Checker(); |
840 | |
|
841 | |
|
842 | |
|
843 | |
List classPathStrings; |
844 | 18 | List outputDirectories = new ArrayList(); |
845 | |
try |
846 | |
{ |
847 | 18 | classPathStrings = this.project.getCompileClasspathElements(); |
848 | 16 | outputDirectories.add( this.project.getBuild().getOutputDirectory() ); |
849 | |
|
850 | 16 | if ( includeTestSourceDirectory && ( testSourceDirectory != null ) && ( testSourceDirectory.exists() ) |
851 | |
&& ( testSourceDirectory.isDirectory() ) ) |
852 | |
{ |
853 | 0 | classPathStrings = this.project.getTestClasspathElements(); |
854 | 0 | outputDirectories.add( this.project.getBuild().getTestOutputDirectory() ); |
855 | |
} |
856 | |
} |
857 | 2 | catch ( DependencyResolutionRequiredException e ) |
858 | |
{ |
859 | 2 | throw new MavenReportException( e.getMessage(), e ); |
860 | 16 | } |
861 | |
|
862 | 16 | List urls = new ArrayList( classPathStrings.size() ); |
863 | |
|
864 | 16 | Iterator iter = classPathStrings.iterator(); |
865 | 32 | while ( iter.hasNext() ) |
866 | |
{ |
867 | |
try |
868 | |
{ |
869 | 16 | urls.add( new File( ( (String) iter.next() ) ).toURL() ); |
870 | |
} |
871 | 0 | catch ( MalformedURLException e ) |
872 | |
{ |
873 | 0 | throw new MavenReportException( e.getMessage(), e ); |
874 | 16 | } |
875 | |
} |
876 | |
|
877 | 16 | Iterator iterator = outputDirectories.iterator(); |
878 | 32 | while ( iterator.hasNext() ) |
879 | |
{ |
880 | |
try |
881 | |
{ |
882 | 16 | String outputDirectoryString = (String) iterator.next(); |
883 | 16 | if ( outputDirectoryString != null ) |
884 | |
{ |
885 | 0 | File outputDirectoryFile = new File( outputDirectoryString ); |
886 | 0 | if ( outputDirectoryFile.exists() ) |
887 | |
{ |
888 | 0 | URL outputDirectoryUrl = outputDirectoryFile.toURL(); |
889 | 0 | getLog().debug( "Adding the outputDirectory " + outputDirectoryUrl.toString() |
890 | |
+ " to the Checkstyle class path" ); |
891 | 0 | urls.add( outputDirectoryUrl ); |
892 | |
} |
893 | |
} |
894 | |
} |
895 | 0 | catch ( MalformedURLException e ) |
896 | |
{ |
897 | 0 | throw new MavenReportException( e.getMessage(), e ); |
898 | |
} |
899 | 0 | catch ( IOException e ) |
900 | |
{ |
901 | 0 | throw new MavenReportException( e.getMessage(), e ); |
902 | 16 | } |
903 | |
} |
904 | |
|
905 | 16 | URLClassLoader projectClassLoader = new URLClassLoader( (URL[]) urls.toArray( new URL[urls.size()] ), null ); |
906 | 16 | checker.setClassloader( projectClassLoader ); |
907 | |
|
908 | 16 | if ( moduleFactory != null ) |
909 | |
{ |
910 | 16 | checker.setModuleFactory( moduleFactory ); |
911 | |
} |
912 | |
|
913 | 16 | if ( filterSet != null ) |
914 | |
{ |
915 | 0 | checker.addFilter( filterSet ); |
916 | |
} |
917 | |
|
918 | 16 | checker.configure( config ); |
919 | |
|
920 | 16 | AuditListener listener = getListener(); |
921 | |
|
922 | 16 | if ( listener != null ) |
923 | |
{ |
924 | 16 | checker.addListener( listener ); |
925 | |
} |
926 | |
|
927 | 16 | if ( consoleOutput ) |
928 | |
{ |
929 | 4 | checker.addListener( getConsoleListener() ); |
930 | |
} |
931 | |
|
932 | 16 | CheckstyleReportListener sinkListener = new CheckstyleReportListener( sourceDirectory ); |
933 | 16 | if ( includeTestSourceDirectory && ( testSourceDirectory != null ) && ( testSourceDirectory.exists() ) |
934 | |
&& ( testSourceDirectory.isDirectory() ) ) |
935 | |
{ |
936 | 0 | sinkListener.addSourceDirectory( testSourceDirectory ); |
937 | |
} |
938 | |
|
939 | 16 | checker.addListener( sinkListener ); |
940 | |
|
941 | 16 | int nbErrors = checker.process( files ); |
942 | |
|
943 | 16 | checker.destroy(); |
944 | |
|
945 | 16 | if ( stringOutputStream != null ) |
946 | |
{ |
947 | 2 | getLog().info( stringOutputStream.toString() ); |
948 | |
} |
949 | |
|
950 | 16 | if ( failsOnError && nbErrors > 0 ) |
951 | |
{ |
952 | |
|
953 | |
|
954 | |
|
955 | 2 | throw new MavenReportException( "There are " + nbErrors + " checkstyle errors." ); |
956 | |
} |
957 | 14 | else if ( nbErrors > 0 ) |
958 | |
{ |
959 | 14 | getLog().info( "There are " + nbErrors + " checkstyle errors." ); |
960 | |
} |
961 | |
|
962 | 14 | return sinkListener.getResults(); |
963 | |
} |
964 | |
|
965 | |
|
966 | |
|
967 | |
|
968 | |
public String getOutputName() |
969 | |
{ |
970 | 54 | return "checkstyle"; |
971 | |
} |
972 | |
|
973 | |
private AuditListener getListener() |
974 | |
throws MavenReportException |
975 | |
{ |
976 | 16 | AuditListener listener = null; |
977 | |
|
978 | 16 | if ( StringUtils.isNotEmpty( outputFileFormat ) ) |
979 | |
{ |
980 | 16 | File resultFile = outputFile; |
981 | |
|
982 | 16 | OutputStream out = getOutputStream( resultFile ); |
983 | |
|
984 | 16 | if ( "xml".equals( outputFileFormat ) ) |
985 | |
{ |
986 | 14 | listener = new XMLLogger( out, true ); |
987 | |
} |
988 | 2 | else if ( "plain".equals( outputFileFormat ) ) |
989 | |
{ |
990 | 2 | listener = new DefaultLogger( out, true ); |
991 | |
} |
992 | |
else |
993 | |
{ |
994 | |
|
995 | 0 | throw new MavenReportException( "Invalid output file format: (" + outputFileFormat |
996 | |
+ "). Must be 'plain' or 'xml'." ); |
997 | |
} |
998 | |
} |
999 | |
|
1000 | 16 | return listener; |
1001 | |
} |
1002 | |
|
1003 | |
private OutputStream getOutputStream( File file ) |
1004 | |
throws MavenReportException |
1005 | |
{ |
1006 | 18 | File parentFile = file.getAbsoluteFile().getParentFile(); |
1007 | |
|
1008 | 18 | if ( !parentFile.exists() ) |
1009 | |
{ |
1010 | 0 | parentFile.mkdirs(); |
1011 | |
} |
1012 | |
|
1013 | |
FileOutputStream fileOutputStream; |
1014 | |
try |
1015 | |
{ |
1016 | 18 | fileOutputStream = new FileOutputStream( file ); |
1017 | |
} |
1018 | 0 | catch ( FileNotFoundException e ) |
1019 | |
{ |
1020 | 0 | throw new MavenReportException( "Unable to create output stream: " + file, e ); |
1021 | 18 | } |
1022 | 18 | return fileOutputStream; |
1023 | |
} |
1024 | |
|
1025 | |
private File[] getFilesToProcess( String includes, String excludes ) |
1026 | |
throws IOException |
1027 | |
{ |
1028 | 18 | StringBuffer excludesStr = new StringBuffer(); |
1029 | |
|
1030 | 18 | if ( StringUtils.isNotEmpty( excludes ) ) |
1031 | |
{ |
1032 | 0 | excludesStr.append( excludes ); |
1033 | |
} |
1034 | |
|
1035 | 18 | String[] defaultExcludes = FileUtils.getDefaultExcludes(); |
1036 | 360 | for ( int i = 0; i < defaultExcludes.length; i++ ) |
1037 | |
{ |
1038 | 342 | if ( excludesStr.length() > 0 ) |
1039 | |
{ |
1040 | 324 | excludesStr.append( "," ); |
1041 | |
} |
1042 | |
|
1043 | 342 | excludesStr.append( defaultExcludes[i] ); |
1044 | |
} |
1045 | |
|
1046 | 18 | List files = FileUtils.getFiles( sourceDirectory, includes, excludesStr.toString() ); |
1047 | 18 | if ( includeTestSourceDirectory && ( testSourceDirectory != null ) && ( testSourceDirectory.exists() ) |
1048 | |
&& ( testSourceDirectory.isDirectory() ) ) |
1049 | |
{ |
1050 | 0 | files.addAll( FileUtils.getFiles( testSourceDirectory, includes, excludesStr.toString() ) ); |
1051 | |
} |
1052 | |
|
1053 | 18 | return (File[]) files.toArray( EMPTY_FILE_ARRAY ); |
1054 | |
} |
1055 | |
|
1056 | |
private Properties getOverridingProperties() |
1057 | |
throws MavenReportException |
1058 | |
{ |
1059 | 18 | Properties p = new Properties(); |
1060 | |
|
1061 | |
try |
1062 | |
{ |
1063 | 18 | File propertiesFile = locator.resolveLocation( propertiesLocation, "checkstyle-checker.properties" ); |
1064 | |
|
1065 | 18 | if ( propertiesFile != null ) |
1066 | |
{ |
1067 | 0 | p.load( new FileInputStream( propertiesFile ) ); |
1068 | |
} |
1069 | |
|
1070 | 18 | if ( StringUtils.isNotEmpty( propertyExpansion ) ) |
1071 | |
{ |
1072 | |
|
1073 | 0 | propertyExpansion = StringUtils.replace( propertyExpansion, "\\", "\\\\" ); |
1074 | 0 | p.load( new StringInputStream( propertyExpansion ) ); |
1075 | |
} |
1076 | |
|
1077 | |
|
1078 | |
|
1079 | |
|
1080 | 18 | if ( "config/maven_checks.xml".equals( configLocation ) ) |
1081 | |
{ |
1082 | 2 | if ( "LICENSE.txt".equals( headerLocation ) ) |
1083 | |
{ |
1084 | 0 | headerLocation = "config/maven-header.txt"; |
1085 | |
} |
1086 | |
} |
1087 | 18 | if ( StringUtils.isNotEmpty( headerLocation ) ) |
1088 | |
{ |
1089 | |
try |
1090 | |
{ |
1091 | 18 | File headerFile = locator.resolveLocation( headerLocation, "checkstyle-header.txt" ); |
1092 | |
|
1093 | 18 | if ( headerFile != null ) |
1094 | |
{ |
1095 | 18 | p.setProperty( "checkstyle.header.file", headerFile.getAbsolutePath() ); |
1096 | |
} |
1097 | |
} |
1098 | 0 | catch ( IOException e ) |
1099 | |
{ |
1100 | 0 | throw new MavenReportException( "Unable to process header location: " + headerLocation, e ); |
1101 | 18 | } |
1102 | |
} |
1103 | |
|
1104 | 18 | if ( cacheFile != null ) |
1105 | |
{ |
1106 | 18 | p.setProperty( "checkstyle.cache.file", cacheFile ); |
1107 | |
} |
1108 | |
} |
1109 | 0 | catch ( IOException e ) |
1110 | |
{ |
1111 | 0 | throw new MavenReportException( "Failed to get overriding properties", e ); |
1112 | 18 | } |
1113 | |
|
1114 | 18 | if ( suppressionsFileExpression != null ) |
1115 | |
{ |
1116 | 0 | String suppresionFile = getSuppressionLocation(); |
1117 | |
|
1118 | 0 | if ( suppresionFile != null ) |
1119 | |
{ |
1120 | 0 | p.setProperty( suppressionsFileExpression, suppresionFile ); |
1121 | |
} |
1122 | |
} |
1123 | |
|
1124 | 18 | return p; |
1125 | |
} |
1126 | |
|
1127 | |
private String getConfigFile() |
1128 | |
throws MavenReportException |
1129 | |
{ |
1130 | |
try |
1131 | |
{ |
1132 | 18 | File configFile = locator.getResourceAsFile( configLocation, "checkstyle-checker.xml" ); |
1133 | |
|
1134 | 18 | if ( configFile == null ) |
1135 | |
{ |
1136 | 0 | throw new MavenReportException( "Unable to process config location: " + configLocation ); |
1137 | |
} |
1138 | 18 | return configFile.getAbsolutePath(); |
1139 | |
} |
1140 | 0 | catch ( org.codehaus.plexus.resource.loader.ResourceNotFoundException e ) |
1141 | |
{ |
1142 | 0 | throw new MavenReportException( "Unable to find configuration file at location " |
1143 | |
+ configLocation, e ); |
1144 | |
} |
1145 | 0 | catch ( FileResourceCreationException e ) |
1146 | |
{ |
1147 | 0 | throw new MavenReportException( "Unable to process configuration file location " |
1148 | |
+ configLocation, e ); |
1149 | |
} |
1150 | |
|
1151 | |
} |
1152 | |
|
1153 | |
private ModuleFactory getModuleFactory() |
1154 | |
throws CheckstyleException |
1155 | |
{ |
1156 | |
|
1157 | 18 | ModuleFactory moduleFactory = PackageNamesLoader.loadModuleFactory( Thread.currentThread() |
1158 | |
.getContextClassLoader() ); |
1159 | |
|
1160 | |
try |
1161 | |
{ |
1162 | |
|
1163 | 18 | File packageNamesFile = locator.resolveLocation( packageNamesLocation, "checkstyle-packages.xml" ); |
1164 | |
|
1165 | 18 | if ( packageNamesFile != null ) |
1166 | |
{ |
1167 | |
|
1168 | 0 | moduleFactory = PackageNamesLoader.loadModuleFactory( packageNamesFile.getAbsolutePath() ); |
1169 | |
} |
1170 | |
} |
1171 | 0 | catch ( IOException e ) |
1172 | |
{ |
1173 | 0 | getLog().error( "Unable to process package names location: " + packageNamesLocation, e ); |
1174 | 18 | } |
1175 | 18 | return moduleFactory; |
1176 | |
} |
1177 | |
|
1178 | |
private String getSuppressionLocation() |
1179 | |
throws MavenReportException |
1180 | |
{ |
1181 | |
try |
1182 | |
{ |
1183 | 0 | File suppressionsFile = locator.resolveLocation( suppressionsLocation, "checkstyle-suppressions.xml" ); |
1184 | |
|
1185 | 0 | if ( suppressionsFile == null ) |
1186 | |
{ |
1187 | 0 | return null; |
1188 | |
} |
1189 | |
|
1190 | 0 | return suppressionsFile.getAbsolutePath(); |
1191 | |
} |
1192 | 0 | catch ( IOException e ) |
1193 | |
{ |
1194 | 0 | throw new MavenReportException( "Failed to process supressions location: " + suppressionsLocation, e ); |
1195 | |
} |
1196 | |
} |
1197 | |
|
1198 | |
private FilterSet getSuppressions() |
1199 | |
throws MavenReportException |
1200 | |
{ |
1201 | |
try |
1202 | |
{ |
1203 | 18 | File suppressionsFile = locator.resolveLocation( suppressionsLocation, "checkstyle-suppressions.xml" ); |
1204 | |
|
1205 | 18 | if ( suppressionsFile == null ) |
1206 | |
{ |
1207 | 18 | return null; |
1208 | |
} |
1209 | |
|
1210 | 0 | return SuppressionsLoader.loadSuppressions( suppressionsFile.getAbsolutePath() ); |
1211 | |
} |
1212 | 0 | catch ( CheckstyleException ce ) |
1213 | |
{ |
1214 | 0 | throw new MavenReportException( "failed to load suppressions location: " + suppressionsLocation, ce ); |
1215 | |
} |
1216 | 0 | catch ( IOException e ) |
1217 | |
{ |
1218 | 0 | throw new MavenReportException( "Failed to process supressions location: " + suppressionsLocation, e ); |
1219 | |
} |
1220 | |
} |
1221 | |
|
1222 | |
private DefaultLogger getConsoleListener() |
1223 | |
throws MavenReportException |
1224 | |
{ |
1225 | |
DefaultLogger consoleListener; |
1226 | |
|
1227 | 4 | if ( useFile == null ) |
1228 | |
{ |
1229 | 2 | stringOutputStream = new StringOutputStream(); |
1230 | 2 | consoleListener = new DefaultLogger( stringOutputStream, false ); |
1231 | |
} |
1232 | |
else |
1233 | |
{ |
1234 | 2 | OutputStream out = getOutputStream( useFile ); |
1235 | |
|
1236 | 2 | consoleListener = new DefaultLogger( out, true ); |
1237 | |
} |
1238 | |
|
1239 | 4 | return consoleListener; |
1240 | |
} |
1241 | |
|
1242 | |
private static ResourceBundle getBundle( Locale locale ) |
1243 | |
{ |
1244 | 38 | return ResourceBundle.getBundle( "checkstyle-report", locale, CheckstyleReport.class.getClassLoader() ); |
1245 | |
} |
1246 | |
|
1247 | |
|
1248 | |
|
1249 | |
|
1250 | |
public boolean canGenerateReport() |
1251 | |
{ |
1252 | |
|
1253 | 20 | return sourceDirectory.exists(); |
1254 | |
} |
1255 | |
|
1256 | |
|
1257 | |
|
1258 | |
|
1259 | |
public void setReportOutputDirectory( File reportOutputDirectory ) |
1260 | |
{ |
1261 | 0 | super.setReportOutputDirectory( reportOutputDirectory ); |
1262 | 0 | this.outputDirectory = reportOutputDirectory; |
1263 | 0 | } |
1264 | |
} |