2011-03-06 41 views
20

我應該在編譯階段運行Maven插件,所以在這個消耗我的插件項目,我必須做這樣的事情:如何將maven插件默認添加到階段?

<executions> 
<execution> 
    <phase>compile</phase> 
    <goals> 
    <goal>my-goal</goal> 
    </goals> 
</execution> 
</executions> 

我需要的是默認如果用戶已經包含我的插件(理想情況下上面的部分不需要,只是插件聲明),則將my-goal附加到編譯階段。

這可能嗎?

+0

你有沒有找到一種方法,以避免執行部分完全? – Zalumon 2017-07-31 11:07:13

回答

7

在您的Mojo classdef註釋中放入一個@phaseannotation

的醫生說:

@phase <phaseName> 

此批註指定默認階段爲實現這一目標。如果您將此目標的執行添加到pom.xml並且未指定階段,Maven將默認將目標綁定到此註釋中指定的階段。

如果這不起作用,我猜JIRA是有保證的。

+4

好的這個作品**部分**真的,我沒有必要明確'phase'部分,但我仍然需要把整個''廢話 – 2011-03-07 00:50:12

+7

哦,是的,肯定。爲了避免你需要一個自定義的生命週期。 – bmargulies 2011-03-07 00:56:25

-5

您將插件關聯到maven lifecyle goal。插件配置應在階段中聲明。

例如,如果你婉構建階段你需要做這樣的事情在運行某些插件:

<project> 
... 
... 
<build> 
    <plugin> 
     **Configuration of plugin** 
    </plugin> 
</build> 
</project> 

請務必仔細閱讀這裏的Maven生命週期(這是行家的基本理解): http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html

,然後閱讀有關如何配置插件:http://maven.apache.org/guides/mini/guide-configuring-plugins.html

PS在開始時進入maven的邏輯並不容易。但事後有獎勵。

+0

所以你的意思是我應該把' 2011-03-06 23:01:35

6

在插件中創建一個src \ main \ resources \ META-INF \ plexus \ components.xml實例。

在那裏爲您希望Mojo支持的工件類型創建一個LifeCycle映射。確保它列出了您想要支持的所有階段和插件。可能最好從maven-core.jar中複製。

然後將您的插件添加到您希望生成的階段的相應LifeCycle中。

例如消耗-AAR魔加入到編譯AAR生命週期的相。

<!-- Android archive (aar) support --> 
<component> 
    <role>org.apache.maven.lifecycle.mapping.LifecycleMapping</role> 
    <role-hint>aar</role-hint> 
    <implementation> 
    org.apache.maven.lifecycle.mapping.DefaultLifecycleMapping 
    </implementation> 
    <configuration> 
    <phases> 
     <generate-sources> 
     com.jayway.maven.plugins.android.generation2:android-maven-plugin:generate-sources 
     </generate-sources> 
     <process-resources>org.apache.maven.plugins:maven-resources-plugin:resources</process-resources> 
     <compile> 
     com.jayway.maven.plugins.android.generation2:android-maven-plugin:consume-aar, 
     org.apache.maven.plugins:maven-compiler-plugin:compile 
     </compile> 
0

這是可能的,但它是一個無證的maven功能。

使用此components.xml

<component-set> 
    <components> 
    <component> 
     <role>org.apache.maven.lifecycle.Lifecycle</role> 
     <implementation>org.apache.maven.lifecycle.Lifecycle</implementation> 
     <role-hint>myplugin</role-hint> 
     <configuration> 
      <id>accurest</id> 
      <phases> 
       <phase>my-plugin-not-used-phase</phase> 
      </phases> 
      <default-phases> 
       <compile> 
        my.package:my-plugin:${project.version}:my-goal 
       </compile> 
      </default-phases> 
     </configuration> 
    </component> 
</components> 

但你的插件需要與<extensions>true</extensions>被添加到修改現有的生命週期。

更多:How to bind plugin mojos (goals) to few phases of default lifecycle?

房地產項目:https://github.com/spring-cloud/spring-cloud-contract/blob/master/spring-cloud-contract-tools/spring-cloud-contract-maven-plugin/src/main/resources/META-INF/plexus/components.xml

相關問題