如何输出一个简单的 Maven 依赖列表

How to output a simple list of Maven dependencies

我正在尝试从我的 POM 中获取一个简单的、机器可解析的依赖项列表。如果我这样做:

mvn org.apache.maven.plugins:maven-dependency-plugin:2.1:list -f sample.pom

我得到了一堆毫无意义的 [INFO] 输出:

[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building Unnamed - my-group:my-project:jar:1.0
[INFO]    task-segment: [org.apache.maven.plugins:maven-dependency-plugin:2.1:list]
[INFO] ------------------------------------------------------------------------
[INFO] [dependency:list {execution: default-cli}]
[INFO] 
[INFO] The following files have been resolved:
[INFO]    com.squareup.picasso:picasso:jar:2.5.2:compile
[INFO]    commons-io:commons-io:jar:1.3.2:compile
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1 second
[INFO] Finished at: Mon Jan 11 14:06:05 GMT 2016
[INFO] Final Memory: 17M/325M
[INFO] ------------------------------------------------------------

然后我必须手动抓取以获取信息。如果我添加 -q 开关

mvn -q org.apache.maven.plugins:maven-dependency-plugin:2.1:list -f sample.pom

如果已经在本地,我会收到下载进度消息或什么也没有(当然除非出现错误)。

难道真的没有办法执行依赖列表命令,所以它只是输出一个简单的下载依赖列表?类似于:

> mvn org.apache.maven.plugins:maven-dependency-plugin:2.1:list -f sample.pom
    com.squareup.picasso:picasso:jar:2.5.2:compile
    commons-io:commons-io:jar:1.3.2:compile
> 

可以重定向 maven-dependency-plugin to a file with the help of the outputFile 属性的输出:

If specified, this parameter will cause the dependencies to be written to the path specified, instead of writing to the console.

mvn org.apache.maven.plugins:maven-dependency-plugin:2.1:list -f sample.pom -DoutputFile="..."

如果文件不存在,插件将创建该文件。如果已经存在,内容将被覆盖(但这可以通过appendOutput属性来控制)。

这应该会为您提供一个简单且可解析的依赖项列表。


作为 side-note,我注意到您使用的是非常旧的 maven-dependency-plugin 版本(2.1 is dated January 2009). The latest is 2.10

基于 Tunaki 的回答并与 this answer 结合使用 stdout 作为文件,我得到了这个:

> mvn -q org.apache.maven.plugins:maven-dependency-plugin:2.10:list \
  -f sample.pom -DoutputFile=>(cat)

The following files have been resolved:
   commons-io:commons-io:jar:1.3.2:compile
   com.squareup.picasso:picasso:jar:2.5.2:compile

>

不完美 - 但对我的目的来说是一个重大改进。