如何默认将maven插件附加到阶段?

时间:2011-03-06 21:27:58

标签: java maven maven-plugin

我有一个应该在编译阶段运行的maven插件,所以在使用我的插件的项目中,我必须做这样的事情:

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

我需要的是默认情况下my-goal附加到编译阶段如果用户已经包含了我的插件(理想情况下,上面的部分不是必需的,只是插件声明)。

这可能吗?

4 个答案:

答案 0 :(得分:7)

在您的Mojo classdef注释中添加@phase annotation

医生说:

@phase <phaseName>

此注释指定此目标的默认阶段。如果将此目标的执行添加到pom.xml并且未指定阶段,Maven将默认将目标绑定到此批注中指定的阶段。

如果这不起作用,我猜JIRA是有道理的。

答案 1 :(得分:6)

在插件中创建src \ main \ resources \ META-INF \ plexus \ components.xml的实例。

在那里为您希望Mojo支持的工件类型创建LifeCycle映射。确保它列出了您要支持的所有阶段和插件。可能最好从maven-core.jar中复制一个。

然后将您的插件添加到您希望它们构建的阶段的相应LifeCycle中。

例如消耗aar Mojo已添加到 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>

答案 2 :(得分: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

答案 3 :(得分:-5)

您将插件与maven lifecyle goal相关联。插件配置应在阶段中声明。

例如,如果您想在 build 阶段运行一些插件,则需要执行以下操作:

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

请仔细阅读maven生命周期(这是了解maven的基础): http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html

然后阅读有关如何配置插件的信息:http://maven.apache.org/guides/mini/guide-configuring-plugins.html

P.S。进入maven逻辑并不容易。但事后才有回报。

相关问题