Skip to content

Instantly share code, notes, and snippets.

@natritmeyer
Created November 11, 2025 17:30
Show Gist options
  • Select an option

  • Save natritmeyer/04d891edb96687e995398e507223c994 to your computer and use it in GitHub Desktop.

Select an option

Save natritmeyer/04d891edb96687e995398e507223c994 to your computer and use it in GitHub Desktop.
Using SpotBugs on a test-only maven project

SpotBugs on a Test-Only Maven Project

This Gist documents the minimum required for the two ways to integrate SpotBugs on a test-only project:

Integrating SpotBugs in the build

The following plugin config will run spotbugs against your test code, and fail the build if it finds anything. Run it as follows:

mvn clean test

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://maven.apache.org/POM/4.0.0"
         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.example</groupId>
  <artifactId>spotbugs-test-only</artifactId>
  <version>0.0.1</version>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.13.2</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>com.github.spotbugs</groupId>
        <artifactId>spotbugs-maven-plugin</artifactId>
        <version>4.9.8.1</version>
        <configuration>
          <includeTests>true</includeTests>
        </configuration>
        <executions>
          <execution>
            <phase>test</phase>
            <goals>
              <goal>check</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

Integrating SpotBugs into project report generation

The following plugin config will generate a spotbugs report against your test code. Note that it requires compiled test code which is generated not in the compile phase but the test-compile phase that follows it. The test-compile phase is part of the test phase, so to generate the spotbugs report execute either of the following:

mvn clean test site (which will generate the report after compiling and executing your tests)

...or...

mvn clean test-compile site (which will generate the report after compiling but not executing your tests)

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://maven.apache.org/POM/4.0.0"
         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.example</groupId>
  <artifactId>spotbugs-test-only</artifactId>
  <version>0.0.1</version>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.13.2</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-site-plugin</artifactId>
        <version>3.21.0</version>
      </plugin>
    </plugins>
  </build>

  <reporting>
    <plugins>
      <plugin>
        <groupId>com.github.spotbugs</groupId>
        <artifactId>spotbugs-maven-plugin</artifactId>
        <version>4.9.8.1</version>
        <configuration>
          <includeTests>true</includeTests>
        </configuration>
      </plugin>
    </plugins>
  </reporting>
</project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment