2009-09-15 30 views

回答

14

您可以通過定義自定義生命週期並在通過execute批註執行Mojo之前調用該生命週期來完成此操作。

在你的Mojo,在Javadoc聲明要執行的生命週期:

/** 
* Invoke the custom lifecycle before executing this goal. 
* 
* @goal my-goal 
* @execute lifecycle="my-custom-lifecycle" phase="process-resources" 
*/ 
public class MyMojo extends AbstractMojo { 
... 

然後定義中的src/main /資源/ META-INF /行家/ lifecycle.xml一個自定義的生命週期。

生命週期有點像plexus的components.xml,但允許您爲這些目標指定配置。

請注意,語法與pom中的插件配置略有不同。您使用以下方法定義一個目標:作爲分隔符而不是指定單獨的groupId,artifactId和版本元素,否則它與pom中的插件配置的執行元素基本相同。你甚至可以在lifecycle.xml中使用一些屬性(儘管可能不是所有的屬性都支持,我需要檢查它)。

下面的示例與進程中的資源相不同的配置調用的依賴性插件兩次:

<lifecycles> 
    <lifecycle> 
    <id>download-dependencies</id> 
    <phases> 
     <phase> 
     <id>process-resources</id> 
     <executions> 
      <execution> 
      <goals> 
       <goal> 
       org.apache.maven.plugins:maven-dependency-plugin:copy-dependencies 
       </goal> 
      </goals> 
      <configuration> 
       <includeScope>compile</includeScope> 
       <includeTypes>war</includeTypes> 
       <overWrite>true</overWrite> 
       <outputDirectory> 
       ${project.build.outputDirectory}/wars 
       </outputDirectory> 
      </configuration> 
      </execution> 
      <execution> 
      <goals> 
       <goal> 
       org.apache.maven.plugins:maven-dependency-plugin:copy-dependencies 
       </goal> 
      </goals> 
      <configuration> 
       <includeScope>compile</includeScope> 
       <includeTypes>jar</includeTypes> 
       <overWrite>true</overWrite> 
       <outputDirectory> 
       ${project.build.outputDirectory}/jars 
       </outputDirectory> 
      </configuration> 
      </execution> 
     </executions> 
     </phase> 
    </phases> 
    </lifecycle> 
</lifecycles> 

通過這種方法,依賴插件將被用在叉形的process-resources相位每個配置調用一次生命週期(全部發生在Mojo定義的執行中)。

在lifecycle.xml中,可以定義多個階段以及生命週期的每個階段的多個執行。可用階段在Maven lifecycle中定義。

您可以在Maven書籍的Creating a Custom Lifecycle部分找到關於生命週期的更多信息。儘管如此,它並沒有給出詳盡的清單。唯一的其他參考我知道如果是從Maven 2 alpha,所以可能不是最新的

+0

謝謝這看起來應該工作。你知道我在哪裏可以找到在lifecycle.xml中允許哪些內容? – 2009-09-15 15:13:04

+0

我已經添加了一些引用到答案的結尾。我不知道規範的引用,雖然 – 2009-09-15 15:39:59

+0

存在Maven的Lifecycle.xml引用[here](http://maven.apache.org/ref/2.2.1/maven-plugin-descriptor/lifecycle-mappings.html),儘管它沒有比例子提供的更多的信息。 – prunge 2012-03-13 04:02:41

相關問題