【问题标题】:How to bind plugin mojos (goals) to few phases of default lifecycle?如何将插件 mojos(目标)绑定到默认生命周期的几个阶段?
【发布时间】:2016-08-25 01:03:39
【问题描述】:

我的custom maven plugin 有三个目标(mojos):

  • convert 分配给默认阶段:process-test-resources
  • generateStubs 分配给默认阶段:package
  • generateTests 分配给默认阶段:generate-test-sources

如何将这三个mojo绑定到default lifcycle phase,这样用户就可以简单地使用插件,无需特殊配置和对项目packaging进行任何更改?

用户只需添加:

<plugin>
    <groupId>io.codearte.accurest</groupId>
    <artifactId>accurest-maven-plugin</artifactId>
    <extensions>true</extensions>
</plugin>

而不是

<plugin>
    <groupId>io.codearte.accurest</groupId>
    <artifactId>accurest-maven-plugin</artifactId>
    <executions>
        <execution>
            <goals>
                <goal>convert</goal>
                <goal>generateStubs</goal>
                <goal>generateTests</goal>
            </goals>
        </execution>
    </executions>
</plugin>

我可以使用components.xml 来实现这一点,如下所示,但这需要一些丑陋的技巧(指定不存在的阶段 - ugly-fix),我不确定这个解决方案是否适用于所有情况。

<component-set>
    <components>
        <component>
            <role>org.apache.maven.lifecycle.Lifecycle</role>
            <implementation>org.apache.maven.lifecycle.Lifecycle</implementation>
            <role-hint>accurest</role-hint>
            <configuration>
                <id>accurest</id>
                <phases>
                    <phase>ugly-fix</phase> // plugin fail without this
                </phases>
                <default-phases>
                    <process-test-resources>
                        io.codearte.accurest:accurest-maven-plugin:${project.version}:convert
                    </process-test-resources>
                    <generate-test-sources>
                        io.codearte.accurest:accurest-maven-plugin:${project.version}:generateTests
                    </generate-test-sources>
                    <package>
                        io.codearte.accurest:accurest-maven-plugin:${project.version}:generateStubs
                    </package>
                </default-phases>
            </configuration>
        </component>
    </components>
</component-set>

这是正确的吗?进行此类配置是否有更好的方法?

更多信息:

【问题讨论】:

    标签: maven maven-3 maven-plugin


    【解决方案1】:

    我认为您正在寻找的是Mojo 注释的defaultPhase 属性,有关所有详细信息,请参阅https://maven.apache.org/components/plugin-tools/maven-plugin-tools-annotations/

    【讨论】:

    • 不,不是这样,OP 希望在不在 POM 中声明任何 &lt;execution&gt; 的情况下执行插件。 defaultPhase 需要执行。
    • 是的,这是第二件事.. OP 希望它与默认生命周期中的任何包装一起自动工作(不是他们自己的)。 :) (这就是我卡住的地方,无需从 maven 核心复制粘贴整个 default-bindings.xml
    • 不可能。而且我不建议调整生命周期,只需将其留给打包插件即可。简单地想象一下:如果每个构建插件都重写生命周期会发生什么?我很确定大多数 Maven 用户都知道如何将插件目标绑定到阶段,尤其是使用准备好的复制粘贴示例:)
    • @MᴀʀɪᴜsᴢS 在您的示例中,您有一个自定义包装,其生命周期涉及您的 3 个阶段(因此它按预期方式工作)。这与为默认生命周期的所有打包调用插件目标不同。
    • @MᴀʀɪᴜsᴢS 它没有,例如,如果您使用jar 包装进行测试并且您的components.xml 中有&lt;role-hint&gt;jar&lt;/role-hint&gt;,您会看到它们是唯一被调用的并且不调用普通jar 的其他默认阶段。
    【解决方案2】:

    您可以通过将ugly-fix 替换为&lt;phases&gt; 标签中的正确目标来实现:

    <component-set>
      <components>
        <component>
            <role>org.apache.maven.lifecycle.Lifecycle</role>
            <implementation>org.apache.maven.lifecycle.Lifecycle</implementation>
            <role-hint>accurest</role-hint>
            <configuration>
                <id>accurest</id>
                <phases>
                    <process-test-resources>
                        io.codearte.accurest:accurest-maven-plugin:${project.version}:convert
                    </process-test-resources>
                    <generate-test-sources>
                        io.codearte.accurest:accurest-maven-plugin:${project.version}:generateTests
                    </generate-test-sources>
                    <package>
                        io.codearte.accurest:accurest-maven-plugin:${project.version}:generateStubs
                    </package>
                </phases>
                <default-phases>
                    <process-test-resources>
                        io.codearte.accurest:accurest-maven-plugin:${project.version}:convert
                    </process-test-resources>
                    <generate-test-sources>
                        io.codearte.accurest:accurest-maven-plugin:${project.version}:generateTests
                    </generate-test-sources>
                    <package>
                        io.codearte.accurest:accurest-maven-plugin:${project.version}:generateStubs
                    </package>
                </default-phases>
            </configuration>
        </component>
    </components>
    

    【讨论】:

    • 这看起来仍然像 hack
    猜你喜欢
    • 1970-01-01
    • 2015-11-05
    • 2020-03-25
    • 2013-02-21
    • 1970-01-01
    • 2014-12-23
    • 2012-10-26
    • 1970-01-01
    • 2011-12-09
    相关资源
    最近更新 更多