2012-11-06 47 views
9

我想爲我的父pom的pluginManagement中的插件定義不同的執行,然後將特定的執行綁定到子poms中的階段。 根據所使用的插件和pluginManagement部分的位置,我發現行爲不一致。如何覆蓋默認綁定到階段的Maven插件

在第一個示例中,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> 

運行 '命令mvn乾淨安裝' 對孩子POM將運行編譯器插件的兩次執行,只有第一個執行的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給出了運行時只爲每個插件1日執行所需的行爲。 只有在pluginManagement位於同一pom且每個執行都綁定到phase = none(可能是因爲它將執行綁定到默認階段)的情況下,編譯器插件(以及大多數其他)才能正常工作。 antrun插件在任何情況下都能正常工作。

如何在父pom中擁有pluginManagement部分的情況下實現此目的,而無需特別將不需要的執行綁定到子poms中的phase = none? 這是一個在Maven中的錯誤,或者這種行爲有點合理嗎? 我已經在Maven 3.0.4和Maven 2.2.1上試了這個,得到了相同的結果。

+0

我嘗試了你的第一個設置,並且我只對編譯器和antrun插件擁有'execution-1'。你在說編譯器插件同時運行了'execution-1'和'execution-2'。正如我所說,這並沒有發生在我身上。 – maba

+0

你說得對。訣竅是在默認綁定的插件中添加phase = none。我在添加完成後沒有將父母pom部署到本地回購,因此我嘗試使用舊的父母。 –

回答

4

提供的示例工作正常。包括修復程序後,我沒有重新部署父母。

大多數插件會將執行綁定到默認階段。所以當一個插件的執行被觸發時,所有未綁定的執行都將被綁定到默認階段,並且也會運行。

爲避免此行爲,父pom的pluginManagement部分中插件的所有執行都應綁定到phase = none(如所示的示例中所示)。這種方式不會執行,除非在子poms中顯式覆蓋階段。