如何将 Maven 依赖项以可读格式打印到文件中?
我正在开发一个包含许多 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
这可以(至少现在)通过 dependency:tree 插件的命令行选项来完成。
尝试:
参考: Maven Dependency Plugin Page
您只询问了“可读”格式,但您也可以通过各种选项传递 -DoutputType 参数。另请注意,在我安装的版本中,我收到以下警告:
因此,请考虑使用 -DoutputFile=/path/to/file 进行尝试
另外,我无法获取 -DoutputType 参数来给我除默认文本之外的任何内容,但没有机会玩它。 YMMV。
This can (at least now) be done with command line options to the dependency:tree plugin.
Try:
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:
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.
如果您在同一存储库/项目下有多个模块,并且希望将所有模块的依赖关系放在一个文件中,以便能够区分一个构建和另一个构建,以查看某处是否发生了更改,您可以执行
以下操作:
查看其他可用选项 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
e.g.
See other options available at https://maven.apache.org/plugins/maven-dependency-plugin/tree-mojo.html
添加
插件会生成一个
classes/META-INF/maven/dependency.properties
文件,其中项目依赖项可轻松解析。产生的输出示例:
Adding the
plugin produces a
classes/META-INF/maven/dependencies.properties
file with the project dependencies easily parseable.Example of the output produced:
在 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?
我已运行以下命令并获取具有所有 Maven 依赖项的文件。
此命令将创建一个名为“temp”的文件夹,并在该文件夹内创建一个包含所有依赖项的文件名 mvn_dependency_tree.txt。
I have run the below command and got the file having all the maven dependency.
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.
您始终可以安装 MinGW 和 MSYS,然后使用
dependency:tree
来使用 Linux 示例在Windows中You can always install MinGW and MSYS and then use the Linux examples using
dependency:tree
in Windows也许 effective-pom (与一些用于保存文件的 linux 命令)足以满足您的需求。
Perhaps effective-pom (in conjunction with some linux commands for saving the file) can be sufficient for your needs.
请参阅 https://maven.apache.org/plugins /maven-dependency-plugin/tree-mojo.html#outputFile
如果项目有多个模块并且您希望将依赖项输出到单个文件,请使用绝对路径和附加选项。
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.