2011-08-16 122 views
11

我有一個可執行多個程序集的pom。當我跑步時,例如它運行所有的執行。我怎麼能告訴它只運行foo執行?Maven:如何指定運行哪個程序集插件執行

<build> 
    <plugins> 
     <plugin> 
      <artifactId>maven-assembly-plugin</artifactId> 
      <executions> 
       <execution> 
        <id>foo/id> 
        <phase>package</phase> 
        <goals><goal>single</goal></goals> 
        <configuration>...</configuration> 
       </execution> 
       <execution> 
        <id>bar</id> 
        <phase>package</phase> 
        <goals><goal>single</goal></goals> 
        <configuration>...</configuration> 
       </execution> 

上面我所是,在我的腦海裏,類似於以下Makefile

all: foo bar 

foo: 
    ... build foo ... 

bar: 
    ... build bar ... 

我可以運行make all或者乾脆make建立的一切,或者我可以運行make foomake bar到建立個人目標。我如何用Maven實現這一點?

回答

25

您需要使用profiles,這裏是一個pom.xml例如:

<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"> 
    <modelVersion>4.0.0</modelVersion> 

    <groupId>com.mycompany</groupId> 
    <artifactId>FooBar</artifactId> 
    <version>1.0</version> 
    <packaging>jar</packaging> 

    <profiles> 

     <profile> 
      <id>Foo</id> 
      <build> 
       <plugins> 
        <plugin> 
         <artifactId>maven-assembly-plugin</artifactId> 
         <executions> 
         <execution> 
          <id>foo/id> 
          <phase>package</phase> 
          <goals><goal>single</goal></goals> 
          <!-- configuration>...</configuration --> 
         </execution> 
         </executions> 
        </plugin> 
       </plugins> 
      </build> 
     </profile> 

     <profile> 
      <id>Bar</id> 
      <build> 
       <plugins> 
        <plugin> 
         <artifactId>maven-assembly-plugin</artifactId> 
         <executions> 
         <execution> 
          <id>Bar</id> 
          <phase>package</phase> 
          <goals><goal>single</goal></goals> 
          <!-- configuration>...</configuration --> 
         </execution> 
         </executions> 
        </plugin> 
       </plugins> 
      </build> 
     </profile> 

    </profiles> 

</project> 

而且你會調用的Maven這樣的:

mvn package -P Foo // Only Foo 
mvn package -P Bar // Only Bar 
mvn package -P Foo,Bar // All (Foo and Bar) 
8

我Maven是一個有點生疏,但我認爲你可以做到這幾個方面:

1)使用的配置文件。使用「maven -PprofileName」在命令行中指定配置文件。

2)把你的執行分爲不同的階段/目標,只運行你想要的。

2

如果您不想「bar」運行,那麼請不要將其綁定到生命週期階段。插件執行只在綁定到階段時運行,並且該階段作爲構建的一部分執行。正如The Coolah所建議的那樣,配置文件是管理何時執行與生命週期階段相關的一種方式,以及何時不執行。