2011-03-06 111 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

相关问题