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

发布于 2024-12-12 13:06:25 字数 231 浏览 0 评论 0原文

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

I am working on a big projects with many pom.xml files and I need to specify all the libraries that I use. This means that I need to read pom.xml files recursively and get groupId, artifactId, scope and version. I checked out mvn dependency:tree but I can't find a way to print it to a file in a readable format. I saw appendOutput but I saw no example on how to use it in cmd. I saw some solutions done in Linux but I only have access to Windows XP.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

说不完的你爱 2024-12-19 13:06:26

这可以(至少现在)通过 dependency:tree 插件的命令行选项来完成。

尝试:

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。

This can (at least now) be done with command line options to the dependency:tree plugin.

Try:

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

Reference: Maven Dependency Plugin Page

You only asked about "readable" format, but you can also pass the -DoutputType parameter with various options. Also note that the version I have installed, I get the following warning:

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

So, consider trying it with -DoutputFile=/path/to/file

Also, I was unable to get the -DoutputType paramater to give me anything other than the default text, but didn't have a chance to play around with it. YMMV.

日裸衫吸 2024-12-19 13:06:26

如果您在同一存储库/项目下有多个模块,并且希望将所有模块的依赖关系放在一个文件中,以便能够区分一个构建和另一个构建,以查看某处是否发生了更改,您可以执行

$project_dir> mvn dependency:tree -DoutputFile=<absolute_path_to_file> -DappendOutput=true

以下操作:

$project_dir> mvn dependency:tree -DoutputFile=`pwd`/mvn_dependency_tree.txt -DappendOutput=true

查看其他可用选项 https://maven.apache.org/插件/maven-dependency-plugin/tree-mojo.html

If you have multiple modules under the same repo/project and want the dependencies of all the modules in one file, so as to be able to diff b/w one build and another to see if something changed somewhere, you can do

$project_dir> mvn dependency:tree -DoutputFile=<absolute_path_to_file> -DappendOutput=true

e.g.

$project_dir> mvn dependency:tree -DoutputFile=`pwd`/mvn_dependency_tree.txt -DappendOutput=true

See other options available at https://maven.apache.org/plugins/maven-dependency-plugin/tree-mojo.html

陪你搞怪i 2024-12-19 13:06:26

添加

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

插件会生成一个 classes/META-INF/maven/dependency.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

Adding the

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

plugin produces a classes/META-INF/maven/dependencies.properties file with the project dependencies easily parseable.

Example of the output produced:

# 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
枯寂 2024-12-19 13:06:26

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

根据此网站(只是 Google 的最佳结果),似乎Windows 的控制台也使用 > 符号将输出流定向到文件。
那么你介意尝试一下吗?

On GNU/Linux I would just do mvn dependency:tree > myFile. However, if you're restricted to Windows only, than I would look for Windows' syntax for streaming the output of a command.

According to this site (just a top-results from Google) it seems that Windows' console also use > sign to direct the output stream to i.e. a file.
So would you mind trying this?

安稳善良 2024-12-19 13:06:26

我已运行以下命令并获取具有所有 Maven 依赖项的文件。

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

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

I have run the below command and got the file having all the maven dependency.

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

This command will create a folder named "temp" and inside this folder a file name mvn_dependency_tree.txt will be created with all the dependencies.

苍景流年 2024-12-19 13:06:26

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

You can always install MinGW and MSYS and then use the Linux examples using dependency:tree in Windows

舟遥客 2024-12-19 13:06:26

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

Perhaps effective-pom (in conjunction with some linux commands for saving the file) can be sufficient for your needs.

对你而言 2024-12-19 13:06:26

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

如果项目有多个模块并且您希望将依赖项输出到单个文件,请使用绝对路径和附加选项。

mvn dependency:tree -DoutputFile=/abs_path/deps.txt -DappendOutput=true

See https://maven.apache.org/plugins/maven-dependency-plugin/tree-mojo.html#outputFile

If the project has multi modules and you want to output the dependencies to a single file, use absolute path and append options.

mvn dependency:tree -DoutputFile=/abs_path/deps.txt -DappendOutput=true
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文