Class PomEditor
java.lang.Object
eu.maveniverse.domtrip.Editor
eu.maveniverse.domtrip.maven.AbstractMavenEditor
eu.maveniverse.domtrip.maven.PomEditor
Specialized editor for Maven POM files that extends the base
Editor class
with Maven-specific functionality and element ordering.
The PomEditor provides lossless XML editing capabilities specifically tailored for Maven POM files, including:
- Maven Element Ordering - Automatically orders elements according to Maven conventions
- Formatting Preservation - Maintains original formatting, whitespace, and comments
- Intelligent Blank Lines - Adds appropriate blank lines between element groups
- Maven-specific Methods - Convenience methods for common POM operations
Basic Usage:
// Parse existing POM
Document doc = Document.of(pomXmlString);
PomEditor editor = new PomEditor(doc);
// Add elements with proper ordering
Element root = editor.root();
editor.insertMavenElement(root, "description", "My project description");
editor.insertMavenElement(root, "name", "My Project"); // Will be ordered before description
// Serialize with preserved formatting
String result = editor.toXml();
Element Ordering:
The PomEditor automatically orders elements according to Maven POM conventions:
- Project elements: modelVersion, parent, groupId, artifactId, version, packaging, name, description, etc.
- Build elements: defaultGoal, directory, finalName, sourceDirectory, etc.
- Plugin elements: groupId, artifactId, version, extensions, executions, etc.
- Dependency elements: groupId, artifactId, version, classifier, type, scope, etc.
- Since:
- 0.1
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionclassHelper for managing regular and managed Maven dependencies within a POM.classclassclassclassHelper for inspecting Maven profiles within a POM.classclassNested classes/interfaces inherited from class eu.maveniverse.domtrip.Editor
eu.maveniverse.domtrip.Editor.EditorCommentBuilder, eu.maveniverse.domtrip.Editor.EditorElementBuilder, eu.maveniverse.domtrip.Editor.EditorTextBuilder, eu.maveniverse.domtrip.Editor.NodeBuilder -
Constructor Summary
ConstructorsConstructorDescriptionCreates a new PomEditor with default configuration.PomEditor(eu.maveniverse.domtrip.Document document) Creates a new PomEditor with an existing Document.PomEditor(eu.maveniverse.domtrip.Document document, eu.maveniverse.domtrip.DomTripConfig config) Creates a new PomEditor with an existing Document and custom configuration.PomEditor(eu.maveniverse.domtrip.DomTripConfig config) Creates a new PomEditor with custom configuration. -
Method Summary
Modifier and TypeMethodDescriptionvoidcreateMavenDocument(String rootElementName) Creates a new Maven POM document with the specified root element name.Create a helper for managing regular and managed Maven dependencies within this POM.eu.maveniverse.domtrip.ElementfindChildElement(eu.maveniverse.domtrip.Element parent, String elementName) Finds a child element by name under the specified parent.getChildElementText(eu.maveniverse.domtrip.Element parent, String childName) Gets the text content of a child element, or returns null if not found.getOrderListForParent(eu.maveniverse.domtrip.Element parent) Gets the appropriate element order list for the given parent element.booleanhasChildElement(eu.maveniverse.domtrip.Element parent, String childName) Checks if an element exists as a child of the given parent.protected eu.maveniverse.domtrip.ElementinsertElementAtPosition(eu.maveniverse.domtrip.Element parent, String elementName, eu.maveniverse.domtrip.Element insertBefore, eu.maveniverse.domtrip.Element insertAfter, List<String> order, int elementIndex) Enhanced element insertion with POM-specific blank line handling.eu.maveniverse.domtrip.ElementinsertMavenElement(eu.maveniverse.domtrip.Element parent, String elementName) Inserts a new Maven element with proper ordering and formatting.eu.maveniverse.domtrip.ElementinsertMavenElement(eu.maveniverse.domtrip.Element parent, String elementName, String textContent) Inserts a new Maven element with text content and proper ordering.parent()plugins()profiles()Create a helper for inspecting Maven profiles within this POM.voidsetPackaging(String value) Setsproject/packagingto the given value.voidsetVersion(String value) Setsproject/versionorproject/parent/version(if project version doesn't exist).protected booleanshouldSkipInOrdering(String elementName) Determines whether an element name should be skipped during ordering analysis.eu.maveniverse.domtrip.ElementupdateOrCreateChildElement(eu.maveniverse.domtrip.Element parent, String childName, String content) Updates or creates a child element with the given content.Methods inherited from class AbstractMavenEditor
insertElementAtCorrectPosition, insertElementAtCorrectPosition, toCoordinates, toGA, toGATC, toJarCoordinates, toPluginGA, toPomCoordinatesMethods inherited from class eu.maveniverse.domtrip.Editor
add, addBlankLineAfter, addBlankLineBefore, addComment, addElement, addElement, addElement, addElement, addElements, addQNameElements, commentOutElement, commentOutElements, config, createDocument, document, documentStats, insertElementAfter, insertElementAfter, insertElementAt, insertElementAt, insertElementBefore, insertElementBefore, isWellFormed, removeAttribute, removeElement, root, select, selectFirst, setAttribute, setAttributes, setTextContent, toXml, toXml, toXmlPretty, uncommentElement, walk
-
Constructor Details
-
PomEditor
public PomEditor()Creates a new PomEditor with default configuration. -
PomEditor
public PomEditor(eu.maveniverse.domtrip.DomTripConfig config) Creates a new PomEditor with custom configuration.- Parameters:
config- the configuration to use
-
PomEditor
public PomEditor(eu.maveniverse.domtrip.Document document) Creates a new PomEditor with an existing Document.- Parameters:
document- the existing Document to edit
-
PomEditor
public PomEditor(eu.maveniverse.domtrip.Document document, eu.maveniverse.domtrip.DomTripConfig config) Creates a new PomEditor with an existing Document and custom configuration.- Parameters:
document- the existing Document to editconfig- the configuration to use
-
-
Method Details
-
getOrderListForParent
Gets the appropriate element order list for the given parent element. This implementation provides POM-specific ordering with blank line markers.- Specified by:
getOrderListForParentin classAbstractMavenEditor- Parameters:
parent- the parent element- Returns:
- the ordered list of element names, or null if no specific ordering is defined
-
shouldSkipInOrdering
Determines whether an element name should be skipped during ordering analysis. For POM files, empty strings represent blank line markers.- Overrides:
shouldSkipInOrderingin classAbstractMavenEditor- Parameters:
elementName- the element name to check- Returns:
- true if this element should be skipped during ordering
-
insertElementAtPosition
protected eu.maveniverse.domtrip.Element insertElementAtPosition(eu.maveniverse.domtrip.Element parent, String elementName, eu.maveniverse.domtrip.Element insertBefore, eu.maveniverse.domtrip.Element insertAfter, List<String> order, int elementIndex) throws eu.maveniverse.domtrip.DomTripException Enhanced element insertion with POM-specific blank line handling.- Overrides:
insertElementAtPositionin classAbstractMavenEditor- Parameters:
parent- the parent elementelementName- the element name to insertinsertBefore- element to insert before (may be null)insertAfter- element to insert after (may be null)order- the complete ordering listelementIndex- the index of this element in the ordering- Returns:
- the newly created element
- Throws:
eu.maveniverse.domtrip.DomTripException- if the element cannot be added
-
insertMavenElement
public eu.maveniverse.domtrip.Element insertMavenElement(eu.maveniverse.domtrip.Element parent, String elementName) throws eu.maveniverse.domtrip.DomTripException Inserts a new Maven element with proper ordering and formatting.This method automatically determines the correct position for the new element based on Maven POM conventions and adds appropriate blank lines.
- Parameters:
parent- the parent elementelementName- the name of the new element- Returns:
- the newly created element
- Throws:
eu.maveniverse.domtrip.DomTripException- if the element cannot be added
-
insertMavenElement
public eu.maveniverse.domtrip.Element insertMavenElement(eu.maveniverse.domtrip.Element parent, String elementName, String textContent) throws eu.maveniverse.domtrip.DomTripException Inserts a new Maven element with text content and proper ordering.- Parameters:
parent- the parent elementelementName- the name of the new elementtextContent- the text content for the element- Returns:
- the newly created element
- Throws:
eu.maveniverse.domtrip.DomTripException- if the element cannot be added
-
findChildElement
public eu.maveniverse.domtrip.Element findChildElement(eu.maveniverse.domtrip.Element parent, String elementName) Finds a child element by name under the specified parent.- Parameters:
parent- the parent elementelementName- the child element name to find- Returns:
- the child element if found, null otherwise
-
createMavenDocument
public void createMavenDocument(String rootElementName) throws eu.maveniverse.domtrip.DomTripException Creates a new Maven POM document with the specified root element name.- Parameters:
rootElementName- the name of the root element (typically "project")- Throws:
eu.maveniverse.domtrip.DomTripException- if the document cannot be created
-
dependencies
Create a helper for managing regular and managed Maven dependencies within this POM. Provides high-level operations for adding, updating, deleting, aligning, and inspecting dependencies, including support for exclusions, dependencyManagement, and convention detection.- Returns:
- a Dependencies helper bound to this PomEditor instance
-
plugins
-
extensions
-
subprojects
-
properties
-
parent
-
profiles
Create a helper for inspecting Maven profiles within this POM.- Returns:
- a Profiles helper bound to this PomEditor instance
- Since:
- 1.1.0
-
setVersion
Setsproject/versionorproject/parent/version(if project version doesn't exist).- Parameters:
value- the version value- Throws:
eu.maveniverse.domtrip.DomTripException- if no version element can be found
-
setPackaging
Setsproject/packagingto the given value.- Parameters:
value- the packaging value (e.g., "jar", "pom", "war")- Throws:
eu.maveniverse.domtrip.DomTripException- if an error occurs during editing
-
hasChildElement
Checks if an element exists as a child of the given parent.This is a convenience method that provides a simple boolean check for child element existence without needing to handle Optional.
Example:
PomEditor editor = new PomEditor(document); Element root = editor.root(); if (editor.hasChildElement(root, "properties")) { // Properties section exists }- Parameters:
parent- the parent elementchildName- the child element name to check- Returns:
- true if the child element exists, false otherwise
- Since:
- 0.3.0
- See Also:
-
getChildElementText
Gets the text content of a child element, or returns null if not found.This is a convenience method that provides a simple way to get child element text content with null fallback instead of handling Optional.
Example:
PomEditor editor = new PomEditor(document); Element dependency = // ... get dependency element String version = editor.getChildElementText(dependency, "version"); if (version != null) { // Process version }- Parameters:
parent- the parent elementchildName- the child element name- Returns:
- the text content of the child element, or null if not found
- Since:
- 0.3.0
- See Also:
-
updateOrCreateChildElement
public eu.maveniverse.domtrip.Element updateOrCreateChildElement(eu.maveniverse.domtrip.Element parent, String childName, String content) throws eu.maveniverse.domtrip.DomTripException Updates or creates a child element with the given content.If the child element exists, updates its text content. If it doesn't exist, creates it with the specified content using proper Maven element ordering.
Example:
PomEditor editor = new PomEditor(document); Element root = editor.root(); // This will update existing description or create new one editor.updateOrCreateChildElement(root, "description", "My project description");- Parameters:
parent- the parent elementchildName- the child element namecontent- the content to set- Returns:
- the updated or created element
- Throws:
eu.maveniverse.domtrip.DomTripException- if an error occurs during editing- Since:
- 0.3.0
- See Also:
-