14

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?

1 Answer 1

17

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

4
  • thanks this looks like it should work. Do you know where I can find out what content is allowed in the lifecycle.xml? Sep 15, 2009 at 15:13
  • I've added some references to the end of the answer. I don't know of a canonical reference though Sep 15, 2009 at 15:39
  • Maven's Lifecycle.xml reference exists here, though it doesn't have much more information than the example provides.
    – prunge
    Mar 13, 2012 at 4:02
  • Can I run an entire default phase (install) before my own phase? Mar 29, 2023 at 15:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.