forked from gleclaire/findbugs-maven-plugin
-
Notifications
You must be signed in to change notification settings - Fork 51
/
SpotBugsMojo.groovy
1187 lines (945 loc) · 35 KB
/
SpotBugsMojo.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
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
842
843
844
845
846
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
878
879
880
881
882
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
920
921
922
923
924
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
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package org.codehaus.mojo.spotbugs
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import groovy.xml.StreamingMarkupBuilder
import org.apache.maven.artifact.Artifact
import org.apache.maven.artifact.repository.ArtifactRepository
import org.apache.maven.artifact.resolver.ArtifactResolver
import org.apache.maven.doxia.siterenderer.Renderer
import org.apache.maven.doxia.tools.SiteTool
import org.apache.maven.plugin.MojoExecutionException
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.LifecyclePhase
import org.apache.maven.plugins.annotations.Mojo
import org.apache.maven.plugins.annotations.Parameter
import org.apache.maven.plugins.annotations.ResolutionScope
import org.apache.maven.project.MavenProject
import org.apache.maven.reporting.AbstractMavenReport
import org.apache.maven.repository.RepositorySystem
import org.codehaus.plexus.resource.ResourceManager
import org.codehaus.plexus.resource.loader.FileResourceCreationException
import org.codehaus.plexus.resource.loader.FileResourceLoader
/**
* Generates a SpotBugs Report when the site plugin is run.
* The HTML report is generated for site commands only.
*
*
* @author <a href="mailto:gleclaire@codehaus.org">Garvin LeClaire</a>
*/
@Mojo(name = "spotbugs", requiresDependencyResolution = ResolutionScope.TEST, requiresProject = true, threadSafe = true)
class SpotBugsMojo extends AbstractMavenReport implements SpotBugsPluginsTrait {
/**
* Location where generated html will be created.
*
*/
@Parameter(defaultValue = '${project.reporting.outputDirectory}', required = true)
File outputDirectory
/**
* Turn on and off xml output of the Spotbugs report.
*
* @since 1.0.0
*/
@Parameter(defaultValue = "false", property = "spotbugs.xmlOutput", required = true)
boolean xmlOutput
/**
* Specifies the directory where the xml output will be generated.
*
* @since 1.0.0
*/
@Parameter(defaultValue = '${project.build.directory}', required = true)
File xmlOutputDirectory
/**
* This has been deprecated and is on by default.
*
* @since 1.2.0
*
*/
@Deprecated
@Parameter(defaultValue = "true")
boolean spotbugsXmlOutput
/**
* Specifies the directory where the Spotbugs native xml output will be generated.
*
* @since 1.2.0
*/
@Parameter(defaultValue = '${project.build.directory}', required = true)
File spotbugsXmlOutputDirectory
/**
* Doxia Site Renderer.
*/
@Component(role = Renderer.class)
Renderer siteRenderer
/**
* Directory containing the class files for Spotbugs to analyze.
*/
@Parameter(defaultValue = '${project.build.outputDirectory}', required = true)
File classFilesDirectory
/**
* Directory containing the test class files for Spotbugs to analyze.
*
*/
@Parameter(defaultValue = '${project.build.testOutputDirectory}', required = true)
File testClassFilesDirectory
/**
* Location of the Xrefs to link to.
*
*/
@Parameter(defaultValue = '${project.reporting.outputDirectory}/xref')
File xrefLocation
/**
* Location of the Test Xrefs to link to.
*
*/
@Parameter(defaultValue = '${project.reporting.outputDirectory}/xref-test')
File xrefTestLocation
/**
* The directories containing the sources to be compiled.
*
*/
@Parameter(defaultValue = '${project.compileSourceRoots}', required = true, readonly = true)
List compileSourceRoots
/**
* The directories containing the test-sources to be compiled.
*
* @since 2.0
*/
@Parameter(defaultValue = '${project.testCompileSourceRoots}', required = true, readonly = true)
List testSourceRoots
/**
* Run Spotbugs on the tests.
*
* @since 2.0
*/
@Parameter(defaultValue = "false", property = "spotbugs.includeTests")
boolean includeTests
/**
* List of artifacts this plugin depends on. Used for resolving the Spotbugs coreplugin.
*
*/
@Parameter(property = "plugin.artifacts", required = true, readonly = true)
List pluginArtifacts
/**
* List of Remote Repositories used by the resolver
*
*/
@Parameter(property = "project.remoteArtifactRepositories", required = true, readonly = true)
List remoteRepositories
/**
* The local repository, needed to download the coreplugin jar.
*
*/
@Parameter(property = "localRepository", required = true, readonly = true)
ArtifactRepository localRepository
/**
* Remote repositories which will be searched for the coreplugin jar.
*
*/
@Parameter(property = "project.remoteArtifactRepositories", required = true, readonly = true)
List remoteArtifactRepositories
/**
* Maven Project
*
*/
@Parameter(property = "project", required = true, readonly = true)
MavenProject project
/**
* Encoding used for xml files. Default value is UTF-8.
*
*/
@Parameter(defaultValue = "UTF-8", readonly = true)
String xmlEncoding
/**
* The file encoding to use when reading the source files. If the property <code>project.build.sourceEncoding</code>
* is not set, the platform default encoding is used.
*
* @since 2.2
*/
@Parameter(defaultValue = '${project.build.sourceEncoding}', property = "encoding")
String sourceEncoding
/**
* The file encoding to use when creating the HTML reports. If the property <code>project.reporting.outputEncoding</code>
* is not set, the platform default encoding is used.
*
* @since 2.2
*/
@Parameter(defaultValue = '${project.reporting.outputEncoding}', property = "outputEncoding")
String outputEncoding
/**
* Threshold of minimum bug severity to report. Valid values are High, Default, Low, Ignore, and Exp (for experimental).
*
*/
@Parameter(defaultValue = "Default", property = "spotbugs.threshold")
String threshold
/**
* Artifact resolver, needed to download the coreplugin jar.
*/
@Component(role = ArtifactResolver.class)
ArtifactResolver artifactResolver
/**
* Used to look up Artifacts in the remote repository.
*
*/
@Component(role = RepositorySystem.class)
RepositorySystem factory
/**
* <p>
* File name of the include filter. Only bugs in matching the filters are reported.
* </p>
*
* <p>
* Potential values are a filesystem path, a URL, or a classpath resource.
* </p>
*
* <p>
* This parameter is resolved as resource, URL, then file. If successfully
* resolved, the contents of the configuration is copied into the
* <code>${project.build.directory}</code>
* directory before being passed to Spotbugs as a filter file.
* It supports multiple files separated by a comma
* </p>
*
* @since 1.0-beta-1
*/
@Parameter(property = "spotbugs.includeFilterFile")
String includeFilterFile
/**
* <p>
* File name of the exclude filter. Bugs matching the filters are not reported.
* </p>
*
* <p>
* Potential values are a filesystem path, a URL, or a classpath resource.
* </p>
*
* <p>
* This parameter is resolved as resource, URL, then file. If successfully
* resolved, the contents of the configuration is copied into the
* <code>${project.build.directory}</code>
* directory before being passed to Spotbugs as a filter file.
* It supports multiple files separated by a comma
* </p>
*
* @since 1.0-beta-1
*/
@Parameter(property = "spotbugs.excludeFilterFile")
String excludeFilterFile
/**
* <p>
* File names of the baseline files. Bugs found in the baseline files won't be reported.
* </p>
*
* <p>
* Potential values are a filesystem path, a URL, or a classpath resource.
* </p>
*
* <p>
* This parameter is resolved as resource, URL, then file. If successfully
* resolved, the contents of the configuration is copied into the
* <code>${project.build.directory}</code>
* directory before being passed to Spotbugs as a filter file.
* </p>
*
* This is a comma-delimited list.
*
* @since 2.4.1
*/
@Parameter(property = "spotbugs.excludeBugsFile")
String excludeBugsFile
/**
* Effort of the bug finders. Valid values are Min, Default and Max.
*
* @since 1.0-beta-1
*/
@Parameter(defaultValue = "Default", property = "spotbugs.effort")
String effort
/**
* turn on Spotbugs debugging
*
*/
@Parameter(defaultValue = "false", property = "spotbugs.debug")
Boolean debug
/**
* Relaxed reporting mode. For many detectors, this option suppresses the heuristics used to avoid reporting false
* positives.
*
* @since 1.1
*/
@Parameter(defaultValue = "false", property = "spotbugs.relaxed")
Boolean relaxed
/**
* The visitor list to run. This is a comma-delimited list.
*
* @since 1.0-beta-1
*/
@Parameter(property = "spotbugs.visitors")
String visitors
/**
* The visitor list to omit. This is a comma-delimited list.
*
* @since 1.0-beta-1
*/
@Parameter(property = "spotbugs.omitVisitors")
String omitVisitors
/**
* <p>
* The plugin list to include in the report. This is a comma-delimited list.
* </p>
*
* <p>
* Potential values are a filesystem path, a URL, or a classpath resource.
* </p>
*
* <p>
* This parameter is resolved as resource, URL, then file. If successfully
* resolved, the contents of the configuration is copied into the
* <code>${project.build.directory}</code>
* directory before being passed to Spotbugs as a plugin file.
* </p>
*
* @since 1.0-beta-1
*/
@Parameter(property = "spotbugs.pluginList")
String pluginList
/**
* <p>
* Collection of PluginArtifact to work on. (PluginArtifact contains groupId, artifactId, version, type.)
* See <a href="./usage.html#Using Detectors from a Repository">Usage</a> for details.
* </p>
*
*
* @since 2.4.1
*/
@Parameter
PluginArtifact[] plugins;
/**
* Restrict analysis to the given comma-separated list of classes and packages.
*
* @since 1.1
*/
@Parameter(property = "spotbugs.onlyAnalyze")
String onlyAnalyze
/**
* This option enables or disables scanning of nested jar and zip files found
* in the list of files and directories to be analyzed.
*
* @since 2.3.2
*/
@Parameter(property = "spotbugs.nested", defaultValue = "false")
Boolean nested
/**
* Prints a trace of detectors run and classes analyzed to standard output.
* Useful for troubleshooting unexpected analysis failures.
*
* @since 2.3.2
*/
@Parameter(property = "spotbugs.trace", defaultValue = "false")
Boolean trace
/**
* Maximum bug ranking to record.
*
* @since 2.4.1
*/
@Parameter(property = "spotbugs.maxRank")
int maxRank
/**
* Skip entire check.
*
* @since 1.1
*/
@Parameter(property = "spotbugs.skip", defaultValue = "false")
boolean skip
/**
* @since 2.0
*/
@Component(role = ResourceManager.class)
ResourceManager resourceManager
/**
* SiteTool.
*
* @since 2.1-SNAPSHOT
*/
@Component(role = SiteTool.class)
SiteTool siteTool
/**
* Fail the build on an error.
*
* @since 2.0
*/
@Parameter(property = "spotbugs.failOnError", defaultValue = "true")
boolean failOnError
/**
* Fork a VM for Spotbugs analysis. This will allow you to set timeouts and heap size
*
* @since 2.3.2
*/
@Parameter(property = "spotbugs.fork", defaultValue = "true")
boolean fork
/**
* Maximum Java heap size in megabytes (default=512).
* This only works if the <b>fork</b> parameter is set <b>true</b>.
*
* @since 2.2
*/
@Parameter(property = "spotbugs.maxHeap", defaultValue = "512")
int maxHeap
/**
* Specifies the amount of time, in milliseconds, that Spotbugs may run before
* it is assumed to be hung and is terminated.
* The default is 600,000 milliseconds, which is ten minutes.
* This only works if the <b>fork</b> parameter is set <b>true</b>.
*
* @since 2.2
*/
@Parameter(property = "spotbugs.timeout", defaultValue = "600000")
int timeout
/**
* <p>
* the arguments to pass to the forked VM (ignored if fork is disabled).
* </p>
*
* @since 2.4.1
*/
@Parameter(property = "spotbugs.jvmArgs")
String jvmArgs
/**
* Skip the Spotbugs HTML report generation if there are no violations found. Defaults to
* <code>false</code>.
*
* @since 3.0.1
*/
@Parameter(property = "spotbugs.skipEmptyReport", defaultValue = "false")
boolean skipEmptyReport
/**
* Set the path of the user preferences file to use.
* Will try to read the path as a resource before treating it as a local path.
*
* This will read in a configuration file to set up Spotbugs.
*
* The parameters in the POM file will override anything in the config file
*
* @since 3.0.2
*/
@Parameter(property = "spotbugs.userPrefs")
String userPrefs
int bugCount
int errorCount
ResourceBundle bundle
File outputSpotbugsFile
/**
* Checks whether prerequisites for generating this report are given.
*
* @return true if report can be generated, otherwise false
* @see org.apache.maven.reporting.MavenReport#canGenerateReport()
*/
boolean canGenerateReport() {
def canGenerate = false
log.debug("****** SpotBugsMojo canGenerateReport *******")
if (!skip && classFilesDirectory.exists()) {
classFilesDirectory.eachFileRecurse {
if (it.name.contains(SpotBugsInfo.CLASS_SUFFIX)) {
canGenerate = true
}
}
log.debug("canGenerate Src is ${canGenerate}")
}
if (!skip && testClassFilesDirectory.exists() && includeTests) {
testClassFilesDirectory.eachFileRecurse {
if (it.name.contains(SpotBugsInfo.CLASS_SUFFIX)) {
canGenerate = true
}
}
log.debug("canGenerate Test Src is ${canGenerate}")
}
if (canGenerate && outputSpotbugsFile == null) {
outputSpotbugsFile = new File("${spotbugsXmlOutputDirectory}/spotbugsXml.xml")
ClassLoader tccl = Thread.currentThread().getContextClassLoader();
try {
// The SAX parser factory will fail with CCE if the TCCL is out of sync with our class loader
// This for Maven 2.2.1 only MFINDBUGS-178
Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
executeSpotbugs(outputSpotbugsFile)
} finally {
Thread.currentThread().setContextClassLoader(tccl);
}
if (skipEmptyReport && bugCount == 0) {
canGenerate = false
}
}
log.debug("canGenerate is ${canGenerate}")
return canGenerate
}
/**
* Returns the plugins description for the "generated reports" overview page.
*
* @param locale
* the locale the report should be generated for
*
* @return description of the report
* @see org.apache.maven.reporting.MavenReport#getDescription(java.util.Locale)
*/
String getDescription(Locale locale) {
return getBundle(locale).getString(SpotBugsInfo.DESCRIPTION_KEY)
}
/**
* Returns the plugins name for the "generated reports" overview page and the menu.
*
* @param locale
* the locale the report should be generated for
*
* @return name of the report
* @see org.apache.maven.reporting.MavenReport#getName(java.util.Locale)
*/
String getName(Locale locale) {
return getBundle(locale).getString(SpotBugsInfo.NAME_KEY)
}
/**
* Returns report output file name, without the extension.
*
* Called by AbstractMavenReport.execute() for creating the sink.
*
* @return name of the generated page
* @see org.apache.maven.reporting.MavenReport#getOutputName()
*/
String getOutputName() {
return SpotBugsInfo.PLUGIN_NAME
}
/**
* Executes the generation of the report.
*
* Callback from Maven Site Plugin.
*
* @param locale he wanted locale to generate the report, could be null.
*
* @see org.apache.maven.reporting.MavenReport #executeReport(java.util.Locale)
*/
void executeReport(Locale locale) {
log.debug("****** SpotBugsMojo executeReport *******")
executeCheck(locale)
if (!skip && canGenerateReport()) {
log.debug("Locale is ${locale.getLanguage()}")
log.debug("****** SpotBugsMojo executeReport *******")
log.debug("report Output Directory is " + getReportOutputDirectory())
log.debug("Output Directory is " + outputDirectory)
log.debug("Classes Directory is " + classFilesDirectory)
log.debug(" Plugin Artifacts to be added ->" + pluginArtifacts.toString())
generateXDoc(locale)
if (!outputDirectory.exists()) {
if (!outputDirectory.mkdirs()) {
throw new MojoExecutionException("Cannot create html output directory")
}
}
if (outputSpotbugsFile != null && outputSpotbugsFile.exists()) {
if (skipEmptyReport && bugCount == 0) {
log.info("Skipping Generation Spotbugs HTML since there are not any bugs")
} else {
log.debug("Generating Spotbugs HTML")
SpotbugsReportGenerator generator = new SpotbugsReportGenerator(getSink(), getBundle(locale), this.project.getBasedir(), siteTool)
boolean isJxrPluginEnabled = isJxrPluginEnabled()
generator.setIsJXRReportEnabled(isJxrPluginEnabled)
if (isJxrPluginEnabled) {
generator.setCompileSourceRoots(this.compileSourceRoots)
generator.setTestSourceRoots(this.testSourceRoots)
generator.setXrefLocation(this.xrefLocation)
generator.setXrefTestLocation(this.xrefTestLocation)
generator.setIncludeTests(this.includeTests)
}
generator.setLog(log)
generator.threshold = threshold
generator.effort = effort
generator.setSpotbugsResults(new XmlSlurper().parse(outputSpotbugsFile))
generator.setOutputDirectory(new File(outputDirectory.getAbsolutePath()))
generator.generateReport()
log.debug("xmlOutput is ${xmlOutput}")
}
}
} else {
log.info("cannot generate report");
}
}
public void execute() {
log.debug("****** SpotBugsMojo execute *******")
Locale locale = Locale.getDefault()
if (!skip) {
executeCheck(locale)
if (canGenerateReport()) {
generateXDoc(locale)
}
}
}
private void executeCheck(Locale locale) {
log.debug("****** SpotBugsMojo executeCheck *******")
log.debug("Generating Spotbugs XML")
if (!spotbugsXmlOutputDirectory.exists()) {
if (!spotbugsXmlOutputDirectory.mkdirs()) {
throw new MojoExecutionException("Cannot create xml output directory")
}
}
}
private void generateXDoc(Locale locale) {
log.debug("****** SpotBugsMojo generateXDoc *******")
if (outputSpotbugsFile != null && outputSpotbugsFile.exists()) {
log.debug("xmlOutput is ${xmlOutput}")
if (xmlOutput) {
log.debug(" Using the xdoc format")
if (!xmlOutputDirectory.exists()) {
if (!xmlOutputDirectory.mkdirs()) {
throw new MojoExecutionException("Cannot create xdoc output directory")
}
}
XDocsReporter xDocsReporter = new XDocsReporter(getBundle(locale), log, threshold, effort, outputEncoding)
xDocsReporter.setOutputWriter(new OutputStreamWriter(new FileOutputStream(new File("${xmlOutputDirectory}/spotbugs.xml")), outputEncoding))
xDocsReporter.setSpotbugsResults(new XmlSlurper().parse(outputSpotbugsFile))
xDocsReporter.setCompileSourceRoots(this.compileSourceRoots)
xDocsReporter.setTestSourceRoots(this.testSourceRoots)
xDocsReporter.generateReport()
}
}
}
/**
* Returns the report output directory.
*
* Called by AbstractMavenReport.execute() for creating the sink.
*
* @return full path to the directory where the files in the site get copied to
* @see org.apache.maven.reporting.AbstractMavenReport#getOutputDirectory()
*/
protected String getOutputDirectory() {
return outputDirectory.getAbsolutePath()
}
/**
* Return the project.
*
* @return the project.
* @see org.apache.maven.reporting.AbstractMavenReport#getProject()
*/
protected MavenProject getProject() {
return this.project
}
/**
* Return the Site Renderer.
*
*/
protected Renderer getSiteRenderer() {
return this.siteRenderer
}
/**
* Determines if the JXR-Plugin is included in the report section of the POM.
*
* @param bundle
* The bundle to load the artifactIf of the jxr plugin.
* @return True if the JXR-Plugin is included in the POM, false otherwise.
*
*/
protected boolean isJxrPluginEnabled() {
boolean isEnabled = false
if (xrefLocation.exists()) {
isEnabled = true
return isEnabled
}
List reportPlugins = getProject().getReportPlugins()
reportPlugins.each() { reportPlugin ->
log.debug("report plugin -> ${reportPlugin.getArtifactId()}")
if ("maven-jxr-plugin".equals(reportPlugin.getArtifactId()) || "jxr-maven-plugin".equals(reportPlugin.getArtifactId())) {
isEnabled = true
}
}
log.debug("jxr report links are ${isEnabled ? "enabled" : "disabled"}")
return isEnabled
}
ResourceBundle getBundle(locale) {
this.bundle = ResourceBundle.getBundle(SpotBugsInfo.BUNDLE_NAME, locale, SpotBugsMojo.class.getClassLoader())
log.debug("Mojo Locale is " + this.bundle.getLocale().getLanguage())
return bundle
}
/**
* Get the Spotbugs command line arguments.
*
* @param Spotbugs temp output file
*
* @return Spotbugs command line arguments.
*
*/
private ArrayList<String> getSpotbugsArgs(File tempFile) {
ResourceHelper resourceHelper = new ResourceHelper(log, spotbugsXmlOutputDirectory, resourceManager)
def args = new ArrayList<String>()
if(userPrefs) {
log.debug(" Adding User Preferences File -> ${userPrefs}" )
args << "-userPrefs"
args << resourceHelper.getResourceFile(userPrefs.trim())
}
args << "-xml:withMessages"
args << "-auxclasspathFromInput"
args << "-projectName"
args << "${project.name}"
args << getEffortParameter()
args << getThresholdParameter()
if (debug) {
log.debug("progress on")
args << "-progress"
}
if (pluginList || plugins) {
args << "-pluginList"
args << getSpotbugsPlugins()
}
if (visitors) {
args << "-visitors"
args << visitors
}
if (omitVisitors) {
args << "-omitVisitors"
args << omitVisitors
}
if (relaxed) {
args << "-relaxed"
}
if (nested) {
args << "-nested:true"
} else {
args << "-nested:false"
}
if (onlyAnalyze) {
args << "-onlyAnalyze"
args << onlyAnalyze
}
if (includeFilterFile) {
log.debug(" Adding Include Filter File ")
String[] includefilters = includeFilterFile.split(SpotBugsInfo.COMMA)
includefilters.each { includefilter ->
args << "-include"
args << resourceHelper.getResourceFile(includefilter.trim())
}
}
if (excludeFilterFile) {
log.debug(" Adding Exclude Filter File ")
String[] excludefilters = excludeFilterFile.split(SpotBugsInfo.COMMA)
excludefilters.each { excludeFilter ->
args << "-exclude"
args << resourceHelper.getResourceFile(excludeFilter.trim())
}
}
if (excludeBugsFile) {
log.debug(" Adding Exclude Bug Files (Baselines)")
String[] excludeFiles = excludeBugsFile.split(SpotBugsInfo.COMMA)
excludeFiles.each() { excludeFile ->
args << "-excludeBugs"
args << resourceHelper.getResourceFile(excludeFile.trim())
}
}
if (maxRank) {
args << "-maxRank"
args << maxRank
}
args << "-output"
args << tempFile.getAbsolutePath()
if (classFilesDirectory.exists() && classFilesDirectory.isDirectory()) {
log.debug(" Adding to Source Directory ->" + classFilesDirectory.absolutePath)
args << classFilesDirectory.absolutePath
}
if (testClassFilesDirectory.exists() && testClassFilesDirectory.isDirectory() && includeTests) {
log.debug(" Adding to Source Directory ->" + testClassFilesDirectory.absolutePath)
args << testClassFilesDirectory.absolutePath
}
return args
}
/**
* Get the Spotbugs AuxClasspath.
*
*/
private String getSpotbugsAuxClasspath() {
def auxClasspathElements
if (classFilesDirectory.exists() && classFilesDirectory.isDirectory()) {
auxClasspathElements = project.compileClasspathElements
}
if (testClassFilesDirectory.exists() && testClassFilesDirectory.isDirectory() && includeTests) {
auxClasspathElements = project.testClasspathElements
}
def auxClasspath = ""
if (auxClasspathElements) {
log.debug(" AuxClasspath Elements ->" + auxClasspathElements)
def auxClasspathList = auxClasspathElements.findAll { project.build.outputDirectory != it.toString() }
if (auxClasspathList.size() > 0) {
auxClasspath += File.pathSeparator
log.debug(" Last AuxClasspath is ->" + auxClasspathList[auxClasspathList.size() - 1])
auxClasspathList.each() { auxClasspathElement ->
log.debug(" Adding to AuxClasspath ->" + auxClasspathElement.toString())
auxClasspath += auxClasspathElement.toString() + File.pathSeparator
}
}
}
log.debug(" AuxClasspath is ->" + auxClasspath)
return auxClasspath
}
/**
* Set up and run the Spotbugs engine.
*
* @param locale
* the locale the report should be generated for
*
*/
private void executeSpotbugs(File outputFile) {
log.debug("****** SpotBugsMojo executeSpotbugs *******")
long startTime, duration
File tempFile = new File("${project.build.directory}/spotbugsTemp.xml")
if (tempFile.exists()) {
tempFile.delete()
}
tempFile.getParentFile().mkdirs()
tempFile.createNewFile()
outputEncoding = outputEncoding ?: 'UTF-8'
log.debug("****** Executing SpotBugsMojo *******")
resourceManager.addSearchPath(FileResourceLoader.ID, project.getFile().getParentFile().getAbsolutePath())
resourceManager.addSearchPath(SpotBugsInfo.URL, "")
resourceManager.setOutputDirectory(new File(project.getBuild().getDirectory()))
log.debug("resourceManager outputDirectory is ${resourceManager.outputDirectory}")
log.debug(" Plugin Artifacts to be added -> ${pluginArtifacts.toString()}")
log.debug("outputFile is " + outputFile.getCanonicalPath())
log.debug("output Directory is " + spotbugsXmlOutputDirectory.getAbsolutePath())
log.debug("Temp File is " + tempFile.getCanonicalPath())
def ant = new AntBuilder()