小编典典

获取干净的机器友好的Maven行/ xml / json输出,可通过STDOUT中的脚本解析?

java

例如,对Maven项目运行以下命令:

mvn dependency:list

我需要从Maven中得到的 只有 这两行(从下面的输出中切出):

com.example.code_samples.maven_dependencies:direct_library:jar:0.0.1-SNAPSHOT:compile
com.example.code_samples.maven_dependencies:indirect_library:jar:0.0.1-SNAPSHOT:compile

有没有办法(CLI--option)在干净的行,xml,json,…中仅查看此请求的信息?

相反,输出看起来更像是非结构化日志。它没有已知的格式,并且在STDOUT中将所有类型的信息混合在一起。

[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO] 
[INFO] direct_library
[INFO] dependent_binary
[INFO] indirect_library
[INFO] maven_dependencies
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building direct_library 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
Downloading: http://nexus:8081/nexus/content/repositories/snapshots/com/example/code_samples/maven_dependencies/indirect_library/0.0.1-SNAPSHOT/maven-metadata.xml
Downloaded: http://nexus:8081/nexus/content/repositories/snapshots/com/example/code_samples/maven_dependencies/indirect_library/0.0.1-SNAPSHOT/maven-metadata.xml (2 KB at 16.1
 KB/sec)
[INFO] 
[INFO] --- maven-dependency-plugin:2.8:list (default-cli) @ direct_library ---
[INFO] 
[INFO] The following files have been resolved:
[INFO]    junit:junit:jar:4.4:test
[INFO]    com.example.code_samples.maven_dependencies:indirect_library:jar:0.0.1-SNAPSHOT:compile
[INFO] 
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building dependent_binary 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
Downloading: http://nexus:8081/nexus/content/repositories/snapshots/com/example/code_samples/maven_dependencies/direct_library/0.0.1-SNAPSHOT/maven-metadata.xml
Downloaded: http://nexus:8081/nexus/content/repositories/snapshots/com/example/code_samples/maven_dependencies/direct_library/0.0.1-SNAPSHOT/maven-metadata.xml (2 KB at 86.2 K
B/sec)
[INFO] 
[INFO] --- maven-dependency-plugin:2.8:list (default-cli) @ dependent_binary ---
[INFO] 
[INFO] The following files have been resolved:
[INFO]    com.example.code_samples.maven_dependencies:direct_library:jar:0.0.1-SNAPSHOT:compile
[INFO]    com.example.code_samples.maven_dependencies:indirect_library:jar:0.0.1-SNAPSHOT:compile
[INFO] 
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building indirect_library 3.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-dependency-plugin:2.8:list (default-cli) @ indirect_library ---
[INFO] 
[INFO] The following files have been resolved:
[INFO]    none
[INFO] 
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building maven_dependencies 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-dependency-plugin:2.8:list (default-cli) @ maven_dependencies ---
[INFO] 
[INFO] The following files have been resolved:
[INFO]    none
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO] 
[INFO] direct_library ..................................... SUCCESS [  0.813 s]
[INFO] dependent_binary ................................... SUCCESS [  0.026 s]
[INFO] indirect_library ................................... SUCCESS [  0.013 s]
[INFO] maven_dependencies ................................. SUCCESS [  0.002 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.065 s
[INFO] Finished at: 2015-03-24T12:10:01+08:00
[INFO] Final Memory: 18M/607M
[INFO] ------------------------------------------------------------------------

更新

另外,我会接受使用Maven API来获取运行时数据的解决方案Collection<String>(而不是无法可靠解析的上述文本输出)。

我看了一下Maven Invoker API,但我没有希望-
我的测试表明这只是从代码中调用Maven的一种方式。这些API不会返回运行时数据(只是错误代码,所有有用的信息会再次打印在日志中)。


阅读 269

收藏
2020-11-26

共1个答案

小编典典

我能够提供一个属性,以将所需的干净输出保存到文件中。

例如,-DoutputFile-Doutput

mvn dependency:list     -DoutputFile=dependencies.output.txt
mvn help:effective-pom  -Doutput=effective.pom.xml

如果需要STDOUT,则为cat文件。

即使它不是通用的,到目前为止,该解决方法仍可以为我解决所有情况。

2020-11-26