java-获取干净的机器友好的Maven行/ xml / json输出,可通过STDOUT中的脚本解析?
作者:互联网
例如,对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] ------------------------------------------------------------------------
UPDATE
或者,我会接受使用Maven API来获取运行时数据(例如Collection< String> (而不是无法可靠解析的上述文本输出).
我看了Maven Invoker API,但我没有希望-测试表明,这只是从代码中调用Maven的一种方式.这些API不会返回运行时数据(只是错误代码,所有有用的信息会再次打印在日志中).
解决方法:
Maven使用标准slf4j日志记录,包装在plexus容器中. https://maven.apache.org/maven-logging.html
您应该能够配置slf4j绑定以提供不同的输出格式,如此处http://logback.qos.ch/manual/layouts.html#log4jXMLLayout所述
我对清理线和json记录格式不熟悉;但是,slf4j可能是适应性更强的日志记录软件包之一,因此,环顾一下Formatter,您就有机会找到一个.但是,如果没有,上面的最后一个链接还将介绍如何编写自己的日志记录布局的页面.
标签:java,maven,maven-plugin 来源: https://codeday.me/bug/20191011/1894161.html