Maven Dependency Plugin - 更改依赖树的输出格式

Maven Dependency Plugin - Change Output Format of Dependency Tree

我使用 Maven 3.3.9Maven Dependency Plugin 2.4 版生成 GraphML 格式的模块依赖关系树。将该文件导入yed等工具生成依赖关系图。

我使用以下命令进行测试:

mvn dependency:tree -DoutputType=graphml -DoutputFile=dependency.graphml

我遇到的问题是,文件中的每个节点都有太多信息无法满足我的需要。这让我的图表很难读。

我得到的输出(这是一个例子):

org.apache.maven.plugins:maven-dependency-plugin:maven-plugin:2.0-alpha-5-SNAPSHOT

我想要的(这是一个例子):

maven-dependency-plugin

如何修改输出格式以满足我的需要?

获得当前和期望的输出并了解目的(如果可能)会很有帮助,因为 maven 有很多功能,我们可以阻止您 "re-inventing the wheel" 并节省您的时间。

我读过 docs,似乎他们没有向 exclude/include 部分依赖项公开接口,目前最好的解决方案是使用 grep

$ mvn -v
Apache Maven 3.3.9

Outputtype 点对 grepping 更友好

$ 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] ------------------------------------------------------------------------
[INFO] Total time: 1.215 s
[INFO] Finished at: 2018-03-27T17:58:31+03:00
[INFO] Final Memory: 14M/303M
[INFO] ------------------------------------------------------------------------

首先 grep 所有带有 >

的行
$ mvn dependency:tree -DoutputType=dot | grep \>
[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" ; 

获取>

之后的字符串
$ mvn dependency:tree -DoutputType=dot | grep \> | cut -d\> -f2
 "org.apache.httpcomponents:httpclient:jar:4.5.5:compile" ; 
 "com.google.code.gson:gson:jar:2.8.2:compile" ; 
 "info.picocli:picocli:jar:2.3.0:compile" ; 
 "log4j:log4j:jar:1.2.17:compile" ; 
 "org.xerial:sqlite-jdbc:jar:3.21.0:compile" ; 
 "org.apache.httpcomponents:httpcore:jar:4.4.9:compile" ; 
 "commons-logging:commons-logging:jar:1.2:compile" ; 
 "commons-codec:commons-codec:jar:1.10:compile" ; 

用冒号分割字符串,得到第二个匹配项

$ mvn dependency:tree -DoutputType=dot | grep \> | cut -d\> -f2 | cut -d: -f2
httpclient
gson
picocli
log4j
sqlite-jdbc
httpcore
commons-logging
commons-codec

给你,工件列表

更新:

在“:tree”和"BUILD SUCCESS"

之间拉线
$ mvn dependency:tree | awk '/:tree/,/BUILD SUCCESS/'
[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ test ---
[INFO] com.a:test:jar:1.0
[INFO] +- org.apache.httpcomponents:httpclient:jar:4.5.5:compile
[INFO] |  +- org.apache.httpcomponents:httpcore:jar:4.4.9:compile
[INFO] |  +- commons-logging:commons-logging:jar:1.2:compile
[INFO] |  \- commons-codec:commons-codec:jar:1.10:compile
[INFO] +- com.google.code.gson:gson:jar:2.8.2:compile
[INFO] +- info.picocli:picocli:jar:2.3.0:compile
[INFO] +- log4j:log4j:jar:1.2.17:compile
[INFO] \- org.xerial:sqlite-jdbc:jar:3.21.0:compile
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS

从顶部(使用 awk)和底部(使用 head)删除两行

$ mvn dependency:tree | awk '/:tree/,/BUILD SUCCESS/' | awk 'NR > 2 { print }' | head -n -2
[INFO] +- org.apache.httpcomponents:httpclient:jar:4.5.5:compile
[INFO] |  +- org.apache.httpcomponents:httpcore:jar:4.4.9:compile
[INFO] |  +- commons-logging:commons-logging:jar:1.2:compile
[INFO] |  \- commons-codec:commons-codec:jar:1.10:compile
[INFO] +- com.google.code.gson:gson:jar:2.8.2:compile
[INFO] +- info.picocli:picocli:jar:2.3.0:compile
[INFO] +- log4j:log4j:jar:1.2.17:compile
[INFO] \- org.xerial:sqlite-jdbc:jar:3.21.0:compile

拉相关线

$ mvn dependency:tree | awk '/:tree/,/BUILD SUCCESS/' | awk 'NR > 2 { print }' | head -n -2 | grep -o -P '.*(?<=:).*(?=:jar)'
[INFO] +- org.apache.httpcomponents:httpclient
[INFO] |  +- org.apache.httpcomponents:httpcore
[INFO] |  +- commons-logging:commons-logging
[INFO] |  \- commons-codec:commons-codec
[INFO] +- com.google.code.gson:gson
[INFO] +- info.picocli:picocli
[INFO] +- log4j:log4j
[INFO] \- org.xerial:sqlite-jdbc

通过使用 sed -e 's/\(- \).*\(:\)//'

删除 -(破折号和 space)和 :(冒号)之间的字符串来删除 groupId
$ mvn dependency:tree | awk '/:tree/,/BUILD SUCCESS/' | awk 'NR > 2 { print }' | head -n -2 | grep -o -P '.*(?<=:).*(?=:jar)' | sed -e 's/\(- \).*\(:\)//'
[INFO] +- :httpclient
[INFO] |  +- :httpcore
[INFO] |  +- :commons-logging
[INFO] |  \- :commons-codec
[INFO] +- :gson
[INFO] +- :picocli
[INFO] +- :log4j
[INFO] \- :sqlite-jdbc

使用 tr

删除不必要的冒号
$ mvn dependency:tree | awk '/:tree/,/BUILD SUCCESS/' | awk 'NR > 2 { print }' | head -n -2 | grep -o -P '.*(?<=:).*(?=:jar)' | sed -e 's/\(- \).*\(:\)//' | tr -d :
[INFO] +- httpclient
[INFO] |  +- httpcore
[INFO] |  +- commons-logging
[INFO] |  \- commons-codec
[INFO] +- gson
[INFO] +- picocli
[INFO] +- log4j
[INFO] \- sqlite-jdbc

更新二:

虽然你完全改变了你的问题,但这里有一个奇特的 one-liner 答案:

mvn dependency:tree -DoutputType=graphml -DoutputFile=dependency.graphml && python -c "exec(\"from bs4 import BeautifulSoup;bs = BeautifulSoup(open('dependency.graphml'), 'xml')\nfor e in bs.find_all('NodeLabel'):    e.string = e.string.split(':')[1]\nprint(bs.prettify())\")" > dependency_fixed.graphml

生成依赖关系树 w/

mvn dependency:tree -DoutputType=graphml -DoutputFile=dependency.graphml

完成后(这就是 && 的原因)它将执行 python 脚本

from bs4 import BeautifulSoup
bs = BeautifulSoup(open('dependency.graphml'), 'xml')
for e in bs.find_all('NodeLabel'):
    e.string = e.string.split(':')[1]
print(bs.prettify())  # print(bs) will print the minified version

遍历 NodeLabel 个元素,用冒号分割结果的第二个元素替换值,并用 > dependency_fixed.graphml

将输出保存到文件中