You are previewing Maven: The Definitive Guide.

Maven: The Definitive Guide

Cover of Maven: The Definitive Guide by Sonatype Company Published by O'Reilly Media, Inc.

Plugins and the Maven Lifecycle

In Chapter 10, you learned that lifecycles can be customized by packaging types. A plugin can both introduce a new packaging type and customize the lifecycle. In this section, you will learn how you can customize the lifecycle from a custom Maven plugin. You will also see how you can tell a Mojo to execute a parallel lifecycle.

Executing a Parallel Lifecycle

Let’s assume you write some goal that depends on the output from a previous build. Maybe the ZipMojo goal can run only if there is output to include in an archive. You can specify something like a prerequisite goal by using the @execute annotation on a Mojo class. This annotation will cause Maven to spawn a parallel build and execute a goal or a lifecycle in a parallel instance of Maven that isn’t going to affect the current build. Maybe you wrote some Mojo that you can run once a day that runs mvn install and then packages up all of the output in some sort of customized distribution format. Your Mojo descriptor could tell Maven that before you execute your CustomMojo, you’d like it to execute the default lifecycle up to the install phase and then expose the results of that project to your Mojo as the property ${executedProject}. You could then reference properties in that project before some sort of postprocessing.

Another possibility is that you have a goal that does something completely unrelated to the default lifecycle. Let’s consider something completely unexpected. Maybe you have a goal that ...

The best content for your career. Discover unlimited learning on demand for around $1/day.