搜索maven项目中是否存在依赖项

时间:2013-01-31 10:34:51

标签: maven dependencies pom.xml

对于我正在构建的脚本的一部分,我希望能够在maven项目中搜索某个依赖项。我已经看过maven的dependency插件,但是我找不到任何关于如何使用它的好教程,到目前为止还没有运气好。

在maven项目中搜索存在依赖项的最佳方法是什么?

2 个答案:

答案 0 :(得分:2)

最好的方法是使用dependency plugin本身,如:

mvn dependency:tree -Dincludes=velocity:velocity

答案 1 :(得分:1)

您可能希望将maven依赖项插件与shell脚本结合使用。 AFAIK依赖插件不支持搜索特定工件。

这是一个例子:

如果您运行mvn org.apache.maven.plugins:maven-dependency-plugin:2.6:list

输出可能类似于:

[INFO] The following files have been resolved:
[INFO]    junit:junit:jar:4.10:test
[INFO]    org.hamcrest:hamcrest-core:jar:1.1:test
[INFO]    commons-logging:commons-logging:jar:1.1.1:compile

您将看到完整的依赖项列表。

现在,如果您在基于Linux的系统上运行,您只需编写一个bash脚本来检查依赖是否存在:

dependencyExists = mvn org.apache.maven.plugins:maven-dependency-plugin:2.6:list | grep "org.hamcrest:hamcrest"

如果变量被填充,则依赖性存在,否则不存在。