# Add SpotBugs to a Maven Build

Yes. An agent adds the SpotBugs Maven plugin with a threshold, an exclusion file and SARIF upload to CI, runs it, and opens a PR that lists every finding.

[Run this on Rush](https://cloud.getrush.ai/run/add-spotbugs/maven/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

The agent reads the build before it adds a static analyser, because a Maven project with several validate-phase checks has an order and a set of conventions a new plugin must respect. In a service laid out like spring-projects/spring-petclinic, `pom.xml` inherits from `spring-boot-starter-parent` 4.1.0, targets Java 17, and already runs the enforcer plugin for the Java version, `spring-javaformat-maven-plugin` 0.0.47 at `validate`, `maven-checkstyle-plugin` 3.6.0 with Checkstyle 12.3.1 and the `nohttp` rules (Checkstyle is held at 12 because 13 needs JDK 21, as a comment in the properties says), JaCoCo 0.8.15 with a report at `prepare-package`, the Spring Boot plugin's `build-info`, `git-commit-id` and CycloneDX. The wrapper in `.mvn/wrapper/maven-wrapper.properties` is Maven 3.9.16 with `distributionType=only-script`. `.github/workflows/maven-build.yml` runs `./mvnw -B verify` on Java 17 with the `adopt` distribution and Maven caching; a second workflow builds with Gradle from `build.gradle`, which has the same plugins declared again. Among the tests, `MySqlIntegrationTests` and `PostgresIntegrationTests` use Testcontainers and disable themselves without Docker.

The agent adds `com.github.spotbugs:spotbugs-maven-plugin`, current release 4.10.5.0, to the `plugins` block with a `spotbugs.version` property beside the others. Its configuration sets `effort` to `Max`, `threshold` to `Medium`, `failOnError` to true, `xmlOutput` for the report and `sarifOutput` so the findings can be uploaded, and an `excludeFilterFile` of `src/spotbugs/spotbugs-exclude.xml`, placed next to the existing `src/checkstyle/` directory. The exclusion file starts with the patterns that are noise in a Spring Data application rather than bugs: `EI_EXPOSE_REP` and `EI_EXPOSE_REP2` on the JPA entities under `model`, `owner` and `vet`, whose getters return the mutable collections JPA manages, each with a comment that says why. An execution binds the `check` goal to `verify`, after the tests, so a finding fails the same command CI runs.

`maven-build.yml` gains a step after the build that uploads `target/spotbugsSarif.json` through `github/codeql-action/upload-sarif` with `security-events: write` on the job, so each finding appears on the pull request's Security tab with its class and line, and the XML report is kept as an artifact on every outcome. The Gradle build gets the equivalent `com.github.spotbugs` plugin in `build.gradle` with the same threshold and the same exclusion file, so the two workflows agree on what passes; the pull request says so, and a team that only builds with Maven can drop that hunk.

To check it, the agent runs `./mvnw -B verify` in its sandbox, where the Docker-dependent integration tests skip themselves, reads `target/spotbugsXml.xml`, and lists in the pull request every finding above the threshold with its pattern, class and line, without adding any of them to the exclusion file: on a codebase this size the list is expected to be short, and whether each is fixed or suppressed with a reason is a reviewer's call. It runs `./mvnw spring-javaformat:validate` and the Checkstyle goal to show the existing checks still pass, and `./gradlew spotbugsMain` for the Gradle side.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "Add SpotBugs to this Maven build. Read pom.xml, .mvn/wrapper/maven-wrapper.properties, .github/workflows/maven-build.yml, .github/workflows/gradle-build.yml, build.gradle, README.md and the test classes that use Testcontainers first. Add the spotbugs-maven-plugin with a version property, effort Max, threshold Medium, failOnError, XML and SARIF output, and an exclusion file under src/spotbugs that starts with the entity getter patterns Spring Data makes noisy, each with a comment, and bind the check goal to verify. Add a workflow step that uploads the SARIF to code scanning with security-events write on that job and keeps the XML report as an artifact on every outcome. Mirror the plugin in build.gradle with the same threshold and exclusion file. Run ./mvnw -B verify in your sandbox, list every finding above the threshold with pattern, class and line without suppressing any, run the format and Checkstyle checks and the Gradle SpotBugs task, and open a pull request with the output.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
```

1. **Get an API key.** In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
2. **Send the request.** POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
3. **Read what came back.** Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

## What comes back

The pull request changes `pom.xml`, `build.gradle` and `.github/workflows/maven-build.yml`, and adds `src/spotbugs/spotbugs-exclude.xml`. Its description lists the findings from the sandbox run by pattern, each with the class and line, the `verify` output including which integration tests skipped and why, the format and Checkstyle results, and the Gradle task's result.

The `Java CI with Maven` workflow runs on the pull request with the SpotBugs check inside `verify`, the SARIF findings on the Security tab and the XML report attached to the run. The Gradle workflow runs the mirrored task. No Java source changes; the dependencies, the tests and the Flyway scripts under `src/main/resources/db` are as they were.

Before merging, a reviewer goes through the list and decides for each finding between a fix, a suppression with a reason in the exclusion file, or a higher threshold, and decides whether the first merge should fail the build or only report, which is one `failOnError` flip.

## Run it on every Maven service without static analysis

A team with several Maven services sends this body once per repository to `POST /o/{org}/sessions`, each with its own repository in `resources` and a `request_id` that is a UUID v5 of the repository's full name, so a retried call returns the run already started instead of opening a second pull request. Each run reads its own build: a multi-module project gets the plugin in the parent's `pluginManagement` and the check in each module, one without a Gradle build skips that hunk, and one that already runs `verify` on a Java matrix gets the upload step once with the SARIF from one matrix entry.

Once merged, every push runs the check, so no further run is needed to keep the report current. A `webhook_url` on the key reports each `run.completed` with the pull request in `result`, and a `run.failed` names a build the plugin could not run on, for instance one on a Java version SpotBugs no longer reads.

## Why run it on Rush?

The plugin is a few lines, but a first rollout is a list of judgments: where in the lifecycle to bind it, which patterns are noise for this framework, how to get the findings in front of reviewers, and what to do with the first report. The agent reads the existing checks and the test setup, wires the plugin in its own sandbox, runs the whole build, and brings the findings back as a list with lines rather than a red CI.

The build output, the findings and the other checks' results stay with the run and are pasted into the pull request, so the review is a short list of bug patterns with class names rather than a plugin to try. Several services can get the same analysis at once, each with its own exclusion file.

## Frequently asked questions

### Why exclude the entity getters up front?

`EI_EXPOSE_REP` fires on every getter that returns a mutable collection, and JPA entities do that by design so the persistence context can track changes. Reporting them would bury the findings that matter. Each exclusion carries a comment, and the pattern is scoped to the entity packages, not the whole project.

### Will the integration tests run in the sandbox?

The two Testcontainers tests check for Docker and skip without it, so `verify` passes in the sandbox with those two skipped, and the pull request says which ones. The workflow's runner has Docker, so they run there as before.

### Does this replace Checkstyle or the format check?

No. Checkstyle and Spring Java Format judge source style at `validate`; SpotBugs reads compiled bytecode for bug patterns after the tests. They catch different things, and the pull request leaves both existing plugins untouched.

### Can I add security rules too?

Yes. The Find Security Bugs detector plugs into the same plugin through its `plugins` block and adds patterns for injection, weak crypto and the like. The pull request mentions it as a follow-up so the first review is about the base rules.

## More Maven jobs

- [Add Spotless Formatting to a Maven Build](https://getrush.ai/run/add-spotless-formatting/maven/)
- [Fix "Source option 5 is no longer supported. Use 7 or later." in Maven](https://getrush.ai/run/fix-source-option-5-no-longer-supported/maven/)
- [Migrate a Jenkins Pipeline to GitHub Actions for a Maven Project](https://getrush.ai/run/migrate-jenkins-to-github-actions/maven/)

## The same job on other stacks

- [Add SpotBugs to a Gradle Build](https://getrush.ai/run/add-spotbugs/gradle/)

## More jobs like this: Hand off a long task

- [Add SQLFluff Linting to a dbt Project](https://getrush.ai/run/add-sqlfluff-linting/dbt/)
- [Prefetch tRPC Queries on the Server in a Next.js Pages Router App](https://getrush.ai/run/add-ssr-prefetching/trpc/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
