Get clean machine-friendly Maven lines/xml/json ou

2019-02-28 07:09发布

For example, run this command with for a maven project:

mvn dependency:list

What I need from Maven is only these two lines (cut out from the output below):

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

Is there a way (CLI --option) to see only this requested info in clean lines, xml, json, ...?

Instead, the output looks more like an unstructured log. It has no known format and mixes all types of information together in 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

Alternatively, I would accept solution to use Maven API to get runtime data like Collection<String> (instead of the text output like above which cannot be parsed reliably).

I took a look at Maven Invoker API, but I don't have hope - my tests showed that it's just a way to invoke Maven from code. And these APIs return no runtime data (just error code with all useful information printed in logs again).

2条回答
乱世女痞
2楼-- · 2019-02-28 07:42

Maven uses standard slf4j logging, wrapped in a plexus container. https://maven.apache.org/maven-logging.html

You should be able to configure the slf4j bindings to provide different output formats as detailed here http://logback.qos.ch/manual/layouts.html#log4jXMLLayout

I'm not familiar with the cleanlines and json logging formats; however, slf4j is probably one of the more adaptable logging packages, so look around for a Formatter and you have a chance at finding one. However, if you don't the last link above also covers the pages on how to write your own logging layout.

查看更多
Bombasti
3楼-- · 2019-02-28 07:46

I was able to supply a property to save required clean output into a file.

For example, -DoutputFile or -Doutput:

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

If STDOUT is required, cat the file.

Even though it's not generic, so far, the workaround solves all cases for me.

查看更多
登录 后发表回答