我使用Maven 3.3.9
和Maven 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
如何修改输出格式以满足我的需求?
答案 0 :(得分:4)
获得当前&期望的输出并了解目的(如果可能),因为maven
具有许多功能,我们可以阻止您“重新发明轮子”并节省您的时间。
我已经阅读了docs,似乎他们没有公开接口来排除/包含部分依赖项,到目前为止最好的解决方案是使用grep
$ mvn -v
Apache Maven 3.3.9
输出类型点对于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/\(- \).*\(:\)/\1\2/'
(冒号)之间的字符串,从而删除groupId
$ 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
使用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
虽然你完全改变了你的问题,但这是一个奇特的单行答案:
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