我使用Maven 3.3.9和Maven依赖插件版本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
如何修改输出格式以满足我的需要?
发布于 2018-03-27 14:41:22
这将有助于获得当前和期望的输出,并了解目的(如果可能的话),因为maven有许多特性,我们可以防止您“重新发明车轮”,并节省您的时间。
我读过文档,他们似乎没有公开接口来排除/包含部分依赖关系,到目前为止最好的解决方案是使用grep
$ mvn -v
Apache Maven 3.3.9输出型点更适合于打招呼。
$ 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给你,文物清单
更新:
在“:树”和“建立成功”之间拉线
$ 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通过使用groupId删除- (破折号和空格)与: (冒号)之间的字符串来删除sed -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应用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更新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生成依赖关系树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将out保存到文件中。
https://stackoverflow.com/questions/49515260
复制相似问题