IAmYourFaja
IAmYourFaja

Reputation: 56904

Maven: Relationship between Lifecycle Phase & Goal

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!

Upvotes: 16

Views: 5523

Answers (2)

Sugan
Sugan

Reputation: 455

Maven has 3 lifecycles which are inturn executed in many phases (package, verify, etc). When these phases are executed, it calls the previous phases as well.

Goals are individual tasks. When it is called, that particular task is executed. Check the below blog for more detailed explanation. https://techytopics.wordpress.com/maven-simplified/

Upvotes: 0

Alexander Pogrebnyak
Alexander Pogrebnyak

Reputation: 45576

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

Upvotes: 11

Related Questions