如何覆盖Maven插件默认绑定的阶段?

14

我想在父POM的插件管理中为插件定义不同的执行,并将特定的执行绑定到子POM中的阶段。

我发现插件的使用和插件管理部分的位置会导致不一致的行为。

在这个第一个例子中,插件管理位于父POM中,为编译器插件定义了2个执行和antrun插件的2个执行。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<name>master-pom</name>
<modelVersion>4.0.0</modelVersion>
<groupId>plugin.test</groupId>
<artifactId>master-pom</artifactId>
<packaging>pom</packaging>
<version>1.0.0-SNAPSHOT</version>
<build>
    <pluginManagement>
        <plugins>               
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <executions>
                    <execution>
                        <id>execution-1</id>
                        <phase>none</phase>
                        <goals>
                            <goal>compile</goal>
                        </goals>
                        <configuration>
                            <source>1.6</source>
                            <target>1.6</target>
                            <includes>
                                <include>**/*</include>
                            </includes>
                        </configuration>
                    </execution>
                    <execution>
                        <id>execution-2</id>
                        <phase>none</phase>
                        <goals>
                            <goal>compile</goal>
                        </goals>
                        <configuration>
                            <source>1.5</source>
                            <target>1.5</target>
                            <includes>
                                <include>**/*</include>
                            </includes>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-antrun-plugin</artifactId>
                <version>1.7</version>
                <executions>
                    <execution>
                        <id>execution-1</id>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <target>
                                <echo message="execution 1"/>
                            </target>
                        </configuration>
                    </execution>
                    <execution>
                        <id>execution-2</id>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <target>
                                <echo message="execution 1"/>
                            </target>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

        </plugins>
    </pluginManagement>
</build>

还有子POM:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<name>build</name>
<modelVersion>4.0.0</modelVersion>
<groupId>plugin.test</groupId>
<artifactId>build</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>pom</packaging>
<parent>
    <groupId>plugin.test</groupId>
    <artifactId>master-pom</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <relativePath>./master-pom.xml</relativePath>
</parent>
<build>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <executions>
                <execution>
                    <id>execution-1</id>
                    <phase>generate-sources</phase>
                </execution>
            </executions>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-antrun-plugin</artifactId>
            <executions>
                <execution>
                    <id>execution-1</id>
                    <phase>generate-sources</phase>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
在子模块上运行“mvn clean install”将运行编译器插件的两个执行和antrun插件的第一个执行,尽管每个插件的第一个执行都只绑定到了一个阶段。现在将插件管理移动到子模块:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<name>build</name>
<modelVersion>4.0.0</modelVersion>
<groupId>plugin.test</groupId>
<artifactId>build</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>pom</packaging>
<build>
    <pluginManagement>
        <plugins>               
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <executions>
                    <execution>
                        <id>execution-1</id>
                        <phase>none</phase>
                        <goals>
                            <goal>compile</goal>
                        </goals>
                        <configuration>
                            <source>1.6</source>
                            <target>1.6</target>
                            <includes>
                                <include>**/*</include>
                            </includes>
                        </configuration>
                    </execution>
                    <execution>
                        <id>execution-2</id>
                        <phase>none</phase>
                        <goals>
                            <goal>compile</goal>
                        </goals>
                        <configuration>
                            <source>1.5</source>
                            <target>1.5</target>
                            <includes>
                                <include>**/*</include>
                            </includes>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-antrun-plugin</artifactId>
                <version>1.7</version>
                <executions>
                    <execution>
                        <id>execution-1</id>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <target>
                                <echo message="execution 1"/>
                            </target>
                        </configuration>
                    </execution>
                    <execution>
                        <id>execution-2</id>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <target>
                                <echo message="execution 2"/>
                            </target>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

        </plugins>
    </pluginManagement>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <executions>
                <execution>
                    <id>execution-1</id>
                    <phase>generate-sources</phase>
                </execution>
            </executions>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-antrun-plugin</artifactId>
            <executions>
                <execution>
                    <id>execution-1</id>
                    <phase>generate-sources</phase>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

这个 POM 文件提供了所需的行为,即只运行每个插件的第一个 execution。编译器插件(以及大多数其他插件)只在 pluginManagement 部分位于同一 POM 中,并且每个 execution 绑定到 phase=none 的情况下才能正常工作(可能是因为它将 executions 绑定到默认阶段)。antrun 插件在任何情况下都能正常工作。

如何在父 POM 中具有 pluginManagement 部分并且不必在子 POM 中特别绑定不需要的 executions 到 phase=none 的情况下实现此目标?这是 Maven 中的一个 bug 还是某种程度上合理的行为?我已经在 Maven 3.0.4 和 Maven 2.2.1 上尝试过,并获得了相同的结果。


我尝试了你的第一个设置,编译器和antrun插件都只有execution-1。你说编译器插件同时运行了execution-1execution-2。但是,像我所说的那样,这并没有发生在我身上。 - maba
你是对的。诀窍是在默认绑定插件中添加phase=none。在我添加了这个之后,我没有将我的父POM部署到本地仓库,所以我一直在尝试使用旧的父POM。 - Rigas Grigoropoulos
1个回答

10

所提供的示例工作正常。在包含修复后,我没有重新部署父级。

大多数插件将执行绑定到默认阶段。因此,当触发一个插件的执行时,所有未绑定的执行都将绑定到默认阶段并运行。

为避免这种行为,父POM中插件管理部分的所有插件执行都应绑定到phase=none(如提供的示例所示)。这样,除非在子POM中明确覆盖该阶段,否则不会运行任何执行。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接