Maven Examples
Practical examples of using the DomTrip Maven extension for common POM editing tasks.
Creating a POM from Scratch
Create a complete Maven project POM with Maven-aware element ordering:
// Create a new POM with Maven-aware ordering
PomEditor editor = new PomEditor();
editor.createMavenDocument("project");
Element root = editor.root();
// Add elements - they'll be automatically ordered
editor.insertMavenElement(root, MODEL_VERSION, "4.0.0");
editor.insertMavenElement(root, GROUP_ID, "com.example");
editor.insertMavenElement(root, ARTIFACT_ID, "my-project");
editor.insertMavenElement(root, VERSION, "1.0.0");
editor.insertMavenElement(root, NAME, "My Project");
String result = editor.toXml();
Adding Dependencies
Add dependencies with proper structure and ordering:
PomEditor editor = new PomEditor();
editor.createMavenDocument("project");
Element root = editor.root();
// Add dependencies with proper structure
Element dependencies = editor.insertMavenElement(root, DEPENDENCIES);
editor.dependencies().addDependency(dependencies, "org.junit.jupiter", "junit-jupiter", "5.9.2");
// Add scope to the dependency
Element junitDep = editor.findChildElement(dependencies, DEPENDENCY);
editor.insertMavenElement(junitDep, SCOPE, "test");
Adding Plugins
Configure build plugins with nested configuration:
PomEditor editor = new PomEditor();
editor.createMavenDocument("project");
Element root = editor.root();
// Add build plugins with configuration
Element build = editor.insertMavenElement(root, BUILD);
Element plugins = editor.insertMavenElement(build, PLUGINS);
Element compilerPlugin =
editor.plugins().addPlugin(plugins, "org.apache.maven.plugins", "maven-compiler-plugin", "3.11.0");
Element config = editor.insertMavenElement(compilerPlugin, CONFIGURATION);
editor.addElement(config, "source", "17");
editor.addElement(config, "target", "17");
Multi-Module Projects
Create a parent POM with module declarations:
// Create parent POM
PomEditor parentEditor = new PomEditor();
parentEditor.createMavenDocument("project");
Element parentRoot = parentEditor.root();
// Set up parent project
parentEditor.insertMavenElement(parentRoot, MODEL_VERSION, "4.0.0");
parentEditor.insertMavenElement(parentRoot, GROUP_ID, "com.example");
parentEditor.insertMavenElement(parentRoot, ARTIFACT_ID, "parent-project");
parentEditor.insertMavenElement(parentRoot, VERSION, "1.0.0");
parentEditor.insertMavenElement(parentRoot, PACKAGING, "pom");
// Add modules
Element modules = parentEditor.insertMavenElement(parentRoot, MODULES);
parentEditor.subprojects().addModule(modules, "core");
parentEditor.subprojects().addModule(modules, "web");
parentEditor.subprojects().addModule(modules, "cli");
String parentPom = parentEditor.toXml();
Spring Boot Project
Create a Spring Boot project POM with parent, starters, and plugins:
PomEditor editor = new PomEditor();
editor.createMavenDocument("project");
Element root = editor.root();
// Basic project info
editor.insertMavenElement(root, MODEL_VERSION, "4.0.0");
// Spring Boot parent
Coordinates bootParent = Coordinates.of("org.springframework.boot", "spring-boot-starter-parent", "3.2.0");
editor.parent().setParent(bootParent);
editor.insertMavenElement(root, GROUP_ID, "com.example");
editor.insertMavenElement(root, ARTIFACT_ID, "spring-boot-app");
editor.insertMavenElement(root, VERSION, "1.0.0");
editor.insertMavenElement(root, NAME, "Spring Boot Application");
// Properties
Element properties = editor.insertMavenElement(root, PROPERTIES);
editor.properties().addProperty(properties, "java.version", "17");
// Dependencies (versions managed by parent)
Element deps = editor.insertMavenElement(root, DEPENDENCIES);
editor.dependencies().addDependency(deps, "org.springframework.boot", "spring-boot-starter-web", null);
editor.dependencies().addDependency(deps, "org.springframework.boot", "spring-boot-starter-data-jpa", null);
Element testDep =
editor.dependencies().addDependency(deps, "org.springframework.boot", "spring-boot-starter-test", null);
editor.insertMavenElement(testDep, SCOPE, "test");
// Build
Element build = editor.insertMavenElement(root, BUILD);
Element plugins = editor.insertMavenElement(build, PLUGINS);
editor.plugins().addPlugin(plugins, "org.springframework.boot", "spring-boot-maven-plugin", null);
String result = editor.toXml();
POM Transformation
Transform an existing POM by adding metadata, properties, dependencies, and plugins:
String existingPom = """
<?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>legacy-project</artifactId>
<version>1.0.0</version>
</project>
""";
PomEditor editor = new PomEditor(Document.of(existingPom));
Element root = editor.root();
// Add missing metadata
if (!editor.hasChildElement(root, NAME)) {
editor.insertMavenElement(root, NAME, "Upgraded Project");
}
// Add or update properties
editor.properties().updateProperty(true, "maven.compiler.source", "17");
editor.properties().updateProperty(true, "maven.compiler.target", "17");
editor.properties().updateProperty(true, "project.build.sourceEncoding", "UTF-8");
// Add dependencies using Coordinates
Coordinates junit = Coordinates.of("org.junit.jupiter", "junit-jupiter", "5.10.0");
editor.dependencies().updateDependency(true, junit);
// Add build plugins
Coordinates compiler = Coordinates.of("org.apache.maven.plugins", "maven-compiler-plugin", "3.12.0");
editor.plugins().updatePlugin(true, compiler);
// Set project version
editor.setVersion("2.0.0");
String result = editor.toXml();
Editing Existing POMs
Load and modify existing POM files while preserving formatting:
String existingPom = """
<?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>existing-project</artifactId>
<version>1.0.0</version>
</project>
""";
Document doc = Document.of(existingPom);
PomEditor editor = new PomEditor(doc);
Element root = editor.root();
// Add new elements - formatting and comments are preserved
editor.insertMavenElement(root, NAME, "Existing Project");
editor.insertMavenElement(root, DESCRIPTION, "Updated with DomTrip");
String result = editor.toXml();
Dependency Management Operations
Update, upsert, and delete dependencies and managed dependencies:
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>
</project>
""";
PomEditor editor = new PomEditor(Document.of(pom));
Coordinates slf4j = Coordinates.of("org.slf4j", "slf4j-api", "2.0.9");
// Update an existing dependency version
editor.dependencies().updateDependency(false, slf4j);
// Upsert: update if exists, create if not
Coordinates guava = Coordinates.of("com.google.guava", "guava", "32.1.2-jre");
editor.dependencies().updateDependency(true, guava);
// Add to dependencyManagement
Coordinates junit = Coordinates.of("org.junit.jupiter", "junit-jupiter", "5.10.0");
editor.dependencies().updateManagedDependency(true, junit);
// Delete a dependency
editor.dependencies().deleteDependency(guava);
// Delete a managed dependency
editor.dependencies().deleteManagedDependency(junit);
Managing Properties
Add, update, and delete POM properties:
PomEditor editor = new PomEditor();
editor.createMavenDocument("project");
Element root = editor.root();
editor.insertMavenElement(root, MODEL_VERSION, "4.0.0");
// Add properties section and properties
Element properties = editor.insertMavenElement(root, PROPERTIES);
editor.properties().addProperty(properties, "maven.compiler.source", "17");
editor.properties().addProperty(properties, "maven.compiler.target", "17");
editor.properties().addProperty(properties, "project.build.sourceEncoding", "UTF-8");
editor.properties().addProperty(properties, "junit.version", "5.9.2");
// Update an existing property
editor.properties().updateProperty(false, "junit.version", "5.10.0");
// Upsert: update or create
editor.properties().updateProperty(true, "slf4j.version", "2.0.9");
// Delete a property
editor.properties().deleteProperty("slf4j.version");
Exclusion Management
Add and remove dependency exclusions:
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));
Convention-Aligned Dependencies
Add dependencies that match your project's existing conventions:
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>
<properties>
<slf4j.version>2.0.7</slf4j.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
</dependencies>
</project>
""";
PomEditor editor = new PomEditor(Document.of(pom));
// Add a dependency aligned with auto-detected conventions
// Detects: MANAGED + PROPERTY + DOT_SUFFIX → creates property and managed entry
Coordinates jackson = Coordinates.of("com.fasterxml.jackson.core", "jackson-core", "2.15.2");
editor.dependencies().addAligned(jackson);
// Add with explicit options
Coordinates junit = Coordinates.of("org.junit.jupiter", "junit-jupiter", "5.10.0");
editor.dependencies()
.addAligned(junit, AlignOptions.builder().scope("test").build());
// Force a specific style regardless of detected conventions
Coordinates mockito = Coordinates.of("org.mockito", "mockito-core", "5.5.0");
editor.dependencies()
.addAligned(
mockito,
AlignOptions.builder()
.versionStyle(AlignOptions.VersionStyle.INLINE)
.versionSource(AlignOptions.VersionSource.LITERAL)
.scope("test")
.build());
Cross-POM Alignment
Move dependency versions from child POMs to a parent POM:
String childPom = """
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.example</groupId>
<artifactId>parent-project</artifactId>
<version>1.0.0</version>
</parent>
<artifactId>child-module</artifactId>
<dependencies>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>32.1.2-jre</version>
</dependency>
</dependencies>
</project>
""";
String parentPom = """
<?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>parent-project</artifactId>
<version>1.0.0</version>
<packaging>pom</packaging>
</project>
""";
PomEditor child = new PomEditor(Document.of(childPom));
PomEditor parent = new PomEditor(Document.of(parentPom));
// Move a single dependency's version to the parent's dependencyManagement
Coordinates guava = Coordinates.of("com.google.guava", "guava", null);
child.dependencies()
.alignToParent(
guava,
parent,
AlignOptions.builder()
.versionSource(AlignOptions.VersionSource.PROPERTY)
.namingConvention(AlignOptions.PropertyNamingConvention.DOT_SUFFIX)
.build());
// Child POM: dependency is now version-less
// Parent POM: has dependencyManagement entry + guava.version property
Next Steps
- PomEditor API Reference - Complete method reference
- Element Ordering - How Maven element ordering works
- Dependency Alignment - Auto-detect and align conventions
- Cross-POM Alignment - Multi-module version management