如何为Maven插件定义默认Mojo [英] How to define a default mojo for a maven plugin

查看:284
本文介绍了如何为Maven插件定义默认Mojo的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个插件,可以在target/generate-sources/中生成一个文件. 该插件只有一个mojo.此Mojo声明如下:

I've written a plugin that generate one file in target/generated-sources/. This plugin only has one mojo. This mojo is declared with the following :

/**
 * @goal convertsql
 * @phase generate-sources
 * @requiresProject
 */
public class ConverterMojo extends AbstractMojo { 

在项目中,我想使用插件,但是如果我没有指定executions标签,它将不起作用:

In the project, i want to use the plugin but it doesn't work if i don't specify the executions tag :

<executions>
    <execution>
        <id>convert</id>
        <goals><goal>convertsql</goal></goals>
        <phase>generate-sources</phase>
    </execution>
</executions>

我只想这样配置插件:

<plugin>
    <groupId>com.my.plugins</groupId>
    <artifactId>sqlconverter</artifactId>
    <version>1.0-SNAPSHOT</version>
    <configuration>
        <sourceFile>src/main/resources/sql/schema_oracle.sql</sourceFile>
    </configuration>
</plugin>

是否可以为我的插件指定默认的mojo?默认目标和阶段是在mojo中定义的...我的意思是,当使用jar插件时,我不必告诉我要执行的目标,在哪个阶段...这是自动的.

Is it possible to specify the default mojo for my plugin ? The default goal and phase are defined in the mojo... I mean, when using the jar plugin, i don't have to tell the goal i want to execute, at which phase... it is automatic.

谢谢!

推荐答案

当无法执行Maven插件的默认阶段时,它会自动运行其默认目标.这很令人困惑,因为 有很多用于特定包装的标准插件绑定" .这些定义在Maven核心中: https://maven .apache.org/ref/3.6.1/maven-core/default-bindings.html

Having your Maven plugin automatically run its default goal when its default phase executes is not possible. This is confusing because there are a lot of standard plugin ‘bindings’ for specific packagings. Those are defined in Maven core: https://maven.apache.org/ref/3.6.1/maven-core/default-bindings.html

例如,对于WAR包装,它是:

For example, for WAR packaging it is:

<phases>
  <process-resources>
    org.apache.maven.plugins:maven-resources-plugin:2.6:resources
  </process-resources>
  <compile>
    org.apache.maven.plugins:maven-compiler-plugin:3.1:compile
  </compile>
  <process-test-resources>
    org.apache.maven.plugins:maven-resources-plugin:2.6:testResources
  </process-test-resources>
  <test-compile>
    org.apache.maven.plugins:maven-compiler-plugin:3.1:testCompile
  </test-compile>
  <test>
    org.apache.maven.plugins:maven-surefire-plugin:2.12.4:test
  </test>
  <package>
    org.apache.maven.plugins:maven-war-plugin:2.2:war
  </package>
  <install>
    org.apache.maven.plugins:maven-install-plugin:2.4:install
  </install>
  <deploy>
    org.apache.maven.plugins:maven-deploy-plugin:2.7:deploy
  </deploy>
</phases>

通过在插件中定义默认阶段,您无需指定目标,而只需指定目标.就您而言:

By defining a default phase in your plugin you won’t have to specify that, just the goal. In your case:

<executions>
    <execution>
        <id>convert</id>
        <!--
           Not needed for default phase of plugin goal:
           <phase>generate-sources</phase>
        -->
        <goals>
            <goal>convertsql</goal>
        </goals>
    </execution>
</executions>

另请参见 https://maven.apache.org/developers/mojo -api-specification.html (查找@phase).相关报价(我的重点):

Also see https://maven.apache.org/developers/mojo-api-specification.html (look for @phase). The relevant quote (my emphasis):

定义默认阶段,以将mojo执行绑定到用户执行的操作 没有在POM中明确设置阶段.注意:此注释不会 将插件声明添加到后,自动运行mojo .它仅使用户能够从中省略元素 周围的元素.

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 element from the surrounding element.

这篇关于如何为Maven插件定义默认Mojo的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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