How to define a default mojo for a maven plugin

2020-02-10 22:51发布

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!

2条回答
该账号已被封号
2楼-- · 2020-02-10 23:25

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.

查看更多
地球回转人心会变
3楼-- · 2020-02-10 23:31

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

查看更多
登录 后发表回答