SpotBugs (successor of FindBugs) is a tool for static code analysis, similar like PMD. Both tools help to detect bad code constructs which might need improvement. As they partly detect different issues, they may be well combined and used simultaneously.
Step 1: Add Eclipse IDE Support
The SpotBugs Eclipse Plugin can be installed directly via the Eclipse Marketplace.
After installation projects can be configured to use it from the projects Properties context menu. Navigate to the SpotBugs category and enable all checkboxes on the main site. Further set Minimum rank to report to 20 and Minimum confidence to report to Low.
Once done SpotBugs immediately scans the project for problems. Found issues are displayed as custom markers in editors. Further they are visible in the Bug Explorer view as well as in the Problems view.
SpotBugs also comes with a label decoration on elements in the Package Explorer. If you do not like these then disable all Bug count decorator entries in Preferences/General/Appearance/Label Decorations.
Step 2: Maven Integration
Integration is done via the SpotBugs Maven Plugin. To enable, add following section to your master pom:
<properties> <maven.spotbugs.version>4.1.4</maven.spotbugs.version> </properties> <build> <plugins> <!-- enable spotbugs code analysis --> <plugin> <groupId>com.github.spotbugs</groupId> <artifactId>spotbugs-maven-plugin</artifactId> <version>${maven.spotbugs.version}</version> <configuration> <effort>Max</effort> <threshold>Low</threshold> <fork>false</fork> </configuration> <executions> <execution> <id>spotbugs-integration</id> <phase>verify</phase> <goals> <goal>spotbugs</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
The execution entry takes care that the spotbugs goal is automatically executed during the verify phase. If you remove the execution section you would have to call the spotbugs goal separately:
mvn spotbugs:spotbugs
Step 3: File Exclusions
You might have code that you do not want to get checked (eg generated files). Exclusions need to be defined in an xml file. A simple filter on package level looks like this:
<?xml version="1.0" encoding="UTF-8"?> <FindBugsFilter> <!-- skip EMF generated packages --> <Match> <Package name="~org\.eclipse\.skills\.model.*" /> </Match> </FindBugsFilter>
See the documentation for a full description of filter definitions.
Once defined this file can be used from the SpotBugs Eclipse plugin as well as from the maven setup.
To simplify the maven configuration we can add following profile to our master pom:
<profiles> <profile> <!-- apply filter when filter file exists --> <id>auto-spotbugs-exclude</id> <activation> <file> <exists>.settings/spotbugs-exclude.xml</exists> </file> </activation> <build> <plugins> <!-- enable spotbugs exclude filter --> <plugin> <groupId>com.github.spotbugs</groupId> <artifactId>spotbugs-maven-plugin</artifactId> <version>${maven.spotbugs.version}</version> <configuration> <excludeFilterFile>.settings/spotbugs-exclude.xml</excludeFilterFile> </configuration> </plugin> </plugins> </build> </profile> </profiles>
It gets automatically enabled when a file .settings/spotbugs-exclude.xml exists in the current project.
Step 4: Jenkins Integration
Like with PMD, we again use the warnings-ng plugin on Jenkins to track our findings:
recordIssues tools: [spotBugs(useRankAsPriority: true)]
Try out the live chart on the skills project.
Final Thoughts
PMD is smoother on integration as it stores its rulesets in a common file which can be shared by maven and the Eclipse plugin. SpotBugs currently requires to manage rulesets separately. Still both can be implemented in a way that users automatically get the same warnings in maven and the IDE.