Profile-Scoped Operations

DomTrip supports scoping dependency operations to specific Maven profiles. This allows you to manage profile-specific dependencies independently from top-level project dependencies.

Basic Usage

Use forProfile() to get a Dependencies instance scoped to a specific profile:

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.slf4j</groupId>
              <artifactId>slf4j-api</artifactId>
              <version>2.0.7</version>
            </dependency>
          </dependencies>
          <profiles>
            <profile>
              <id>integration-tests</id>
              <dependencies>
                <dependency>
                  <groupId>org.testcontainers</groupId>
                  <artifactId>testcontainers</artifactId>
                  <version>1.19.0</version>
                  <scope>test</scope>
                </dependency>
              </dependencies>
            </profile>
          </profiles>
        </project>
        """;

PomEditor editor = new PomEditor(Document.of(pom));

// Scope operations to a specific profile
PomEditor.Dependencies profileDeps = editor.dependencies().forProfile("integration-tests");

// Update a dependency within the profile
Coordinates testcontainers = Coordinates.of("org.testcontainers", "testcontainers", "1.20.0");
profileDeps.updateDependency(false, testcontainers);

// Add a new dependency to the profile
Coordinates wiremock = Coordinates.of("com.github.tomakehurst", "wiremock-jre8", "2.35.0");
profileDeps.updateDependency(true, wiremock);

// Profile-scoped operations don't affect top-level dependencies
// editor.dependencies() still targets project/dependencies

How It Works

The forProfile() method returns a new Dependencies instance where all operations resolve relative to the <profile> element instead of the project root:

Operation Top-level Profile-scoped
addDependency() project/dependencies project/profiles/profile[id=X]/dependencies
updateDependency() project/dependencies project/profiles/profile[id=X]/dependencies
deleteDependency() project/dependencies project/profiles/profile[id=X]/dependencies
addAligned() project/dependencies project/profiles/profile[id=X]/dependencies
addExclusion() project/dependencies project/profiles/profile[id=X]/dependencies

API Reference

Scoping Methods

Method Description
dependencies().forProfile(String profileId) Scope to a profile by its <id> value
dependencies().forProfile(Element profileElement) Scope to a pre-resolved <profile> element

Finding Profiles

// Check if a profile exists
boolean exists = editor.profiles().hasProfile("integration-tests");

// Find a profile element
Element profile = editor.profiles().findProfile("integration-tests");
if (profile != null) {
    editor.dependencies().forProfile(profile).addAligned(coords);
}

Notes

  • forProfile(String) throws DomTripException if the profile is not found
  • The profile-scoped Dependencies instance supports all the same operations as the top-level one: CRUD, exclusions, alignment, and convention detection
  • Profile-scoped operations do not affect top-level dependencies and vice versa

Next Steps