Generate a Bug Report Maven FindBugs Plugin

When we don’t want to run static code analysis every time our project is compiled, we can generate a bug report during the site phase of the project. This way we can manually generate a bug report for our project. Or create a periodical report using a CI tool.


We are using the following class to demonstrate a simple use case how maven findbugs plugin generates a bug report. Can you spot the bugs in the following code?

package com.memorynotfound;

public class ClassWithBug {

    public String execute() {
        String str = null;
        return str.toUpperCase();
    }

}

We can generate a bug report using the maven findbugs plugin by following these steps:

  1. Add the findbugs-maven-plugin to the reporting section of the pom.xml file.
  2. Make sure that most accurate analysis is performed by setting the efford element to Max.
  3. Make sure that all bugs are reported by setting the efford element to low.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
                             http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <groupId>com.memorynotfound.maven.plugins.findbugs</groupId>
    <artifactId>create-report-part-of-project-reports</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <name>MAVEN FINDBUGS - ${project.artifactId}</name>
    <url>http://memorynotfound.com</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    </properties>

    <reporting>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>findbugs-maven-plugin</artifactId>
                <version>3.0.3</version>
                <configuration>
                    <effort>Max</effort>
                    <threshold>Low</threshold>
                </configuration>
            </plugin>
        </plugins>
    </reporting>

</project>

We can generate the bug report using the following maven command:

mvn compile site

When the maven command is executed it generates the following output.

[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building MAVEN FINDBUGS - create-report-part-of-project-reports 1.0.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[...] 
[INFO] --- maven-site-plugin:3.3:site (default-site) @ create-report-part-of-project-reports ---
[INFO] configuring report plugin org.codehaus.mojo:findbugs-maven-plugin:3.0.3
[INFO] Fork Value is true
     [java] Warnings generated: 3
[INFO] Done FindBugs Analysis....
[...] 
[INFO] Generating "FindBugs" report    --- findbugs-maven-plugin:3.0.3
[...] 
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------

Demo Bug Report

When the build is successful, maven has generated a bug report in the target/site folder. The following screenshot is a result of the generated bug report of the findbugs-maven-plugin.

generate bug report maven findbugs plugin

You may also like...