Maven:将插件执行绑定到另一个插件的执行,而不是生命周期阶段 [英] Maven: Bind plugin execution to the execution of another plugin, not to a lifecycle phase

查看:108
本文介绍了Maven:将插件执行绑定到另一个插件的执行,而不是生命周期阶段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于接受的答案的说明:由于有确凿的间接证据,我接受了该答案.尽管如此,这只是间接证据,因此请带一点盐.

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.)

要点:我需要release:branch调用regex-plugin来生成一个以当前版本为名称的分支减去-SNAPSHOT后缀.这就是我所拥有的,这要求开发人员激活配置文件并调用verify阶段.我需要开发人员简单地调用release:branch,这又将导致regex-plugin运行.与Gitflow有点婚姻.

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>

release:branch的目的是将当前快照版本(例如,1.0.5-SNAPSHOT)移动到新分支中,该分支应以该版本命名,但没有多余的-SNAPSHOT后缀(1.0.5).然后,当前分支应该采用新的快照版本(1.1.0-SNAPSHOT,而不是1.0.6-SNAPSHOT,因为我们希望发行版本1.0.x留有修补程序的空间,因此我们将其保留给分支)(我没有自动快照版本).计算出了下一个快照版本,因此,如果使用validate运行上面的Maven配置,则必须在提示符下输入它.

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).

推荐答案

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

No, you can't bind to a plugin to another plugin. Only to a phase.

用Maven内部术语来说,"Mojo"是有效的东西. 插件"是包装的Mojo的集合,因此您可以从POM中引用它们. Mojos仅绑定到阶段.

In Maven-internal terms, a "Mojo" is the thing that does work. A "plugin" is a collection of mojos wrapped up so you can reference them from the POM. Mojos bind to phases only.

插件开发文档:

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

Each Mojo specified inside a plugin descriptor must provide the following

...

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

phase ... Defines a default phase to bind a mojo execution to if the user does not explicitly set a phase in the POM. Note: This annotation will not automagically make a mojo run when the plugin declaration is added to the POM. It merely enables the user to omit the <phase> element from the surrounding <execution> element.

有关进一步的确认信息,请参见

For further confirmation, see the source of MojoExecution (the JavaDoc for this class isn't helpful) and notice that there are two possible sources of execution enumerated:

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

An execution that originates from the direct invocation of a goal from the CLI

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

An execution that originates from a goal bound to a lifecycle phase

没有其他方法可以启动执行程序,这意味着您很不走运(除非采取非常规措施,例如滚动自己的插件以结合要链接的两个插件的效果,然后再使用自定义插件).

No other way to kick off an execution means you're out of luck (barring extraordinary measures like rolling your own plugin that combines the effects of the two plugins you want to link and then using your custom plugin).

这篇关于Maven:将插件执行绑定到另一个插件的执行,而不是生命周期阶段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆