maven插件配置继承(嵌套元素的合并策略)

bre*_*mri 2 java maven-plugin maven

我读过这个和那个。除了一些措辞差异外,它们的内容基本相同。后者有一句话我想指出:

配置元素根据元素名称递归合并,...

前者只是说:

默认行为是根据元素名称合并配置元素的内容。如果子 POM 具有特定元素,则该值将成为有效值。如果子 POM 没有元素,但父 POM 有,则父值将成为有效值。

我的问题是当配置元素是嵌套元素时。假设在我的父 pom.xml 中,我有:

<build>
<plugins>
<plugin>
<executions>
  <execution>
    <id>enforce-maven</id>
    <goals>
      <goal>enforce</goal>
    </goals>
    <configuration>
        <rules>
            <bannedDependencies>
                <excludes>
                    <exclude>com.my.group:my.id</exclude>
                </excludes>
            </bannedDependencies>
        </rules>
    </configuration>
    ...
Run Code Online (Sandbox Code Playgroud)

然后在我的孩子 pom 中,我有:

<build>
<plugins>
<plugin>
<executions>
  <execution>
    <id>enforce-maven</id>
    <goals>
      <goal>enforce</goal>
    </goals>
    <configuration>
        <rules>
            <bannedDependencies>
                <includes>
                    <include>com.my.group:my.id</include>
                </includes>
            </bannedDependencies>
        </rules>
    </configuration>
    ...
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,我正在尝试include父级 bomexclude编辑的依赖项。由于我尝试在此处合并的元素(<excludes>和<includes>)位于内部</rules>/</bannedDependencies>,因此我不知道是否会得到:

<build>
<plugins>
<plugin>
<executions>
  <execution>
    <id>enforce-maven</id>
    <goals>
      <goal>enforce</goal>
    </goals>
    <configuration>
        <rules>
            <bannedDependencies>
                <excludes>
                    <exclude>com.my.group:my.id</exclude>
                </excludes>
                <includes>
                    <include>com.my.group:my.id</include>
                </includes>
            </bannedDependencies>
        </rules>
    </configuration>
    ...
Run Code Online (Sandbox Code Playgroud)

或者只是子 pom 指定的内容(换句话说,父 pom 中的嵌套元素被子 pom 中的元素替换)。

gjo*_*anv 7

您将同时获得includes和excludes。您可以通过运行来验证这一点mvn help:effective-pom。强制执行器插件将包含视为排除的例外,因此排除然后包含相同的工件应该具有不被禁止的效果(尽管我还没有测试过这一点)。

当我对配置继承有疑问时,我想象maven从最里面的元素和向外递归地合并配置元素,所以在这种情况下,元素bannedDependencies既includes从子元素获取元素,又从excludes父元素获取元素。如果您不希望子元素继承该bannedDependencies元素的任何内容,可以在子 pom 中执行以下操作:

<bannedDependencies combine.self="override">
...
</bannedDependencies>
Run Code Online (Sandbox Code Playgroud)