Safeguard
Developer Tools•

How do I exclude a vulnerable transitive dependency in Maven?

Two ways to deal with a vulnerable transitive library in Maven: exclusions to remove it, or dependencyManagement to pin a fixed version. When to use each.

Safeguard Security Team
3 min read

Add an exclusion inside the dependency that brings the library in, then declare a fixed version yourself if the code still needs it. Or skip the exclusion and pin a patched version with dependencyManagement. Exclude to remove a library you do not need. Pin to keep one you do.

Definitions

A transitive dependency is one you did not declare but received because a library you declared needs it. An exclusion tells Maven to drop a named artifact from that path. DependencyManagement sets the version Maven uses whenever the artifact appears anywhere in the tree.

Option 1: exclude it

The Maven guide puts it plainly: if project X depends on Y, and Y depends on Z, the owner of X can explicitly exclude Z.

<dependency>
  <groupId>group-a</groupId>
  <artifactId>artifact-a</artifactId>
  <version>1.0</version>
  <exclusions>
    <exclusion>
      <groupId>group-c</groupId>
      <artifactId>excluded-artifact</artifactId>
    </exclusion>
  </exclusions>
</dependency>

The exclusion lives inside the dependency that carries the unwanted artifact. If three of your dependencies pull it in, you may need it in all three.

The risk is that the code path using the library still runs. You then get a NoClassDefFoundError at runtime, not at build time. Run the tests that exercise the feature before you ship.

Option 2: pin a fixed version

When you still need the library, set its version once:

<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>jaxen</groupId>
      <artifactId>jaxen</artifactId>
      <version>2.0.0</version>
    </dependency>
  </dependencies>
</dependencyManagement>

The Enforcer documentation notes that this applies to all transitive dependencies with the same groupId, artifactId and classifier. That is why it beats the default rule where the nearest definition wins, covered in Maven dependency mediation.

Decision table

SituationUse
Library is unused in your code pathExclusion
Library is needed and a fixed version existsdependencyManagement pin
Many modules share the fixPin in a parent POM or BOM, see Maven BOMs
No fixed version existsExclude if possible, otherwise document and monitor

Steps

  1. Run mvn dependency:tree -Dincludes=group:artifact to find every path to the library.
  2. Choose exclusion or pin from the table.
  3. Rebuild and rerun the tree to confirm the resolved version.
  4. Add a comment naming the advisory so nobody removes the pin later.
  5. Rescan.

Try it yourself

Click through it at your own pace, or open it full screen.

Look for which findings already have a fixed version listed. Those are the ones a pin can solve today.

Where Safeguard fits

Safeguard's dependency scanning shows which findings have a fix available, and autonomous remediation can open fix pull requests. That feature is off by default and every category starts on Manual. Run a free scan on a repository to see your own transitive paths.

Quick check

Does an exclusion work for every path?

No. It applies only on the dependency where you declare it. Other dependencies that bring in the same library need their own exclusion, or use a pin instead.

Will the build fail if I exclude something I need?

Usually not. The failure tends to appear at runtime when the missing class is loaded, so test the affected feature.

Sources

Last reviewed: 19 October 2026.

Never miss an update

Weekly insights on software supply chain security, delivered to your inbox.

Self-healing security runs on Safeguard.

Your first fix PR is minutes away.

No sales call required, even your agent can complete the purchase over MCP.