首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何覆盖Maven插件阶段的缺省绑定

如何覆盖Maven插件阶段的缺省绑定
EN

Stack Overflow用户
提问于 2012-11-06 19:48:01
回答 1查看 9.4K关注 0票数 13

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

在第一个示例中,pluginManagement位于父pom中,为编译器插件定义了2次执行,为antrun插件定义了2次执行。

代码语言:javascript
复制
<?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:

代码语言:javascript
复制
<?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插件的第1次执行,尽管每个插件的第1次执行都绑定到了一个阶段。

现在将pluginManagement移到子pom:

代码语言:javascript
复制
<?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中的一个bug,还是这种行为是合理的?我已经在Maven 3.0.4和Maven 2.2.1上进行了尝试,结果相同。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-11-07 18:56:17

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

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

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

票数 9
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13250377

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档