我想在我的父pom的pluginManagement中为插件定义不同的执行,然后将特定的执行绑定到子poms中的阶段。 我看到不一致的行为取决于使用的插件和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部分并且不必在child poms中将不需要的执行绑定到phase = none时如何实现? 这是Maven中的一个错误,还是这种行为在某种程度上是合理的? 我在Maven 3.0.4和Maven 2.2.1上尝试过这个结果。
答案 0 :(得分:8)
提供的示例正常工作。在包含修复程序后,我没有重新部署父级。
大多数插件会将执行绑定到默认阶段。因此,当触发一个插件执行时,所有未绑定的执行都将绑定到默认阶段,并且也将运行。
为避免此行为,父pom的pluginManagement部分中插件的所有执行都应绑定到phase = none(如提供的示例中所示)。这样,除非在子poms中显式覆盖阶段,否则不会执行任何执行。