以所需格式列出 maven 项目的依赖项
Posted
技术标签:
【中文标题】以所需格式列出 maven 项目的依赖项【英文标题】:List dependencies of maven project in desired format 【发布时间】:2019-04-20 18:49:46 【问题描述】:我正在尝试在 maven 中生成依赖树,使用下面的命令
$ 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
有助于实现以下输出
[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
这样的东西,深度标志有助于获得直接依赖关系。
注意:我不能使用dependency:list
,因为我需要上面的结构。
【问题讨论】:
【参考方案1】:maven-dependency-plugin:list
插件目标通过excludeTransitive
选项提供了这样的功能。
但是由于您正在寻找 dot 图形输出格式,因此您无法使用它。
选项将以编程方式解析maven-dependency-plugin:tree
输出的输出并删除传递依赖项,即不以您的模块工件名称开头的行。
下面的一些基本的东西会起作用:
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 项目的依赖项的主要内容,如果未能解决你的问题,请参考以下文章