tags:

views:

246

answers:

4

Hi all,

I would like to be able to determine what versions I am running of a dependency at runtime as well as the version of the web application itself.

Each web application I deploy is packaged with a pom.xml which I can read from, that part is trivial. The next part is parsing the pom without much effort.

As the web application is running, I want to be able to understand what version I am, and what versions my dependencies are.

Ideally, I would like to do something like:

MavenPom pom = new MavenPom(webApplicationPomInputStream);
pom.getVersion();
pom.getArtifactId();
pom.getGroupId();

for(Dependency dependency:pom.getDependencies())
{
  dependency.getVersion();
  dependency.getArtifactId();
  dependency.getGroupId();
}

Should I just use XPath notation here, or is there a library I can call to do this type of thing?

After these posts, I am thinking the quickest/most reliable way is to generate a text file with the dependency tree in it: mvn dependency:tree. Then I will parse the text file, separate the groupId, artifactId, and version, and then determine the structure by the indentation level.

If I do that, can I export to XML instead of text? I can then use JAXB and easily parse that file without doing any/much work.

It is a hack, but looks promising.

Walter

+2  A: 

You can use Maven Embedder as described here

maximdim
This looks like what I want, I have to do some more reading.
Well, I may be wrong but my understanding is that the Maven Embedder allows you to **execute** maven commands within your application at runtime which is not what the OP is asking for. Maybe its API allows to get a `MavenProject` easily though but I'm really not sure of this.
Pascal Thivent
I agree, after looking at the code base, I don't think this will get me rich quick.
Certainly you can get MavenProject instance - it's right in the link I've posted. Another interesting question though if it will try to resolve all transient dependencies (e.g. download dependencies poms) - not sure if you would want to do that in your webapp.
maximdim
A: 

Maybe the command mvn dependency:resolve would do the trick (see the dependency plugin documentation)

Emmanuel

ehsavoie
no no, this is when the web application itself is running. Of course, I can probably use the source, but I think the Maven Embedder might be a better idea.
A: 

Maven has of course such an API. Have a look at org.apache.maven.project.MavenProject. But, to be honest, I don't think it will be that easy to create a MavenProject instance. The source code will be helpful here, check for example MavenProjectTest or maybe the Maven Plugin API (actually, this task would be much, really much, simpler to achieve from a Mojo) for some guidance.

I'd suggest to search for or ask this question on the Maven Mailing Lists, org.apache.maven.dev would be appropriate here IMHO.

Pascal Thivent
Thanks, this looks like the Maven Embedder idea suggested above. I'll probably end up using both of these ideas.
I'm not sure the Maven Embedder is what you are looking for (see my comment to the other answer) but you'll see :)
Pascal Thivent
Pascal, I think you got me the closest to what I am looking for. After looking at the API, it looks like there is a bit of work involved. I'm trying to think what exactly I need to accomplish before determining a solution. I essentially just need to know what I have deployed to a server. It might be easier to save the dependency tree as a text file, parse it in separating the groupId, artifactId, and version, and then constructing hierarchy based on the indentation.
Yes, it might be.
Pascal Thivent
A: 

I will just use the mvn dependency:tree plugin to generate a text file with the dependency tree. Then I will parse that in and create the dependency tree/graph from that. I will get the scope of the artifact, groupId, artifactId, version, and its parent.

I successfully implemented this type of lookup, it simply takes the dependency output, parses it and organizes dependencies simply using the indentation, nothing fancy. The artifact, group, version, and scope are easily parsed since the separator is a :.

Walter