Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven: Relationship between Lifecycle Phase & Goal

Tags:

maven

apache

I'm having a tough time seeing the "forest through the trees" on some Maven concepts here. I understand that Maven comes preconfigured with a slew of so-called "Build Lifecycle Phases" that begin with validate, and test and end with deploy.

I am coming to Maven from Ant where you organized major build stages into targets (which can depend on other targets in a linear fashion), and then you decompose your targets into procedural tasks. Maven seems to supports this but in the form of goals.

My question (since I am used to thinking of building in terms of targets and tasks) is: how do these lifecycle phases (package, verify, etc.) relate to goals? Does one need to configured which goals to run at which phase, or is this done by Maven automagically somehow?

Or, do the goals come predefined with which phase they belong in?

I'm just not seeing how one orders goals to create a custom build that works for them or their organization.

Thanks in advance for any clarity!

like image 758
IAmYourFaja Avatar asked Nov 07 '11 15:11

IAmYourFaja


People also ask

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.

What are the Maven lifecycle phases?

Maven Lifecycle: Below is a representation of the default Maven lifecycle and its 8 steps: Validate, Compile, Test, Package, Integration test, Verify, Install and Deploy.

How does maven life cycle work?

A Build Lifecycle is Made Up of Phases validate - validate the project is correct and all necessary information is available. compile - compile the source code of the project. test - test the compiled source code using a suitable unit testing framework. These tests should not require the code be packaged or deployed.

What are the 3 build lifecycle of Maven?

There are three built-in lifecycles: default: the main lifecycle, as it's responsible for project deployment. clean: to clean the project and remove all files generated by the previous build. site: to create the project's site documentation.


1 Answers

Many Maven plugins bind specific goal(mojo) to a specific lifecycle phase.

E.g. look for documentation of maven-jar-plugin:jar goal. It says:

Binds by default to the lifecycle phase: package.

Also, lifecycle itself is specifying what tools need to be run for each phase. For default packaging types these tools are predefined by Maven and explained here -> http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html#Built-in_Lifecycle_Bindings.

But this example shows how to create totally custom lifecycle binding -> http://www.sonatype.com/people/2009/08/create-a-customized-build-process-in-maven/. As you can see you can bind any combination of plugin goals to a specific lifecycle phase.

Last, but not least, you can add plugin execution manually through the build/plugins element of your pom file. If you do that the plugins in specific phases will execute in a FIFO order.

Also, it may help you to see the Effective POM with all these defaults spelled out. Many IDE's offer this option for Maven projects, but you can also see effective pom from the command line:

mvn help:effective-pom
like image 91
Alexander Pogrebnyak Avatar answered Nov 14 '22 11:11

Alexander Pogrebnyak