Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I execute a set of goals before my Maven plugin runs?

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?

like image 363
talk to frank Avatar asked Sep 15 '09 15:09

talk to frank


People also ask

How do I run a specific goal in Maven?

Run a Maven goal from the context menu In the Maven tool window, click Lifecycle to open a list of Maven goals. Right-click the desired goal and from the context menu select Run 'name of the goal'. IntelliJ IDEA runs the specified goal and adds it to the Run Configurations node.

What actions are executed if you call the Maven goal package?

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.

Are Maven goals tied to phases?

In Maven the "verbs" are goals packaged in Maven plugins which are tied to a phases in a build lifecycle. A Maven lifecycle consists of a sequence of named phases: prepare-resources, compile, package, and install among other. There is phase that captures compilation and a phase that captures packaging.

Which of the following are correct Maven goals?

A Maven plugin is a group of goals; however, these goals aren't necessarily all bound to the same phase. As we can see, the Failsafe plugin has two main goals configured here: integration-test: run integration tests. verify: verify all integration tests passed.


1 Answers

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

like image 113
Rich Seller Avatar answered Nov 07 '22 09:11

Rich Seller