I'm writing a Maven plugin (Mojo) that needs to execute a standard set of other plugin executions before it is run.
Is there a mechanism to declare all the goals within my plugin so I don't have to rely on the user defining them all in their POM?
I'm writing a Maven plugin (Mojo) that needs to execute a standard set of other plugin executions before it is run.
Is there a mechanism to declare all the goals within my plugin so I don't have to rely on the user defining them all in their POM?
You can do this by defining a custom lifecycle and invoking that lifecycle before your Mojo is executed via the execute
annotation.
In your Mojo, declare in the Javadoc the lifecycle to be executed:
/**
* Invoke the custom lifecycle before executing this goal.
*
* @goal my-goal
* @execute lifecycle="my-custom-lifecycle" phase="process-resources"
*/
public class MyMojo extends AbstractMojo {
...
Then define a custom lifecycle in src/main/resources/META-INF/maven/lifecycle.xml.
The lifecycle is a bit like plexus' components.xml, but allows you to specify configuration for those goals.
Note the syntax is slightly different to plugin configurations in the pom. You define a goal using : as a separator rather than specifying separate groupId, artifactId and version elements, otherwise it is largely the same notation as the execution element of a plugin configuration in the pom. You can even use some properties in the lifecycle.xml (though possibly not all properties are supported, I'll need to check that).
The following example invokes the dependency plugin twice with different configurations in the process-resources phase:
<lifecycles>
<lifecycle>
<id>download-dependencies</id>
<phases>
<phase>
<id>process-resources</id>
<executions>
<execution>
<goals>
<goal>
org.apache.maven.plugins:maven-dependency-plugin:copy-dependencies
</goal>
</goals>
<configuration>
<includeScope>compile</includeScope>
<includeTypes>war</includeTypes>
<overWrite>true</overWrite>
<outputDirectory>
${project.build.outputDirectory}/wars
</outputDirectory>
</configuration>
</execution>
<execution>
<goals>
<goal>
org.apache.maven.plugins:maven-dependency-plugin:copy-dependencies
</goal>
</goals>
<configuration>
<includeScope>compile</includeScope>
<includeTypes>jar</includeTypes>
<overWrite>true</overWrite>
<outputDirectory>
${project.build.outputDirectory}/jars
</outputDirectory>
</configuration>
</execution>
</executions>
</phase>
</phases>
</lifecycle>
</lifecycles>
With this approach, the dependency plugin will be invoked once with each configuration in the process-resources
phase of the forked lifecycle (all happening within the execution defined in the Mojo).
In the lifecycle.xml, you can define multiple phases, and multiple executions per phase of the lifecycle. The available phases are defined in the Maven lifecycle.
You can find out more about lifecycles in the Creating a Custom Lifecycle section of the Maven book. It doesn't give an exhaustive list of what is allowed though. The only other reference I know if is from the Maven 2 alpha, so is possibly not that up-to-date