4

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

  • what are you trying to achieve? are you building a maven tool? if you just want to know it for a specific project do 'mvn dependecy:tree'. if you work on bash do 'mvn dependency:tree|grep runtime' – manuel aldana Dec 30 '09 at 12:46

2 Answers2

2

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

0

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
  • 562,542
  • 136
  • 1,062
  • 1,124
  • Thanks, this looks like the Maven Embedder idea suggested above. I'll probably end up using both of these ideas. –  Dec 30 '09 at 13:09
  • 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 Dec 30 '09 at 14:15
  • 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. –  Dec 31 '09 at 13:39