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=""

    <name>MAVEN FINDBUGS - ${project.artifactId}</name>



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] 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] --- findbugs-maven-plugin:3.0.3:findbugs (findbugs) @ fail-build ---
[INFO] Fork Value is true
     [java] Warnings generated: 3
[INFO] Done FindBugs Analysis....
[INFO] <<< findbugs-maven-plugin:3.0.3:check (analyze-compile) < :findbugs @ fail-build <<<
[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[line 7] DM_CONVERT_CASE
[INFO] Null pointer dereference of str in com.memorynotfound.Program.toUpper() [com.memorynotfound.Program] Dereferenced at[line 7] NP_ALWAYS_NULL
[INFO] Load of known null value in com.memorynotfound.Program.toUpper() [com.memorynotfound.Program] At[line 7] NP_LOAD_OF_KNOWN_NULL_VALUE
[INFO] ------------------------------------------------------------------------
[INFO] ------------------------------------------------------------------------

You may also like...