【问题标题】:Maven Dependency Plugin - Change Output Format of Dependency TreeMaven 依赖插件 - 更改依赖树的输出格式
【发布时间】:2018-03-27 14:13:57
【问题描述】:

我使用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

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

【问题讨论】:

    标签: java maven maven-3


    【解决方案1】:

    获得当前和所需的输出并了解目的(如果可能)会很有帮助,因为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
    

    更新 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
    

    使用/生成依赖关系树

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

    完成后(这就是&amp;&amp;的原因)它将执行一个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元素,用冒号分割结果的第二个元素替换值,并用&gt; dependency_fixed.graphml将输出保存到文件中

    【讨论】:

    • @IdddoE 谢谢你的回答。我已经更新了我的问题并使其更加精确。不过,感谢您迄今为止关于 grep 的提示。
    • 嗨@IddoE 更新了我的问题以说明我最终想要实现的目标。
    • 再次感谢您的更新。我不是故意让你不高兴的。将尝试 Python 方法!随时通知您!
    • 您的解决方案按预期工作。非常感谢。我所要做的就是使用 pip install 安装 Python 3bs4lxml
    • @Iddo 谢谢,你的回答看起来很有帮助。我遇到了类似的问题stackoverflow.com/questions/55776695/…,如果你能提供帮助,请看看。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-30
    • 1970-01-01
    • 2015-05-12
    • 1970-01-01
    • 1970-01-01
    • 2017-09-13
    相关资源
    最近更新 更多