以所需格式列出Maven项目的依赖项

时间:2019-04-20 18:49:47

标签: java maven dependencies maven-plugin

我正在尝试使用以下命令在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 个答案:

答案 0 :(得分:0)

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