【问题标题】:How to override default binding to phase of a Maven plugin如何覆盖默认绑定到 Maven 插件的阶段
【发布时间】:2012-10-26 08:48:51
【问题描述】:

我想在我的父 pom 的 pluginManagement 中为插件定义不同的执行,然后将特定的执行绑定到子 pom 中的阶段。 我看到不一致的行为取决于使用的插件和 pluginManagement 部分的位置。

在第一个示例中,pluginManagement 位于父 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>

在子 pom 上运行 'mvn clean install' 将运行编译器插件的执行和仅第一次执行 antrun 插件,尽管只有第一次执行每个都绑定到一个阶段。

现在将 pluginManagement 移动到子 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>
<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 给出了所需的行为,它只为每个插件运行第一次执行。 编译器插件(和大多数其他插件)仅在 pluginManagement 位于同一个 pom 中并且每次执行都绑定到 phase=none 的情况下才能正常工作(可能是因为它将执行绑定到默认阶段)。 antrun 插件在任何情况下都能正常工作。

如何在父 pom 中拥有 pluginManagement 部分且无需将不需要的执行专门绑定到子 pom 中的 phase=none 时实现这一点? 这是 Maven 中的错误还是这种行为在某种程度上是合理的? 我已经在 Maven 3.0.4 和 Maven 2.2.1 上尝试过,结果相同。

【问题讨论】:

  • 我尝试了您的第一个设置,但我只有 execution-1 用于编译器和 antrun 插件。您是说编译器插件同时运行execution-1execution-2。正如我所说,这并没有发生在我身上。
  • 你是对的。诀窍是将 phase=none 添加到默认绑定的插件中。添加之后,我没有将我的父 pom 部署到本地 repo,所以我尝试使用旧的父。

标签: maven


【解决方案1】:

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

大多数插件会将执行绑定到默认阶段。所以当一个插件的一次执行被触发时,所有未绑定的执行都会被绑定到默认阶段并且也会运行。

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

【讨论】:

    猜你喜欢
    • 2011-12-09
    • 2015-11-05
    • 2015-04-23
    • 1970-01-01
    • 2011-04-28
    • 2011-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多