LemongrabThree
LemongrabThree

Reputation: 129

How do I Get a Maven Project's Parent POM using the Java API?

I have a Java project that needs to access Maven properties of another project, which it can access on the file system but has no Maven relationship with - no dependency, parent, plugin, or anything. I found out how to get the object project's POM into the API using this. However, the property I want (jasperreports.version) is inherited from the object project's parent project.

It turns out the getProperties() method of the project that I obtained using the MavenXpp3Reader only contains explicitly mentioned properties, not the ones inherited from the parent. In general, the model (org.apache.maven.model.Model) is pretty much just a syntax tree of the XML. I thought it could be a nice workaround to just duplicate the property in the child POM:

<properties>
    <jasperreports.version2>${jasperreports.version}</jasperreports.version2>
    ...
</properties>

and the properties map at the end just contained the literal string ${jasperreports.version}.

I cannot use the <parent>/<relativePath> property to locate and then manually parse the parent POM, because the parent project will eventually be in a Maven repository instead of being a parent directory. It's a pretty brittle solution anyway. The child project whose POM I am reading will remain accessible as a file, though.

It would be great if I could just get the effective POM of the project as an org.apache.maven.model.Model or org.apache.maven.project.MavenProject which would include all properties from parents with filtering and everything. Is there a way to do this? Or a way I can at least get a Model for the parent project so I can get its properties?

Upvotes: 1

Views: 81

Answers (0)

Related Questions