Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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 if the user has included my plugin already (ideally the above part wouldn't be necessary, just the plugin declaration).

Is this possible?

like image 479
Pablo Fernandez Avatar asked Mar 06 '11 21:03

Pablo Fernandez


People also ask

What is default phase in Maven plugin?

Given the lifecycle phases above, this means that when the default lifecycle is used, Maven will first validate the project, then will try to compile the sources, run those against the tests, package the binaries (e.g. jar), run integration tests against that package, verify the integration tests, install the verified ...

Where do I put Maven plugins?

Introduction. In Maven, there are two kinds of plugins, build and reporting: Build plugins are executed during the build and configured in the <build/> element. Reporting plugins are executed during the site generation and configured in the <reporting/> element.

Are Maven goals tied to phases?

Maven Build PhasesWhen we run a maven build command, we specify the phase to be executed. Any maven build phases that come before the specified phase is also executed. For example, if we run mvn package then it will execute validate, compile, test, and package phases of the project.


2 Answers

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.

like image 65
bmargulies Avatar answered Oct 04 '22 01:10

bmargulies


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> 
like image 43
William Avatar answered Oct 04 '22 03:10

William