A library for analyzing and programmatic editing of Maven pom.xml files.
Its two main parts are PomTransformer and MavenSourceTree
Transform a single pom.xml file by using any of the ready-made Transformations
from the org.l2x6.pom.tuner.transform package or by writing your own.
A basic usage example:
import org.l2x6.pom.tuner.PomTransformer;
import org.l2x6.pom.tuner.model.Gavtcs;
import org.l2x6.pom.tuner.transform.Dependencies;
import org.l2x6.pom.tuner.transform.Modules;
import org.l2x6.pom.tuner.transform.Properties;
PomTransformer.of(
/* Create or set <my-prop>new-value</my-prop>; when creating put it after <existing-property>...</existing-property> */
Properties.set("my-prop", "new-value").afterElement("existing-property"),
/* Remove all properties with names ending with ".version" */
Properties.remove(textElement -> textElement.getElementName().endsWith(".version")),
/* Add org.acme:acme:1.2.3 dependency at a proper position (runtime dependencies first, test dependencies last, then by groupId:artifactId, etc.) */
Dependencies.add(Gavtcs.of("org.acme:acme:1.2.3")).at(Gavtcs.scopeAndGroupFirstComparator())
/* Set version of org.foo:bar to 2.3.4 */
Dependencies.select("org.foo:bar").forEach(gavtcsElement -> gavtcsElement.setVersion("2.3.4"))
).transform(Path.of("pom.xml"));Characteristics:
-
Parses the given
pom.xmlfile once using DOM Trip, applies one or moreTransformations and writes the file back to the disk. -
Keeps all formatting and whitespace, including end of line (EOL) characters (unlike many DOM writers which use the current platform’s default EOLs).
-
Indentation for newly added elements is auto-detected based on the first indent available in the file.
-
Respects the canonical ordering when adding new elements under
<project>and<profile>.
A representation of a Maven module hierarchy.
It comes in handy in a variety of scenarios:
-
Analyze relationships in the dependency graph:
-
On which other modules (including transitives) depends some given module? - see
MavenSourceTree.collect[Own|Transitive]Dependencies() -
Which modules in the tree are required for building some given module? - see
MavenSourceTree.findRequiredModules()
-
-
Transform the
pom.xmlfiles:-
Set versions of the modules - see
MavenSourceTree.setVersions() -
Reduce the tree by removing some modules from the
pom.xmlfile - seeMavenSourceTree.unlinkModules()
-
<dependency>
<groupId>org.l2x6.pom-tuner</groupId>
<artifactId>pom-tuner</artifactId>
<version><!-- see below --></version>
</dependency>Check the latest version on Maven Central: https://repo1.maven.org/maven2/org/l2x6/pom-tuner/pom-tuner/