14

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?

Boris
  • 22,667
  • 16
  • 50
  • 71
  • It looks like SpotBugs approaches the issue from the opposite direction. You don't tell it what to ignore, you tell it what to look at: https://spotbugs.github.io/spotbugs-maven-plugin/usage.html#Specifying_which_classes_to_analyze – Devon_C_Miller Sep 15 '18 at 06:20

1 Answers1

17

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.

  • Place your <excludeFilterFile> in the root of your project.

skomisa
  • 16,436
  • 7
  • 61
  • 102
  • 1
    I tried the solution and it doesn't work. According to this SpotBugs [ticket](https://github.com/spotbugs/spotbugs/issues/694) `SourceFilter` does not work with the full file path. – Boris Sep 17 '18 at 08:38
  • @Boris Try `Package` instead of `Source`. It works. – sgnsajgon Apr 26 '19 at 19:30
  • @sgnsajgon I had mentioned `Package` in my answer, but how does that help the OP? Their question is asking how to exclude a specific directory, not a package. – skomisa Apr 28 '19 at 15:01
  • 1
    @Boris It looks like the implementation of open issue [Add management for source filter using full source path (if available). #903](https://github.com/spotbugs/spotbugs/pull/903) may be needed to resolve this. – skomisa Apr 28 '19 at 15:03
  • @skomisa `Application class loader` in JVM (the default one) searches for classes from given package in the corresponding directory structure. i.e class `com.example.app.Class` typically (in practice almost always) is placed in `com/example/app` directory. And vice versa, classes located in `com/example/app` directory typically have `com.example.app` package. So, effectively, excluding a package is usually the same as excluding directory. Of course, there are rare cases when we write or use custom ClassLoaders, but as far as I can see, most JVM applications use default class loading mechanism. – sgnsajgon Apr 28 '19 at 19:13
  • 3
    where would you place that ignore.xml? – DanDan Jul 18 '19 at 13:03
  • 1
    @DanDan and others, I just placed the include/exclude.xml files in the root of the Eclipse project, picked them up fine. – JGlass Jan 05 '23 at 21:41
  • 1
    @JGlass Thanks for that feedback - I just updated my answer accordingly. – skomisa Jan 06 '23 at 03:50