Some days ago I needed some information from a Maven “pom.xml” file. First I thought about reading the information from that file using XPATH. But, after digging around with Google, I found it’s much simpler to use Maven’s own capabilities for that task.
You need only two JAR files:
- maven-model-2.0.9.jar
- plexus-utils-1.5.1.jar
And a small piece of code:
Reader reader = new FileReader(pomXmlFile);
try {
MavenXpp3Reader xpp3Reader = new MavenXpp3Reader();
Model model = xpp3Reader.read(reader);
} finally {
reader.close();
}
The only thing that took a bit more time was to include an attached “parent POM”. If you’re interested in that, just let me know.