Suppress FindBugs Warnings in a Java and Spring Boot Web Application using Gradle
How to Suppress FindBugs Warnings using Annotations in a +Spring Boot and +Java Application
If your build is breaking because of a FindBugs issue and it is a false-positive or you are unable to resolve the issue because of other considerations, you can add an Annotation to ignore the Findbugs warning.
Update your Gradle Dependencies
You will want to add the following compile time dependency to your build.gradle file.
compile group: ‘findbugs’, name: ‘findbugs’, version: ‘1.0.0’
dependencies { compile group: 'findbugs', name: 'findbugs', version: '1.0.0' }
Get the Findbugs Issue ID
You will need a specific ALL_CAPS identifier so that FindBugs knows what bug to ignore.
Locate the FindBugs Report
In your build message, you will see a link to the findbugs report:
You can also find the report in your build artifact. Right-click and open in your preferred browser to view file.
Open the FindBugs Report
Once you have it, open the Findbugs HTML report in a browser. It should look like the following.
Get the FindBugs identifier
Copy and paste the FindBugs identifier. It should be in ALL_CAPS.
You will use the annotation like this:
@SuppressWarnings(“OUT_OF_RANGE_ARRAY_INDEX”)
Add the Annotation to Your Code
Be sure that the correct findbugs SuppressWarnings annotation is being used.
import edu.umd.cs.findbugs.annotations.SuppressWarnings;
Add Suppress warnings annotation
Add this Suppress warnings annotation above the offending line of code. There are multiple versions of this annotation.
Be sure to use the specific FindBugs issue ID as a parameter in the annotation.
Be sure to use the annotation specific to findbugs in the package edu.umd.cs.findbugs.annotations.
This is the annotation you will want to add above the offending line of code.
@SuppressWarnings("OUT_OF_RANGE_ARRAY_INDEX")
Re-run the Build to Verify that the Warning is being Suppressed
You want to ensure that the warning is being ignored correctly. You may want to add a TODO as well if you intend to fix the issue at a later point as well or at least track the issues you are suppressing in some way.
Your build should run successfully without issuesmoving forward now.
Further info:
- http://findbugs.sourceforge.net/manual/annotations.html
- http://findbugs.sourceforge.net/api/edu/umd/cs/findbugs/annotations/package-summary.html
Reference: | Suppress FindBugs Warnings in a Java and Spring Boot Web Application using Gradle from our JCG partner Chris Anatalio at the Coding, Development, Solutions, Other Stuff blog. |