Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SpotBugs Maven Plugin exclude a directory

I use SpotBugs Maven Plugin for a static analysis and I would like to exclude a directory from the inspection. Looking at the spotbugs:check goal documentation, it seems that it is not possible to configure the plugin is such a way. I also checked documentation for a SpotBugs filter file.

In Apache Maven PMD Plugin this can be done by using excludeRoots parameter:

<excludeRoots>
  <excludeRoot>target</excludeRoot>
</excludeRoots>

Is it possible to exclude a directory from SpotBugs inspection?

like image 897
Boris Avatar asked Sep 14 '18 17:09

Boris


People also ask

How do you run SpotBugs locally?

If you are running SpotBugs on a Windows system, double-click on the file %SPOTBUGS_HOME%\lib\spotbugs. jar to start the SpotBugs GUI. On a Unix, Linux, or macOS system, run the $SPOTBUGS_HOME/bin/spotbugs script, or run the command java -jar $SPOTBUGS_HOME/lib/spotbugs. jar to run the SpotBugs GUI.

How do I create a report from SpotBugs?

To generate the SpotBugs report as part of the Project Reports, add the SpotBugs plugin in the <reporting> section of your pom. xml . Then, execute the site plugin to generate the report.

What is SpotBugs plugin?

SpotBugs is a program to find bugs in Java programs. It looks for instances of “bug patterns” — code instances that are likely to be errors.


1 Answers

It is possible to exclude a directory from inspection with SpotBugs, though the approach is different to the one you described for PMD. It is a two step process:

  1. First create an XML filter file specifying the criteria for the directory(s) to be excluded.

  2. Then, in pom.xml refer to that that file using the optional <excludeFilterFile> setting. Unfortunately, the documentation for that setting is very brief.

As a simple example:

  1. Create a filter file named ignore.xml containing the following which refers to a directory named mydir:

    <?xml version="1.0" encoding="UTF-8"?>
    <FindBugsFilter>
        <Match>
            <Source name="~mydir\..*"/>
        </Match>
    </FindBugsFilter>
    

    The documentation for the <Source> tag is here. See the section on Java element name matching for details on how to specify the name of the <Source>.

  2. Then in pom.xml, in the specification for spotbugs-maven-plugin, include an <excludeFilterFile> tag so that mydir is ignored by SpotBugs:

    <configuration>
      <excludeFilterFile>ignore.xml</excludeFilterFile>
    </configuration>
    

Notes:

  • There is also an <includeFilterFile> tag. See the section titled Specifying which bug filters to run in the usage documentation.

  • As well as Source, SpotBugs provides several other ways to specify what code is to be included or excluded from checking. See the filter file documentation for the Package, Class, Method, Local, Field and Type tags.

like image 159
skomisa Avatar answered Sep 17 '22 11:09

skomisa