开发者

How to attach a maven plugin to a phase by default?

开发者 https://www.devze.com 2023-02-15 07:58 出处:网络
I have a maven plugin that should run in the compile phase, so in the project that consumes my plugin, I have to do something like this:

I have a maven plugin that should run in the compile phase, so in the project that consumes my plugin, I have to do something like this:

<executions>
 <execution>
  <phase>compile</phase>
  <goals>
   <goal>my-goal</goal>
  </goals>
 </execution>
</executions>

What I need is to by default attach my-goal to the compile phase 开发者_C百科if the user has included my plugin already (ideally the above part wouldn't be necessary, just the plugin declaration).

Is this possible?


Put an @phase annotation in your Mojo classdef annotations.

The doc says:

@phase <phaseName>

This annotation specifies the default phase for this goal. If you add an execution for this goal to a pom.xml and do not specify the phase, Maven will bind the goal to the phase specified in this annotation by default.

If this doesn't work, I guess a JIRA is warranted.


Create an instance of src\main\resources\META-INF\plexus\components.xml in your plugin.

In there create a LifeCycle mapping for the artifact types that your want your Mojo to support. Make sure that it lists all the phases and plugins you want to support. Probably best to copy from the one from maven-core.jar.

Then add your plugin in to the appropriate LifeCycle(s) at the phase at which you want them built.

Eg the consume-aar Mojo added into the compile phase of the aar lifecycle.

<!--  Android archive (aar) support -->
<component>
  <role>org.apache.maven.lifecycle.mapping.LifecycleMapping</role>
  <role-hint>aar</role-hint>
  <implementation>
    org.apache.maven.lifecycle.mapping.DefaultLifecycleMapping
  </implementation>
  <configuration>
    <phases>
      <generate-sources>
        com.jayway.maven.plugins.android.generation2:android-maven-plugin:generate-sources
      </generate-sources>
      <process-resources>org.apache.maven.plugins:maven-resources-plugin:resources</process-resources>
      <compile>
        com.jayway.maven.plugins.android.generation2:android-maven-plugin:consume-aar,
        org.apache.maven.plugins:maven-compiler-plugin:compile
      </compile>


This is possible, but it is an undocumented maven feature.

Use this components.xml:

<component-set>
  <components>
    <component>
        <role>org.apache.maven.lifecycle.Lifecycle</role>
        <implementation>org.apache.maven.lifecycle.Lifecycle</implementation>
        <role-hint>myplugin</role-hint>
        <configuration>
            <id>accurest</id>
            <phases>
                <phase>my-plugin-not-used-phase</phase>
            </phases>
            <default-phases>
                <compile>
                    my.package:my-plugin:${project.version}:my-goal
                </compile>
            </default-phases>
        </configuration>
    </component>
</components>

but your plugin need to be added with <extensions>true</extensions> to modify existing lifecycle.

More: How to bind plugin mojos (goals) to few phases of default lifecycle?

Real project: https://github.com/spring-cloud/spring-cloud-contract/blob/master/spring-cloud-contract-tools/spring-cloud-contract-maven-plugin/src/main/resources/META-INF/plexus/components.xml


You associate plugin to maven lifecyle goal. The plugin configuration should be declared in phase.

For example if you wan to run some plugin during build phase you'll need to do something like this :

<project>
...
...
 <build>
   <plugin>
      **Configuration of plugin**
   </plugin>
 </build>
 </project>

Please read carefully about maven lifecycles here (it is fundamental for understanding of maven): http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html

And then read about how to configure a plugin : http://maven.apache.org/guides/mini/guide-configuring-plugins.html

P.S. Getting into logic of maven is not easy at the beginning. But it is rewarding afterwards.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号