Skip to content

Commit

Permalink
Support multiple bug exclusion files.
Browse files Browse the repository at this point in the history
Spotbugs always supported multiple files as a comma-separated list in the <excludeFilterFile> configuration option.

This change also adds support for multiple files as a list:

<excludeFilterFiles>
  <excludeFilterFile>...</excludeFilterFile>
  <excludeFilterFile>...</excludeFilterFile>
<excludeFilterFiles>

This allows using pom inheritance to control multiple files.

Includes integration test to verify function.
  • Loading branch information
hgschmie authored and hazendaz committed Jul 5, 2022
1 parent cd7fbd3 commit b3e0d8c
Show file tree
Hide file tree
Showing 4 changed files with 163 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/it/exclude-multi-list/invoker.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
invoker.goals = clean compile site

# The expected result of the build, possible values are "success" (default) and "failure"
invoker.buildResult = success
61 changes: 61 additions & 0 deletions src/it/exclude-multi-list/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (C) 2006-2020 the original author or authors.
Licensed 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
https://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.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>spotbugs-maven-plugin.it</groupId>
<artifactId>common</artifactId>
<version>testing</version>
<relativePath>../common.xml</relativePath>
</parent>
<artifactId>excludeFile-multi-list</artifactId>
<name>excludeFile-multi-list</name>
<packaging>jar</packaging>
<build>
<extensions>
<extension>
<groupId>spotbugs-maven-plugin.it</groupId>
<artifactId>build-tools</artifactId>
<version>testing</version>
</extension>
</extensions>
</build>
<reporting>
<excludeDefaults>true</excludeDefaults>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jxr-plugin</artifactId>
<version>@jxrPluginVersion@</version>
</plugin>
<plugin>
<groupId>com.github.spotbugs</groupId>
<artifactId>spotbugs-maven-plugin</artifactId>
<configuration>
<spotbugsXmlOutput>true</spotbugsXmlOutput>
<xmlOutput>true</xmlOutput>
<excludeFilterFiles>
<excludeFilterFile>whizbang/lib-filter.xml</excludeFilterFile>
<excludeFilterFile>filters/lib-filter2.xml</excludeFilterFile>
</excludeFilterFiles>
</configuration>
</plugin>
</plugins>
</reporting>
</project>
66 changes: 66 additions & 0 deletions src/it/exclude-multi-list/verify.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright (C) 2006-2020 the original author or authors.
*
* Licensed 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
*
* https://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.XmlSlurper

File spotbugsHtml = new File(basedir, 'target/site/spotbugs.html')
assert spotbugsHtml.exists()

File spotbugXdoc = new File(basedir, 'target/spotbugs.xml')
assert spotbugXdoc.exists()

File spotbugXml = new File(basedir, 'target/spotbugsXml.xml')
assert spotbugXml.exists()


println '***************************'
println "Checking HTML file"
println '***************************'

def xhtmlParser = new XmlSlurper();
xhtmlParser.setFeature("http://apache.org/xml/features/disallow-doctype-decl", false)
xhtmlParser.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false)
def path = xhtmlParser.parse( spotbugsHtml )
//*[@id="contentBox"]/div[2]/table/tbody/tr[2]/td[2]
def spotbugsErrors = path.body.'**'.find {div -> div.@id == 'contentBox'}.section[1].table.tr[1].td[1].toInteger()
println "Error Count is ${spotbugsErrors}"


println '**********************************'
println "Checking Spotbugs Native XML file"
println '**********************************'

path = new XmlSlurper().parse(spotbugXml)

allNodes = path.depthFirst().collect{ it }
def spotbugsXmlErrors = allNodes.findAll {it.name() == 'BugInstance'}.size()
println "BugInstance size is ${spotbugsXmlErrors}"


println '***************************'
println "Checking xDoc file"
println '***************************'

path = new XmlSlurper().parse(spotbugXdoc)

allNodes = path.depthFirst().collect{ it }
def xdocErrors = allNodes.findAll {it.name() == 'BugInstance'}.size()
println "BugInstance size is ${xdocErrors}"


assert xdocErrors == spotbugsXmlErrors

assert spotbugsErrors == spotbugsXmlErrors
32 changes: 32 additions & 0 deletions src/main/groovy/org/codehaus/mojo/spotbugs/SpotBugsMojo.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,28 @@ class SpotBugsMojo extends AbstractMavenReport implements SpotBugsPluginsTrait {
@Parameter(property = "spotbugs.excludeFilterFile")
String excludeFilterFile

/**
* <p>
* File name for exclude filter files. Bugs matching the filters are not reported.
* </p>
*
* <p>
* This is an alternative to <code>&lt;excludeFilterFile&gt;</code> which allows multiple
* files to be specified as separate elements in a pom.
* </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>
*
* @since 4.7.0.1-SNAPSHOT
*/
@Parameter(property = "spotbugs.excludeFilterFiles")
List excludeFilterFiles

/**
* <p>
* File names of the baseline files. Bugs found in the baseline files won't be reported.
Expand Down Expand Up @@ -961,6 +983,16 @@ class SpotBugsMojo extends AbstractMavenReport implements SpotBugsPluginsTrait {

}

if (excludeFilterFiles) {
log.debug(" Adding Exclude Filter Files ")

excludeFilterFiles.each { excludeFilter ->
args << "-exclude"
args << resourceHelper.getResourceFile(excludeFilter.trim())
}

}

if (excludeBugsFile) {
log.debug(" Adding Exclude Bug Files (Baselines)")
String[] excludeFiles = excludeBugsFile.split(SpotBugsInfo.COMMA)
Expand Down

0 comments on commit b3e0d8c

Please sign in to comment.