如何显示 Maven POM 层次结构?
我正在编写一些脚本,我需要获取任何给定 pom 的所有父 pom 的列表。依赖插件似乎只对 pom 的依赖部分中列出的依赖项感兴趣,但似乎没有办法显示父 pom,这也是 Maven 工作所需的依赖项。
我错过了一些基本的东西吗?
I'm doing some scripting and I need to get a list of all the parent poms for any given pom. The dependency plugin seems to be only interested in the dependencies that are listed in the dependency section of the pom, but there doesn't seem to be a way to show the parent poms, which are also required dependencies for Maven to work.
Am I missing something basic?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
没有简单的 Maven 命令可以向您显示 pom.xml 的父 POM 链。原因是这不是一个人们通常会问的常见问题(更多内容见下文)。对于您的脚本,您只需解析 pom.xml 文件,获取父工件坐标,获取工件的 pom.xml 文件,然后解析它的 pom.xml 文件(并重复)。抱歉,但我知道没有捷径,但是其他人已经解决了类似的问题。
技术上你是对的,父 pom 是项目的依赖项,但它不是字面上的 Maven 依赖项,并且处理方式完全不同。父 pom 链、活动配置文件、您的
settings.xml
文件以及安装目录中的 Maven super pom 全部组合在一起,以创建项目的有效 pom 。有效的 POM 才是 Maven 真正用来完成其工作的东西。所以基本上,在依赖插件(或任何其他插件)被激活之前,父 pom 继承链已经被解析和组合。大多数人通常会问的问题是“当 Maven 完成所有内容的组合后,我的 REAL pom.xml 到底是什么样子?”或“我的父 pom 继承链的结果是什么?”或“我的 pom.xml 属性如何受到活动配置文件的影响?”有效的 pom 会告诉你这一切。
我知道您没有问,但对于阅读本文的其他人来说,如果您想查看父 pom.xml,只需在 M2Eclipse POM 编辑器中打开 pom.xml,然后单击概述选项卡上的父工件链接即可。通过这种方式,您只需单击每个 pom,即可快速在 pom.xml 文件链中向上移动。如果项目有超过 3 或 4 个继承的父 pom,那将是一个奇怪的项目。
如果你想查看有效的pom,可以运行命令
mvn help: effective-pom
。或者,您可以单击 M2Eclipse 的 POM 编辑器中的“Effective POM”选项卡。There is no simple Maven command that will show you the chain of parent POMs for a pom.xml. The reason for this is that it is not a common question one would typically ask (more on that below). For your script, you'll just have to parse the pom.xml file, get the parent artifact coordinates, get a hold of the artifact's pom.xml file and then parse it's pom.xml file (and repeat). Sorry, but there is no short cut I know of, but other folks have solved similar problems.
You are right that technically the parent pom is a dependency of your project, but it is not a literal Maven Dependency and is handled completely differently. The chain of parent poms, along with active profiles, your
settings.xml
file, and the Maven super pom from the installation directory are all combined together to create your project's effective pom. The effective POM is what Maven really uses to do its work. So basically, the parent pom inheritance chain is already resolved and combined before the dependency plugin (or any other plugin) is even activated.The questions most people typically ask is 'what does my REAL pom.xml really look like when Maven is done combining everything?' or 'What is the result my inheritance chain of parent poms?' or 'How are my pom.xml properties affected by an active profile?' The effective pom will tell you all of this.
I know you didn't ask, but for others reading this, if you want to see your parent pom.xml, simply open up the pom.xml in the M2Eclipse POM editor and click on the parent artifact link on the overview tab. In this way you can quickly move up the chain of pom.xml files with just a single click per pom. It would be a strange project that had more than 3 or 4 parent poms of inheritance.
If you want to see your effective pom, you can run the command
mvn help:effective-pom
. Alternatively, you can click on the Effective POM tab in M2Eclipse's POM editor.基本解决方案
如果您的项目定义了 3.1 或更高版本,您可以使用:
输出看起来类似于:
[INFO] Ancestor POMs: org.springframework.boot:spring-boot-starter-parent: 1.4.0.RELEASE <- org.springframework.boot:spring-boot-dependency:1.4.0.RELEASE
改进的解决方案
hierarchy-maven-plugin(我写的)可以显示有关导入的 pom 的附加信息,如下所示:
详细信息在这里:https://github.com/ExampleDriven/hierarchy-maven-plugin
Basic solution
If your project defines version 3.1 or later you can use:
The output looks similar to:
[INFO] Ancestor POMs: org.springframework.boot:spring-boot-starter-parent:1.4.0.RELEASE <- org.springframework.boot:spring-boot-dependencies:1.4.0.RELEASE
Improved solution
The hierarchy-maven-plugin (that I wrote) can display additional information about imported poms like this :
Details are here : https://github.com/ExampleDriven/hierarchy-maven-plugin