13

親 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>

そして子ポン:

<?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 でこれを試しましたが、結果は同じでした。

4

1 に答える 1

9

提供された例は正しく動作します。修正を含めた後、親を再デプロイしていませんでした。

ほとんどのプラグインは、実行をデフォルト フェーズにバインドします。したがって、プラグインの 1 つの実行がトリガーされると、バインドされていないすべての実行がデフォルト フェーズにバインドされ、実行されます。

この動作を回避するには、親 pom の pluginManagement セクションでのプラグインのすべての実行を phase=none にバインドする必要があります (提供された例に示すように)。このようにして、フェーズが子 poms で明示的にオーバーライドされない限り、実行は実行されません。

于 2012-11-07T10:56:17.283 に答える