gpt4 book ai didi

maven-2 - Maven2 - pluginManagement 和父子关系的问题

转载 作者:行者123 更新时间:2023-12-03 14:49:49 26 4
gpt4 key购买 nike

来自 maven documentation

pluginManagement: is an element that is seen along side plugins. Plugin Management contains plugin elements in much the same way, except that rather than configuring plugin information for this particular project build, it is intended to configure project builds that inherit from this one. However, this only configures plugins that are actually referenced within the plugins element in the children. The children have every right to override pluginManagement definitions.



现在:如果我在我的父 POM 中有这个
  <build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.0</version>
<executions>
Some stuff for the children
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>

我在父项目上运行 mvn help: Effective-pom 我得到了我想要的东西,即直接在 build 下的插件部分(做工作的那个)仍然是空的。

现在,如果我执行以下操作:
  <build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.0</version>
<executions>
Some stuff for the children
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<inherited>true</inherited>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>

mvn help:effective-pom 我又得到了我想要的东西,插件只包含声明的东西,pluginManagement 部分被忽略。

但随着以下变化
  <build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.0</version>
<executions>
Some stuff for the children
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.0</version>
<inherited>false</inherited> <!-- this perticular config is NOT for kids... for parent only -->
<executions>
some stuff for adults only
</execution>
</executions>
</plugin>
</plugins>
</build>

并运行 mvn help:effective-pom
pluginManagement 部分的内容被添加到已经声明的内容之上。像这样 :
  <build>
<pluginManagement>
...
</pluginManagement>
<plugins>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.0</version>
<inherited>false</inherited> <!-- this perticular config is NOT for kids... for parent only -->
<executions>
Some stuff for the children
</execution>
<executions>
some stuff for adults only
</execution>
</executions>
</plugin>
</plugins>
</build>

有没有办法从父 pom 的部分中排除 child 的部分?实际上,我想要的是 pluginManagement 的行为与文档所述完全相同,即我希望它仅适用于 child ,而不适用于声明它的项目。

作为一个推论,有没有一种方法可以通过在项目的正常构建部分声明插件来覆盖 pluginManagement 中的部分?无论我尝试什么,我都会发现该部分已添加到执行中,但我无法覆盖已经存在的部分。

编辑:

我从未为此找到可接受的解决方案,因此问题仍然存在。下面提供了最接近的解决方案,目前是此问题的公认解决方案,直到出现更好的解决方案。现在有三种方法可以实现所需的结果(根据当前 POM 在继承层次结构中的位置调整插件行为):

1 - 使用配置文件,它会起作用,但您必须注意配置文件不是继承的,这有点违反直觉。它们(如果被激活)应用于声明的 POM,然后这个生成的 POM 向下传播。因此,激活子 POM 配置文件的唯一方法是专门在命令行上(至少我没有找到其他方法)。属性、文件和其他激活方式无法激活 POM,因为触发器不在声明配置文件的 POM 中。

2 -(这就是我最终要做的)声明该插件不是在父级中继承的,并在每个需要它的子级中重新声明(复制粘贴)该花絮。不理想,但它很简单并且有效。

3 - 拆分父 POM 的聚合性质和父性质。然后,由于仅适用于父级的部分位于不同的项目中,因此现在可以按照最初的预期使用 pluginManagement。然而,这意味着必须创建一个新的人工项目,它不会对最终产品做出贡献,而只会服务于 can 系统。这是一个明显的概念流血案例。此外,这仅适用于我的具体情况并且难以概括,因此我放弃了尝试进行这项工作的努力,转而支持 2 中描述的不漂亮但包含更多内容的剪切和粘贴补丁。

如果有人遇到这个问题有更好的解决方案,要么是因为我对 Maven 缺乏了解,要么是因为该工具的发展允许这样做,请在此处发布解决方案以供将来引用。

谢谢大家的帮助 :-)

最佳答案

将插件配置添加到 pluginManagement 意味着如果插件被声明将使用此配置,但您仍然需要在任何想要使用它的 POM 的构建部分中声明插件。

从您引用的部分中解释这一点的关键部分是:

However, this only configures plugins that are actually referenced within the plugins element in the children



因此,如果您在子 pom 中执行此操作,则将应用来自父 pom 的配置:
<build>
<plugins>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
</plugin>
</plugins>
</build>

更新:为了回答实际问题, pluginManagement 部分的内容总是与任何插件声明合并。为了避免父级这样做,您可以在配置文件中定义 pluginManagement 部分,并在子项目上激活该配置文件,而不是在父项目上。然后子项目必须声明该配置文件。

例如:
<profiles>
<profile>
<id>for-children</id>
<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.0</version>
<executions>
<!--Some stuff for the children-->
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
</profile>
</profiles>
<build>
<plugins>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.0</version>
<inherited>false</inherited> <!-- this perticular config is NOT for kids... for parent only -->
<!--some stuff for adults only-->
</executions>
</plugin>
</plugins>
</build>

关于maven-2 - Maven2 - pluginManagement 和父子关系的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1266226/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com