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