如何为 Maven 插件定义默认 mojo

发布于 2024-11-02 06:22:28 字数 1013 浏览 3 评论 0原文

我编写了一个插件,可以在 target/ generated-sources/ 中生成一个文件。 这个插件只有一个魔力。这个魔力是用以下内容声明的:

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

在项目中,我想使用该插件,但如果我不指定执行标签,它就不起作用:

<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中定义的...我的意思是,当使用jar插件时,我不必告诉我要执行的目标,在哪个阶段...它是自动的。

谢谢!

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 { 

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>

I would like to only configure the plugin like this :

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

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.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

真心难拥有 2024-11-09 06:22:28

让 Maven 插件在默认阶段执行时自动运行其默认目标是不可能的。这很令人困惑,因为许多针对特定包装的标准插件“绑定”。这些是在 Maven 核心中定义的: https://maven .apache.org/ref/3.6.1/maven-core/default-bindings.html

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

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

通过在插件中定义默认阶段,您不必指定它,只是目标。在您的情况下:

<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-specation.html(查找@phase)。相关引用(我的重点):

定义一个默认阶段来绑定mojo执行(如果用户这样做)
没有在 POM 中明确设置阶段。注意:此注释不会
添加插件声明时自动运行 mojo
POM。
它仅仅使用户能够省略该元素
周围的元素。

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

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>

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

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.

如梦亦如幻 2024-11-09 06:22:28

您需要将 META-INF/plexus/components.xml 文件添加到您的插件中,并在插件块中设置 true

请参阅11.6.3。覆盖默认生命周期(来自 Maven Book)以供参考

You need to add a META-INF/plexus/components.xml file to your plugin and set <extensions>true</extensions> in your plugin block.

See 11.6.3. Overriding the Default Lifecycle from the Maven Book for reference

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文