How to define a default mojo for a maven plugin

前端 未结 2 541
别那么骄傲
别那么骄傲 2020-12-17 20:23

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 :

/**
 *          


        
相关标签:
2条回答
  • 2020-12-17 21:02

    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.

    0 讨论(0)
  • 2020-12-17 21:17

    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

    0 讨论(0)
提交回复
热议问题