Class DomUtils
java.lang.Object
org.apache.maven.cling.invoker.mvnup.goals.DomUtils
Utility class for XML operations on Maven POM files.
This class provides convenience methods that delegate to:
PomEditor- DomTrip's PomEditorElement- DomTrip's Element API
These methods are kept for convenience and backward compatibility. For more advanced operations, consider using ExtendedPomEditor or DomTrip directly.
Using DomTrip Directly
Many operations can be performed directly using DomTrip's Element API:
// Find child element
Element child = parent.child("version").orElse(null);
// Check if child exists
boolean hasVersion = parent.child("version").isPresent();
// Get child text content
String version = parent.child("version")
.map(Element::textContent)
.orElse(null);
// Get trimmed text content
String trimmedVersion = parent.child("version")
.map(Element::textContentTrimmed)
.orElse(null);
// Set text content (fluent API)
element.textContent("4.0.0");
When to Use DomUtils
Use DomUtils methods when you need:
- Maven-specific element ordering (insertNewElement, insertContentElement)
- High-level helpers (addGAVElements, createDependency, createPlugin)
- Null-safe operations (updateElementContent, removeElement)
- Update-or-create patterns (updateOrCreateChildElement)
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic voidaddGAVElements(eu.maveniverse.domtrip.Element parent, String groupId, String artifactId, String version) Convenience method to add GAV (groupId, artifactId, version) elements to a parent.static eu.maveniverse.domtrip.ElementcreateDependency(eu.maveniverse.domtrip.Element dependenciesElement, String groupId, String artifactId, String version) Convenience method to create a dependency element with GAV.static eu.maveniverse.domtrip.ElementcreatePlugin(eu.maveniverse.domtrip.Element pluginsElement, String groupId, String artifactId, String version) Convenience method to create a plugin element with GAV.static eu.maveniverse.domtrip.ElementfindChildElement(eu.maveniverse.domtrip.Element parent, String name) Finds a child element by name under the specified parent.static eu.maveniverse.domtrip.ElementinsertContentElement(eu.maveniverse.domtrip.Element parent, String name, String content) Inserts a new content element with the given name and text content.static eu.maveniverse.domtrip.ElementinsertNewElement(String name, eu.maveniverse.domtrip.Element parent) Inserts a new child element to the given parent element with proper Maven POM ordering.static voidremoveElement(eu.maveniverse.domtrip.Element element) Removes an element from its parent.static StringtoXml(eu.maveniverse.domtrip.Document document) Serializes a domtrip Document to XML string with preserved formatting.static eu.maveniverse.domtrip.ElementupdateOrCreateChildElement(eu.maveniverse.domtrip.Element parent, String childName, String content) Updates or creates a child element with the given content.
-
Method Details
-
insertNewElement
public static eu.maveniverse.domtrip.Element insertNewElement(String name, eu.maveniverse.domtrip.Element parent) Inserts a new child element to the given parent element with proper Maven POM ordering.- Parameters:
name- the name of the new elementparent- the parent element- Returns:
- the new element
-
insertContentElement
public static eu.maveniverse.domtrip.Element insertContentElement(eu.maveniverse.domtrip.Element parent, String name, String content) Inserts a new content element with the given name and text content.- Parameters:
parent- the parent elementname- the name of the new elementcontent- the text content- Returns:
- the new element
-
findChildElement
public static eu.maveniverse.domtrip.Element findChildElement(eu.maveniverse.domtrip.Element parent, String name) Finds a child element by name under the specified parent.- Parameters:
parent- the parent elementname- the child element name to find- Returns:
- the child element if found, null otherwise
-
toXml
Serializes a domtrip Document to XML string with preserved formatting.- Parameters:
document- the domtrip Document- Returns:
- the XML string with preserved formatting
-
removeElement
public static void removeElement(eu.maveniverse.domtrip.Element element) Removes an element from its parent.- Parameters:
element- the element to remove
-
addGAVElements
public static void addGAVElements(eu.maveniverse.domtrip.Element parent, String groupId, String artifactId, String version) Convenience method to add GAV (groupId, artifactId, version) elements to a parent.- Parameters:
parent- the parent element (e.g., dependency or plugin)groupId- the groupId valueartifactId- the artifactId valueversion- the version value (can be null to skip)
-
createDependency
public static eu.maveniverse.domtrip.Element createDependency(eu.maveniverse.domtrip.Element dependenciesElement, String groupId, String artifactId, String version) Convenience method to create a dependency element with GAV.- Parameters:
dependenciesElement- the dependencies parent elementgroupId- the groupId valueartifactId- the artifactId valueversion- the version value (can be null)- Returns:
- the created dependency element
-
createPlugin
public static eu.maveniverse.domtrip.Element createPlugin(eu.maveniverse.domtrip.Element pluginsElement, String groupId, String artifactId, String version) Convenience method to create a plugin element with GAV.- Parameters:
pluginsElement- the plugins parent elementgroupId- the groupId valueartifactId- the artifactId valueversion- the version value (can be null)- Returns:
- the created plugin element
-
updateOrCreateChildElement
public static eu.maveniverse.domtrip.Element updateOrCreateChildElement(eu.maveniverse.domtrip.Element parent, String childName, String content) Updates or creates a child element with the given content.- Parameters:
parent- the parent elementchildName- the child element namecontent- the content to set- Returns:
- the updated or created element
-