26

Note regarding the accepted answer: I accepted the answer because of strong circumstantial evidence. Nonetheless, this is circumstantial evidence, so take it with a grain of salt.


How can I have a plugin be triggered when the user runs a plugin goal, not a lifecycle phase? (This has been asked before, but the answer was to use a lifecycle phase.)

Case in point: I need release:branch to invoke regex-plugin to generate a branch with the current version as its name, minus the -SNAPSHOT suffix. This is what I have, which requires the developer to activate a profile and invoke the verify phase. I need the developer to simply invoke release:branch, which in turn should cause regex-plugin to run. In a bit of a marriage to Gitflow.

<profile>
    <id>Release Branch</id>
    <build>
        <plugins>
            <!-- On validate, compute the current version without -SNAPSHOT. -->
            <!-- Put the result in a property. -->
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>build-helper-maven-plugin</artifactId>
                <version>1.7</version>
                <executions>
                    <execution>
                        <phase>validate</phase>
                        <goals>
                            <goal>regex-property</goal>
                        </goals>
                        <configuration>
                            <value>${project.version}</value>
                            <regex>^(.*)-SNAPSHOT$</regex>
                            <replacement>$1</replacement>
                            <name>project.unqualifiedVersion</name>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <!-- Also on validate, run the branch plugin, and use -->
            <!-- the non-SNAPSHOT version thus computed in the branch name. -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-release-plugin</artifactId>
                <version>2.3.2</version>
                <executions>
                    <execution>
                        <phase>validate</phase>
                        <goals>
                            <goal>branch</goal>
                        </goals>
                        <configuration>
                            <branchName>release/${project.unqualifiedVersion}</branchName>
                            <updateWorkingCopyVersions>true</updateWorkingCopyVersions>
                            <updateBranchVersions>false</updateBranchVersions>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</profile>

The intent is for release:branch to move the current snapshot version (say, 1.0.5-SNAPSHOT) into a new branch, which should be named after the version but without the superfluous -SNAPSHOT suffix (1.0.5). The current branch should then take on a new snapshot version (1.1.0-SNAPSHOT, not 1.0.6-SNAPSHOT, because we want release 1.0.x to have room for hotfixes, so we reserve it for the branch) (I don't have the automatic computation of the next snapshot version figured out yet, so, if you run the Maven configuration above with validate, you will have to enter it at a prompt).

4

2 回答 2

8

不,您不能将一个插件绑定到另一个插件。只到一个阶段。

在 Maven 内部术语中,“Mojo”是可以工作的东西。“插件”是封装的 mojo 集合,因此您可以从 POM 中引用它们。Mojos 仅绑定到阶段。

插件开发文档

在插件描述符中指定的每个 Mojo 必须提供以下内容

...

phase ... 如果用户未在 POM 中显式设置阶段,则定义将 mojo 执行绑定到的默认阶段。注意:当插件声明添加到 POM 时,此注释不会自动运行 mojo。它仅使用户能够<phase>从周围元素中省略该<execution>元素。

如需进一步确认,请参阅MojoExecution的源代码(该类的JavaDoc没有帮助)并注意枚举了两个可能的执行源:

源自从 CLI 直接调用目标的执行

源自绑定到生命周期阶段的目标的执行

没有其他启动执行的方法意味着您不走运(除非采取特殊措施,例如推出您自己的插件,该插件结合了您要链接的两个插件的效果,然后使用您的自定义插件)。

于 2013-05-07T17:30:14.307 回答
8

到目前为止提供的证据是相当间接的。我自己做了一些研究,所以最好在这里分享一下。以下是更多相同的“不可能”,或者是替代方案的构建块。

jetspeed:mvn plugin --- 运行指定的插件序列;要运行的配置可以通过系统属性进行更改;IDE 集成问题


在插件运行之前执行目标 (StackOverflow) --- 在自定义 Mojo 的上下文中回答了相同的问题


让 Mojo 运行其他目标 (StackOverflow) --- 再次,来自自定义 Mojo 的上下文


配置默认 Mojo 执行--- 描述 Mojos 如何运行的 Maven 页面 - 更多间接证据


在目标执行之前触发阶段(StackOverflow) ---我的问题的迂回解决方案,不幸的是回答是否定的


有趣Ant 插件开发指南——吸引我,因为虽然它需要编写自定义插件,但都是 Ant + Maven 配置,无需编译代码;可能进入门槛较低


创建一个并行的生命周期——吸引人的方法,因为我可以完全控制生命周期的内容到它使用 Gitflow 动词的位置;不清楚 IDE 将如何集成它;存在学习曲线和采用障碍问题

于 2013-05-08T04:39:29.540 回答