Maven - 在运行时查找依赖项 [英] Maven - lookup dependencies at runtime

查看:121
本文介绍了Maven - 在运行时查找依赖项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够确定我在运行时运行的依赖项的版本以及 Web 应用程序本身的版本.

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.

我部署的每个 web 应用程序都打包了一个 pom.xml,我可以从中读取,那部分是微不足道的.下一部分是毫不费力地解析 pom.

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.

在 Web 应用程序运行时,我希望能够了解我是什么版本,以及我的依赖项是什么版本.

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();
}

我应该在这里只使用 XPath 表示法,还是可以调用一个库来做这种类型的事情?

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

在这些帖子之后,我认为最快/最可靠的方法是生成一个包含依赖树的文本文件:mvn dependency:tree.然后我将解析文本文件,将groupId、artifactId和version分开,然后通过缩进级别确定结构.

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.

如果我这样做,我可以导出为 XML 而不是文本吗?然后我可以使用 JAXB 并轻松解析该文件,而无需做任何/太多工作.

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.

沃尔特

推荐答案

我将使用 mvn dependency:tree 插件生成一个带有依赖树的文本文件.然后我将解析它并从中创建依赖树/图.我将获取工件的范围、groupId、artifactId、版本及其父项.

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 :.

沃尔特

这篇关于Maven - 在运行时查找依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆