How do I obtain a fully resolved Model of a pom file?
This is basically a rephrasing of How can i programmaticaly build the effective model of a pom file?
I'm building a maven plugin that performs some validation rules against a set of modules. Those modules' pom files are available but they're not part of the reactor when the plugin is executed.
I can read a pom file and obtain the corresponding Model object using this method (removed exception handling for simplicity):
private Model pomToModel(String pathToPom) throws Exception {
BufferedReader in = new BufferedReader(new FileReader(pathToPom));
MavenXpp3Reader reader = new MavenXpp3Reader();
Model model = reader.read(in);
return model;
}
And it works but the Model object has only the same information that the pom file has.
How can I improve that method so that I can obtain a "fully resolved" Model object? By fully resolved, I mean: with all the transitive dependencies and everything else from the parent poms.
Cheers!
the maven help plugin does something similar when "mvn help:effective-pom" is executed.
see http://svn.apache.org/viewvc/maven/plugins/tags/maven-help-plugin-2.1.1/src/main/java/org/apache/maven/plugins/help/EffectivePomMojo.java?view=markup for the sources.
I think this will not show the transitive depedencies.
The mvn dependency:tree goal does that: http://svn.apache.org/viewvc/maven/plugins/tags/maven-dependency-plugin-2.4/src/main/java/org/apache/maven/plugin/dependency/TreeMojo.java?view=markup
maybe you can create a mixture of both?
I did it :-)
help:effective-pom and dependency:tree really did not help at all.
I had to look at how maven builds the Model for the MavenProject that gets injected in the mojo. help:effective-pom already receives the resolved Model, and dependency:tree only builds a DependencyGraph, but it doesn't load the whole model for a pom into memory.
By using the code below I was able to get a Model object with all the information from the parent, with resolved ${property} expressions, and expanded transitive dependencies.
Here's how:
1) Get a ModelResolver
You will need an instance of interface org.apache.maven.model.resolution.ModelResolver. Unfortunately, maven doesn't provide one easily via dependency injection (at least I couldn't find one), so we'll have to build one. To make things even better, the only two implementations of that interface are package protected, so you need to use some reflection magic to instantiate it. The concrete classes that implement it are DefaultModelResolver and ProjectModelResolver. I was able to build a DefaultModelResolver like this
2) Build the Model
When you have a modelResolver, you can build the Model from a pom file like this:
Doesn't look pretty, but it worked for me.. :P
Maybe is too late for you but if it can help others in the future. So I did it like that:
And that's it. It should work. If you have some special packaging (e.g. bundle...) In the target pom project make sure the plugins associated to those packaging are installed in your current project.
Romain provided the good answer above, but it was using a deprecated class that was removed from maven 3.x The updated version is this :
A working example is in the hierarchy-maven-plugin
Just in case anyone want it, here is an example running in Groovy. It uses the Grape to dynamically load the depdencies needed to consume the pom.xml. It loads both the runtime classpath and the test classpath.
The source code you seek is in
help:effective-pom
, somewhere.--- Edit update ---
After a quick glance, it would seem that you would need to build a Maven
Project
from the read pom. This likely will involve a number of steps that include resolution of the parent project of the POM, downloading and parsing other Maven plugin artifacts and wiring all of the references together.Reading the child-level pom alone won't do it.