通过STDOUT中的脚本可以获得干净的机器友好的Maven线/ xml / json输出?

例如,对于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 (而不是像上面那样无法可靠地解析的文本输出)。

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

Maven使用标准的slf4j日志记录,包裹在丛集容器中。 https://maven.apache.org/maven-logging.html

您应该能够配置slf4j绑定以提供不同的输出格式,详见http://logback.qos.ch/manual/layouts.html#log4jXMLLayout

我不熟悉cleanlines和json日志记录格式; 但是,slf4j可能是更具适应性的日志包之一,所以请寻找一个Formatter,你有机会找到一个。 但是,如果不这样做,上面的最后一个链接也会涵盖有关如何编写自己的日志记录布局的页面。

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

例如, -DoutputFile或-Doutput :

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

如果需要STDOUT,请cat文件。

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