Improve code quality through static code analysis
Use SpotBugs to detect bad pratice and bugs into your Java project
Using static analysis tools to detect bugs or bad pratice at early stage in the development process is greatly benefits for the code quality of the project. Moreover, these tools allow to find and correct many errors in a cheaper way before they appear at runtime.
Static analyzers have a knowledge base on various patterns that in certain conditions cause an error and can provide feedbacks to the developper, the existence of which he himself would hardly have guessed.
SpotBugs the successor of FindBugs which is not longer active since few years now is a static analysis tools that checks for more than 400 bug patterns (bug descriptions can be found here). SpotBugs can be easily integrated into your build process since it support Ant, Gradle, Maven.
SpotBugs gradle plugin task
To integrate SpotBugs into your Gradle build script, you only have to add the following snippet into your build.gradle
as described on the Gradle plugin website :
|
|
The plugins add 2 tasks to run SpotBugs analysis:
- spotbugsMain: runs SpotBugs for your production Java source files and also test Java source files since this task depends on the following.
- spotbugsTest: runs SpotBugs for your test Java source files.
You can also simply use the check
task that depends on both previous tasks:
|
|
Configure SpotBugs gradle plugin task
The SpotBugs configuration is defined in a similar way that it was did for FindBugs, thus you can use all properties defined for the FindBugs configuration, excepted that you must define them into a spotbugs
item rather than into a findbugs
.
Configure which report, xml or html will be generated by using the following snippet:
|
|
❗ Note that you cannot generate both in the same time.
Generated html report uses the language of your system, if it is not suitable, you can select the report language by setting the property user.language
when the SpotBugs analysis is launched. For instance, to generate a report in english, use the command:
|
|
Others configurations will be define into the spotbugs
item as below:
|
|
See meaning of all properties on the FindBugs gradle plugin website.
Sample of SpotBugs report
I used the following Java class that contains several problems as input of the SpotBugs analysis:
|
|
SpotBugs generates a report with 8 warnings: 2 with high priorities and 6 with medium priorities. It is possible to filter reported warnings by using its priority level and setting the property reportLevel
to one of the value low, medium, high
.
Strategy to enable SpotBugs into a Java project
Enable SpotBugs support at the beginning of the project to report all detected failures is a good idead, nevertheless, adding the support into a existing project with a lot of legacy code can be paintful. Indeed, it could reports a lot of warnings and it can be a nightmare to manage them.
As saw previously, you can use the property reportLevel
to report only warnings with a high priority and decrease warnings to take into account. If it is again too much, you can start by working on warnings that the team estimate the most useful and filter the report to only show those. It can be done by setting the property includeFilter
with a path to a filter file such as below, for instance, to report only warnings related to method with Boolean
return type returning explicit null
.
|
|
To have more information on how to write filter file, see the official documentation.
Rather than enable bug patterns one-by-one, the counterside approach can be choose to disable all bug patterns that are considered less valuable by the team. In this case, use the property excludeFilter
.
Publish SpotBugs report into Jenkins
SpotBugs can be easily integrated into Jenkins, one of the leading open-source automation servers. Use it through docker with the following command line:
|
|
Now install the Warnings plugin by going to Manage Jenkins/Manage Plugins
, into available plugins select Warnings Next Generation
and install it without restart.
In order to add SpotBugs analysis into your build job, go to the project and select Configure
, into the Build
section, add a build step that will invoke gradle script and set the tasks to check
. To finish, into the Post-build Actions
, add a step to Record compiler warnings and static analysis results
to publish SpotBugs results and set the Report File Pattern
to **/build/reports/spotbugs/main.xml
which represent the path where the SpotBugs report is generated.
❗ Do not forget to configure the tool to generate the XML report as describe previously.