Parsing a Maven POM

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.

Advertisements

16 thoughts on “Parsing a Maven POM

  1. Hi,
    Thank you for your post, it helps me a lot.
    But I’d like to retrieve information from parent pom like the version of a
    particular jar.
    I had to use the plugin assembly to aggregate the different parent pom to write to an output file…But it is so heavy method.
    Do you have an idea?
    Thank you a lot.
    Tim

  2. Hi,

    Thanks for this article. this was really very helpful. I would appreciate if you can provide more information. Such as if I want to know location of modules and dependencies, How should I proceed, using maven API rather than writing my own code.
    Also what is the significance of maven-mode-builder jar.

    Thanks
    Raghvendra

  3. Can you please send me information on the parent pom… i am getting an exception for a duplicate groupid.

    1. Hi,

      What parent POM? Please send some more information so I can try to help you.

      Cheers,
      Michael

  4. hi

    your post has given me an insight on how to proceed. I would like to know if i want to write a custom rule using maven enforcer plugin to check if every project of mine has a description or not. how should i proceed? any help would be highly appreciated

  5. Hi,
    I want to parse parent pom.xml so that i can extract data from all the pom which are included in parent pom.xml….can you please help?
    Thanks in advance

  6. How to read Parent pom and child pom whatever exists in pom.xml (parent ..child, etc)
    Maven Project Structure

    Parent
    com.test
    myapp
    0.0.1-SNAPSHOT
    pom

    myapp-presentation
    myapp-services

    Child

    com.test
    myapp
    0.0.1-SNAPSHOT

    myapp-presentation

      1. Hi Michael,
        Thanks for your response…
        I have a Maven project with multiple modules and sub-modules and i want to read it at once whatever exists in pom.xml like jars, plugins, etc , using only one call to “parent pom.xml”

        For a basic project, the following structure would work:
        .
        ├── modules-root
        │ ├── moduleA
        │ │ └── pom.xml <— Module A POM
        │ ├── moduleB
        │ │ └── pom.xml <— Module B POM
        │ └── pom.xml <— modules root
        └── pom.xml <— project-root

        project-root

        4.0.0

        org.test
        project-root
        pom
        1.0.0-SNAPSHOT

        junit
        junit
        4.11
        test

        modules-root

        moduleB

        4.0.0

        org.test
        super-pom
        1.0.0-SNAPSHOT

        org.test.moduleB
        modules-moduleB
        jar

        but my issue i am not able to read what is existing in parent pom.xml
        Model model = null;
        FileReader reader = null;
        MavenXpp3Reader mavenreader = new MavenXpp3Reader();

        try {
        reader = new FileReader(pomFile);
        model = mavenreader.read(reader);
        model.setPomFile(new File(pomFile));
        }catch(Exception ex){

        ex.printStackTrace();
        }

        model.getParent();

        List modules = model.getModules();

        for (String module : modules) {

        System.out.println(module);
        }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s