开发者

Maven - lookup dependencies at runtime

开发者 https://www.devze.com 2022-12-14 22:11 出处:网络
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.

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(webApplicationPomIn开发者_运维问答putStream);
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


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消