Findbugs Maven Plugin Fail Build If Problems Are Found

By executing a static code analysis every time your project is compiled/build, you can drastically reduce the amount of bugs that slip into your codebase. Well, if you try hard enough you still can but I mean, bugs that findbugs maven plugin can find. In the following example we show you how you can configure Maven Findbugs to fail your build when bugs are found.

Suppose we have the following class in our application. Can you spot the bug(s) ?

package com.memorynotfound;

public class ClassWithBug {

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

}

We can configure findbus to fail the build when a bug is found. Here is how:

  1. Add the findbugs-maven-plugin inside the plugins element of the build element.
  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.
  4. Add an execution which runs the plugin’s check goal during the Maven compile lifecycle phase.
<?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>fail-build</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>

    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>findbugs-maven-plugin</artifactId>
                <version>3.0.3</version>
                <configuration>
                    <effort>Max</effort>
                    <threshold>Low</threshold>
                    <failOnError>true</failOnError>
                </configuration>
                <executions>
                    <execution>
                        <id>analyze-compile</id>
                        <phase>compile</phase>
                        <goals>
                            <goal>check</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>
We need to tell maven to execute the findbugs on maven compile phase. We can override the build outcome by registering the failOnError element in the configuration element.

Using the following maven command we can compile our project.

mvn clean compile

The previous command generates the following output.

[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building MAVEN FINDBUGS - fail-build 1.0.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[...] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ fail-build ---
[...]
[INFO] >>> findbugs-maven-plugin:3.0.3:check (analyze-compile) > :findbugs @ fail-build >>>
[INFO] 
[INFO] --- findbugs-maven-plugin:3.0.3:findbugs (findbugs) @ fail-build ---
[INFO] Fork Value is true
     [java] Warnings generated: 3
[INFO] Done FindBugs Analysis....
[INFO] 
[INFO] <<< findbugs-maven-plugin:3.0.3:check (analyze-compile) < :findbugs @ fail-build <<<
[INFO] 
[INFO] --- findbugs-maven-plugin:3.0.3:check (analyze-compile) @ fail-build ---
[INFO] BugInstance size is 3
[INFO] Error size is 0
[INFO] Total bugs: 3
[INFO] Use of non-localized String.toUpperCase() or String.toLowerCase() in com.memorynotfound.Program.toUpper() [com.memorynotfound.Program] At Program.java:[line 7] DM_CONVERT_CASE
[INFO] Null pointer dereference of str in com.memorynotfound.Program.toUpper() [com.memorynotfound.Program] Dereferenced at Program.java:[line 7] NP_ALWAYS_NULL
[INFO] Load of known null value in com.memorynotfound.Program.toUpper() [com.memorynotfound.Program] At Program.java:[line 7] NP_LOAD_OF_KNOWN_NULL_VALUE
[...] 
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------

You may also like...