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

And*_*ser 3 java maven-3 maven

我用Maven 3.3.9Maven的依赖插件的版本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

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

Jim*_*est 8

获得当前和所需的输出并了解目的(如果可能)会很有帮助,因为它maven具有许多功能,我们可以防止您“重新发明轮子”并节省您的时间。

我已经阅读了文档,似乎他们没有公开接口来排除/包含部分依赖项,到目前为止最好的解决方案是使用grep

$ mvn -v
Apache Maven 3.3.9
Run Code Online (Sandbox Code Playgroud)

输出类型点对 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] ------------------------------------------------------------------------
Run Code Online (Sandbox Code Playgroud)

首先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" ; 
Run Code Online (Sandbox Code Playgroud)

获取字符串之后 >

$ 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" ; 
Run Code Online (Sandbox Code Playgroud)

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

$ mvn dependency:tree -DoutputType=dot | grep \> | cut -d\> -f2 | cut -d: -f2
httpclient
gson
picocli
log4j
sqlite-jdbc
httpcore
commons-logging
commons-codec
Run Code Online (Sandbox Code Playgroud)

给你,文物清单

更新:

在 ":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
Run Code Online (Sandbox Code Playgroud)

从顶部(使用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
Run Code Online (Sandbox Code Playgroud)

拉相关线路

$ 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
Run Code Online (Sandbox Code Playgroud)

通过使用删除-(破折号和空格)和:(冒号)之间的字符串来删除 groupIdsed -e 's/\(- \).*\(:\)/\1\2/'

$ mvn dependency:tree | awk '/:tree/,/BUILD SUCCESS/' | awk 'NR > 2 { print }' | head -n -2 | grep -o -P '.*(?<=:).*(?=:jar)' | sed -e 's/\(- \).*\(:\)/\1\2/'
[INFO] +- :httpclient
[INFO] |  +- :httpcore
[INFO] |  +- :commons-logging
[INFO] |  \- :commons-codec
[INFO] +- :gson
[INFO] +- :picocli
[INFO] +- :log4j
[INFO] \- :sqlite-jdbc
Run Code Online (Sandbox Code Playgroud)

使用去除不必要的冒号 tr

$ mvn dependency:tree | awk '/:tree/,/BUILD SUCCESS/' | awk 'NR > 2 { print }' | head -n -2 | grep -o -P '.*(?<=:).*(?=:jar)' | sed -e 's/\(- \).*\(:\)/\1\2/' | tr -d :
[INFO] +- httpclient
[INFO] |  +- httpcore
[INFO] |  +- commons-logging
[INFO] |  \- commons-codec
[INFO] +- gson
[INFO] +- picocli
[INFO] +- log4j
[INFO] \- sqlite-jdbc
Run Code Online (Sandbox Code Playgroud)

更新 2:

虽然你完全改变了你的问题,但这里有一个花哨的单行答案:

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
Run Code Online (Sandbox Code Playgroud)

生成依赖树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
Run Code Online (Sandbox Code Playgroud)

它迭代NodeLabel元素,用冒号结果的第二个元素替换值并将输出保存到文件中> dependency_fixed.graphml