以所需格式列出Maven项目的依赖项 [英] List dependencies of maven project in desired format

查看:131
本文介绍了以所需格式列出Maven项目的依赖项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下命令在maven中生成依赖树

Am trying to generate dependency tree in maven, making use of below command

$ mvn dependency:tree -DoutputType=dot

输出如下所示

[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building test 1.0
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ test ---
[INFO] digraph "com.a:test:jar:1.0" { 
[INFO]  "com.a:test:jar:1.0" -> "org.apache.httpcomponents:httpclient:jar:4.5.5:compile" ; 
[INFO]  "com.a:test:jar:1.0" -> "com.google.code.gson:gson:jar:2.8.2:compile" ; 
[INFO]  "com.a:test:jar:1.0" -> "info.picocli:picocli:jar:2.3.0:compile" ; 
[INFO]  "com.a:test:jar:1.0" -> "log4j:log4j:jar:1.2.17:compile" ; 
[INFO]  "com.a:test:jar:1.0" -> "org.xerial:sqlite-jdbc:jar:3.21.0:compile" ; 
[INFO]  "org.apache.httpcomponents:httpclient:jar:4.5.5:compile" -> "org.apache.httpcomponents:httpcore:jar:4.4.9:compile" ; 
[INFO]  "org.apache.httpcomponents:httpclient:jar:4.5.5:compile" -> "commons-logging:commons-logging:jar:1.2:compile" ; 
[INFO]  "org.apache.httpcomponents:httpclient:jar:4.5.5:compile" -> "commons-codec:commons-codec:jar:1.10:compile" ; 
[INFO]  } 
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------

它既具有直接依赖关系,又具有传递依赖关系,我希望它只是直接依赖关系,可以利用grep或某些标志-Dexcludes来帮助实现低于输出的水平

It has both direct and transitive dependencies, what i am expecting is just direct one , can making use of grep or some flags -Dexcludes helps to achieve below output

[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building test 1.0
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ test ---
[INFO] digraph "com.a:test:jar:1.0" { 
[INFO]  "com.a:test:jar:1.0" -> "org.apache.httpcomponents:httpclient:jar:4.5.5:compile" ; 
[INFO]  "com.a:test:jar:1.0" -> "com.google.code.gson:gson:jar:2.8.2:compile" ; 
[INFO]  "com.a:test:jar:1.0" -> "info.picocli:picocli:jar:2.3.0:compile" ; 
[INFO]  "com.a:test:jar:1.0" -> "log4j:log4j:jar:1.2.17:compile" ; 
[INFO]  "com.a:test:jar:1.0" -> "org.xerial:sqlite-jdbc:jar:3.21.0:compile" ;
[INFO]  } 
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------

我期望节点生态系统中的像npm ls --depth=0这样的东西,深度标志有助于获得直接的依赖关系.

Am expecting something like npm ls --depth=0 as in node ecosystem, were depth flags helps to just get direct dependencies.

注意:我无法使用dependency:list,因为我需要上面的结构.

Note: i can't make use of dependency:list as i need above structure.

推荐答案

maven-dependency-plugin:list插件目标通过

The maven-dependency-plugin:list plugin goal provides such a capability with the excludeTransitive option. But since you are looking for a dot graph output format, you cannot make use of it.

该选项将以编程方式解析maven-dependency-plugin:tree输出的输出并删除传递依赖项,即不是以模块工件名称开头的行.

The option would be to programatically parse the output of the maven-dependency-plugin:tree output and delete transitive dependencies, i.e. the lines not starting with your module artifact name.

可以执行以下基本操作:

Something basic as the following would work:

mvn org.apache.maven.plugins:maven-dependency-plugin:2.8:tree -DoutputType=dot -DoutputFile=out_temp & grep "com.a:test:jar:1.0" out_temp > out_final.gv & echo } >> out_final.gv & rm out_temp

这篇关于以所需格式列出Maven项目的依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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