Exclusion Management

DomTrip provides a complete API for managing dependency exclusions in Maven POM files. Exclusions can be added, checked, and removed from both regular and managed dependencies.

Adding Exclusions

Add exclusions to dependencies using Coordinates to identify both the dependency and the exclusion:

String pom = """
        <?xml version="1.0" encoding="UTF-8"?>
        <project xmlns="http://maven.apache.org/POM/4.0.0">
          <modelVersion>4.0.0</modelVersion>
          <groupId>com.example</groupId>
          <artifactId>my-project</artifactId>
          <version>1.0.0</version>
          <dependencies>
            <dependency>
              <groupId>org.springframework</groupId>
              <artifactId>spring-core</artifactId>
              <version>6.0.9</version>
            </dependency>
          </dependencies>
        </project>
        """;

PomEditor editor = new PomEditor(Document.of(pom));
Coordinates springCore = Coordinates.of("org.springframework", "spring-core", "6.0.9");
Coordinates commonsLogging = Coordinates.of("commons-logging", "commons-logging", null);

// Add an exclusion to a dependency
editor.dependencies().addExclusion(springCore, commonsLogging);

// Check if an exclusion exists
boolean hasExcl = editor.dependencies().hasExclusion(springCore, commonsLogging);
assertTrue(hasExcl);

// Delete an exclusion (removes <exclusions> wrapper if empty)
editor.dependencies().deleteExclusion(springCore, commonsLogging);
assertFalse(editor.dependencies().hasExclusion(springCore, commonsLogging));

Managed Dependency Exclusions

The same operations are available for dependencies in <dependencyManagement>:

String pom = """
        <?xml version="1.0" encoding="UTF-8"?>
        <project xmlns="http://maven.apache.org/POM/4.0.0">
          <modelVersion>4.0.0</modelVersion>
          <groupId>com.example</groupId>
          <artifactId>my-project</artifactId>
          <version>1.0.0</version>
          <dependencyManagement>
            <dependencies>
              <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-core</artifactId>
                <version>6.0.9</version>
              </dependency>
            </dependencies>
          </dependencyManagement>
        </project>
        """;

PomEditor editor = new PomEditor(Document.of(pom));
Coordinates springCore = Coordinates.of("org.springframework", "spring-core", "6.0.9");
Coordinates commonsLogging = Coordinates.of("commons-logging", "commons-logging", null);

// Add exclusion to a managed dependency
editor.dependencies().addManagedExclusion(springCore, commonsLogging);

// Check if managed dependency has exclusion
boolean has = editor.dependencies().hasManagedExclusion(springCore, commonsLogging);
assertTrue(has);

// Delete exclusion from managed dependency
editor.dependencies().deleteManagedExclusion(springCore, commonsLogging);

API Reference

Regular Dependencies

Method Description
dependencies().addExclusion(dep, excl) Adds an exclusion; creates <exclusions> wrapper if absent
dependencies().deleteExclusion(dep, excl) Removes an exclusion; removes empty <exclusions> wrapper
dependencies().hasExclusion(dep, excl) Checks if a dependency has a specific exclusion

Managed Dependencies

Method Description
dependencies().addManagedExclusion(dep, excl) Adds an exclusion to a managed dependency
dependencies().deleteManagedExclusion(dep, excl) Removes an exclusion from a managed dependency
dependencies().hasManagedExclusion(dep, excl) Checks if a managed dependency has a specific exclusion

Notes

  • Dependencies are matched by groupId:artifactId (GA coordinates)
  • The version in the Coordinates is not used for matching exclusions
  • When the last exclusion is deleted, the <exclusions> wrapper element is automatically removed
  • Exclusion elements are ordered according to Maven conventions: <groupId> then <artifactId>

Next Steps