如何以可读格式将Maven依赖项打印到文件中?

时间:2011-10-31 12:46:06

标签: maven build-dependencies

我正在开发一个包含许多pom.xml文件的大型项目,我需要指定我使用的所有库。这意味着我需要递归读取pom.xml文件并获取groupId,artifactId,scope和version。我检查了mvn依赖:树但我找不到以可读格式将其打印到文件的方法。我看到appendOutput但我没有看到如何在cmd中使用它的示例。我在Linux中看到了一些解决方案,但我只能访问Windows XP。

7 个答案:

答案 0 :(得分:65)

这可以(至少现在)使用依赖项:tree plugin的命令行选项来完成。

尝试:

mvn dependency:tree -Doutput=/path/to/file

参考:Maven Dependency Plugin Page

您只询问了“可读”格式,但您也可以使用各种选项传递-DoutputType参数。另请注意我安装的版本,我收到以下警告:

[WARNING] The parameter output is deprecated. Use outputFile instead. 

因此,请考虑使用-DoutputFile = / path / to / file

进行尝试

另外,我无法让-DoutputType参数给我提供除默认文本以外的任何内容,但没有机会玩它。 YMMV。

答案 1 :(得分:4)

添加

<plugin>
    <groupId>org.apache.servicemix.tooling</groupId>
    <artifactId>depends-maven-plugin</artifactId>
</plugin>

插件生成一个classes/META-INF/maven/dependencies.properties文件,项目依赖项很容易解析。

产生的输出示例:

# Project dependencies generated by the Apache ServiceMix Maven Plugin
# Generated at: Mon Oct 10 17:43:00 CEST 2011

groupId = my.group.name
artifactId = my.artifact.name
version = 0.0.1-SNAPSHOT
my.group.name/my.artifact.name/version = 0.0.1-SNAPSHOT

# dependencies

junit/junit/version = 4.8
junit/junit/type = jar
junit/junit/scope = test

org.easymock/easymock/version = 2.4
org.easymock/easymock/type = jar
org.easymock/easymock/scope = test

答案 2 :(得分:4)

在GNU / Linux上,我只会做mvn dependency:tree > myFile。但是,如果您仅限于Windows,那么我会查找用于流式传输命令的Windows语法。

根据this site(只是谷歌的最高结果),似乎Windows的控制台也使用>符号将输出流定向到文件。 所以你介意试试吗?

答案 3 :(得分:3)

如果你在同一个repo /项目下有多个模块,并且希望所有模块的依赖关系在一个文件中,以便能够对一个构建进行差异化,而另一个构建可以在某个地方进行更改,那么你可以做

; FormatTime transforms a YYYYMMDDHH24MISS timestamp into the specified date/time format.
FormatTime, Date, CurrentDate, YYYYMMDD

Expires := 20170611  ; 06/12/2017

; Subtract Date timestamp from Expires timestamp
EnvSub, Expires, CurrentDate, days

; The result is stored in Expires
Msgbox % Expires " days left till  ..."

e.g。

tf.contrib.rnn.LSTMStateTuple(c, h)

请参阅https://maven.apache.org/plugins/maven-dependency-plugin/tree-mojo.html

中提供的其他选项

答案 4 :(得分:0)

您始终可以安装MinGW and MSYS,然后在Windows中使用dependency:tree使用Linux示例

答案 5 :(得分:0)

或许effective-pom(与用于保存文件的一些linux命令一起)可满足您的需求。

答案 6 :(得分:0)

我已经运行了以下命令,并获得了具有所有maven依赖项的文件。

mvn dependency:tree -DoutputFile=temp/mvn_dependency_tree.txt

此命令将创建一个名为“ temp”的文件夹,并将在该文件夹内创建具有所有依赖项的文件名mvn_dependency_tree.txt。