Maven: Lifecycle vs. Phase vs. Plugin vs. Goal [closed]
Asked Answered
W

7

129

Relatively new developer here, even though I've been using it for a little while, I'm hoping to solidify my Maven fundamentals. Part of my problem is that I have no experience with Ant, which seems to be from where many explanations stem. I've been reading and watching tutorials, and I keep hearing the same terms:

  • Lifecycle
  • Phase
  • Plugin
  • Goal

From what I've learned, it seems that lifecycle is the broadest of the bunch, and is composed of (or completed by) phases, plugins, and/or goals.

Question: Could you provide any info on how these terms are related and the most common examples?

The more explicit and basic, the better!

Worthen answered 28/10, 2014 at 12:1 Comment(3)
maven.apache.org/guides/introduction/…Fractionize
So, to clarify, build lifecycle=lifecycle, of which there are three types: default, clean, and site? Other explanations had me thinking the there was a fourth lifecycle called build.Worthen
possible duplicate of What is the difference/relation between maven goals and phases?That
S
92

A Maven lifecycle is an (abstract) concept that covers all steps (or better: all the steps the Maven designers decided to support) that are expected to occur in a project's development lifetime. These steps (or stages) are called phases in Maven terminology.

A Maven plugin is a container for/supplier of goals. Code implemented in goals is the real workhorse. (Maven in its core itself is just managing plugins and executing goals). Each of a plugin's goals can be assigned/bound to any of the lifecycle phases.

When invoking mvn <phase> Maven passes all phases (every time) and executes all goals (supplied by plugins) that have been bound to any of the phases prior and up to (and including) the given phase. If there is a phase with no goal bound to it nothing is done. But the phase is passed nevertheless.

I.e. you can't "'insert' additional phases" into one of Maven's built-in lifecycles (clean, default, site). They are already there, always! You could develop your own lifecycle with its own phases but that's far beyond simply using Maven as it is.

Goals can also be executed directly, which you get told when running mvn without any phase or [plugin:]goal:

[with line breaks, [NB: notes] and shortened for readability here]

You must specify a valid lifecycle phase or a goal in the format

<plugin-prefix>:<goal> or

<plugin-group-id>:<plugin-artifact-id>[:<plugin-version>]:<goal>.

Available lifecycle phases are:

[NB: Clean lifecycle] pre-clean, clean, post-clean

[NB: Default lifecycle] validate, initialize,
  generate-sources, process-sources,
  generate-resources, process-resources,
  compile, process-classes,
  generate-test-sources, process-test-sources,
  generate-test-resources, process-test-resources,
  test-compile, process-test-classes, test,
  prepare-package, package,
  pre-integration-test, integration-test, post-integration-test,
  verify, install, deploy

[NB: Site lifecycle] pre-site, site, post-site, site-deploy

...

See also actual output of mvn or Maven, Introduction to the Build Lifecycle, also at References below.

References
Silkworm answered 20/6, 2015 at 12:17 Comment(0)
B
45

Maven: Lifecycle vs. Phase vs. Plugin vs. Goal

Answering late just to clarify yet another level of granularity missing in this thread: executions (of a goal), which are the smallest units of a Maven build.

Hence, we have build cycles (basically, set of actions for a specific overall goal), which consist of phases (lower granularity, a cycle step), which can invoke a set of configured goals provided by certain plugins. That is, Maven is (also) a plugin executor, each plugin can offer one or more goals. You then (also) decide which goal is attached to which phase, most of the times in the defaul life cycle (without any, that is, the default). But you can actually have yet another level: executions (of the same goal, from the same plugin, or of different goals from different plugins)

A picture I prepared to resume the whole enter image description here

And indeed this is how Maven shows it (its smallest unit of work) via the unique string in its build log:

plugin-artifactId:plugin-version:plugin-goal (goal-execution-id) @ project-name

For example, we would have:

[INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) @ sample-project ---

Which indeed means (through different levels of granularity):

  • During the compile phase (unfortunately only mentioned with mvn -X ... for debugging, under REACTOR BUILD PLAN – Tasks: [...]) →
  • I'm invoking the Maven Compiler plugin (specified by artifactId and version) with its compile goal →
  • as defined by the execution with the id default-compile.

It's unique because indeed you could have the same goal (of the same plugin) bound to different phases or to the same phase but in different executions (that is, with different configurations). The maven-compiler-plugin, for instance, is also used during the test-compile phase (a different phase) to compile test code (via its testCompile goal) in a different execution (default-testCompile). You could also compile (using the same plugin and goal) some auto-generated code during a different phase as defined by an execution you specified int the POM (and potentially a different configuration).

Default executions are provided out-of-the-box via Maven packaging bindings, that is, by default (and enforcing convention over configuration) Maven already invokes certain goals (of standard plugins) during certain phases. The executions ids of these default invocations are defined according to certain conventions.

This also explains why if you really want to override a default behavior (binding) of a Maven build, you need to specify (override) exactly the same execution id in your POM for the same plugin. You could, for instance, skip compilation simply defining an execution of the maven-compiler-plugin with the same default-compile id but bound to a non existing phase (or an empty one).

To make it short: an execution tells Maven which goal(s) to execute with which configuration within which phase.

Some executions are provided by default (defaul bindings), which explains why the maven minimal pom of just 6 lines can already do a lot (compile, test, package, etc.): executing goals of standard plugins in certain phases: it's convention over configuration. Then, via the pom.xml configuration you can add stuff (executions) to the build or influence the behavior of already configured plugins (in this case no executions section, but just configuration would be enough).

Yes, you could skip build cycles (and their phases) and directly invoke goals (of plugins). Imagine the following:

mvn compiler:compile
mvn compiler:testCompile
mvn surefire:test
mvn jar:jar

(NOTE: you could also invoke inline in only one call)

Here we are compiling app code, test code, execute tests and package: imagine how manual, error-prone, repetitive and time-consuming this would be. Convention over configuration helps us: Maven introduces build life cycles and phases. The default life cycle (with no name, that is, the default), provides a range of phases based on best practices and conventions (the mantra of Maven).
If you want to achieve the same as above, simply run: mvn package and it will automatically compile, test and package your project. How? invoking plugins. That is, phases are meaningful and configurable set of plugins (goals) executions. To make it even more standard, for each phase Maven will firstly invoke any preceeding phase, so that e.g. if you want to test you'll be sure you firstly compile.

p.s. note that when specifying several goals for the same execution, you will still see clearly in the build log two different executions (with the same id) for the two different goals (hence, still unique tuple).

Bluma answered 12/3, 2016 at 23:9 Comment(0)
C
30

And belatedly another diagram

  • Lifecycles as yellow rectangles
  • Phases of lifecycles as blue rectangles with "callable" phases in darker blue (i.e. the phases with hypenation are not usually called from the command line as they may not be designed to leave the project in a well-defined state).
  • Goals as blue lozenges. The association/binding "phase -> goal" shown is the one of the "jar" packaging mode. Every phase can have goals bound to it. This holds of course for each of the lifecycles, although bindings are only show for the "default" lifecycle.
  • Plugins as grey clipped rectangles. Plugins provide the Goals that can be bound to the Phases.

Maven Lifecycles, Phases, Goals, Plugins

Calvities answered 3/9, 2017 at 22:51 Comment(6)
The graphml file (edited with the free yEd editor) is available at github.com/dtonhofer/diagramsCalvities
1) What do you exactly mean by „"callable" phases“ that are "in darker blue"? Every Maven phase is "callable" (though I rather call it invokable, since there's no code called directly by invoking a phase). Or do you call phases "callable" that have a goal bound to it (by default)? Not even that is true, if you look at validate, initialize and verify.Silkworm
2) The resources:[testR|r]esources goal is NOT bound to the process-sources or process-test-sources phases in the jar lifecycle.Silkworm
3) modello:javaof the Modello Plugin apparently is domain-specific. Binding a plugin's goal to a phase holds for any of the phases.Silkworm
@GeroldBroser Fixed according to comments. "Callable" means that one can call it from the command line and expect to have the project remain in a valid state. There is no meaningful distinction between calling and invoking and to call is what the Maven Introduction uses.Calvities
I'd reverse the arrows of the goal bindings, too, since goals from plugins are bound to phases.Silkworm
M
22

Credit to Sandeep Jindal and Premraj (from here What are Maven goals and phases and what is their difference?). Their explanation help me to understand.

I created some full code examples & some simple explanations here https://www.surasint.com/maven-life-cycle-phase-and-goal-easy-explained/ . I think it may help others to understand and can try something directly.

In short from the link, You should not try to understand all three at once, first you should understand the relationship in these groups:

  • Life Cycle vs Phase
  • Plugin vs Goal

1. Life Cycle vs Phase

Life Cycle is a collection of phase in sequence, see here Life Cycle References. When you call a phase, it will also call all phases before it.

For example, the clean life cycle has 3 phases (pre-clean, clean, post-clean).

mvn clean

It will call pre-clean and clean.

2. Plugin vs Goal

Goal is like an action in Plugin. So if plugin is a class, goal is a method.

you can call a goal like this:

mvn clean:clean

This means "call the clean goal, in the clean plugin" (Nothing relates to the clean phase here. Don't let the word"clean" confusing you, they are not the same! See full explanation in my link above)

3. Now the relation between Phase & Goal:

Phase can (pre)links to Goal(s).For example, normally, the clean phase links to the clean goal. So, when you call this command:

mvn clean

It will call the pre-clean phase and the clean phase which links to the clean:clean goal.

It is almost the same as:

mvn pre-clean clean:clean
Managua answered 4/1, 2017 at 14:42 Comment(3)
@ 2. & 3. IMHO, clean:clean is not the best choice for an example. There are 4 items named clean (lifecycle, phase, plugin, goal) which may be confusing, especially for beginners (I remember that it was for me at the beginning). @ 3. The verb "link" is not a good choice either, IMHO. The offical Maven term is "bind".Silkworm
@GeroldBroser. Completely agree with clean:clean. I had explained and warned that in my full explanation in the link. I will copy that warnings to here too. The reason that I used it because it is good to let people know about this confusing thing word and, especially, the official maven document is using it and that explains it clearly. And yes, it was confusing me too. Anyway, thanks a lot for commentsManagua
typo: the official maven document is using it and does not explains it clearlyManagua
H
16

Sourced from this really good tutorial:

Lifecycles, Lifecycle Phases, Plugins and Plugin Goals are the core of Maven.

  • The Maven command mvn only accepts Lifecycle Phases or Plugin Goals as arguments.
  • Maven comes with three lifecycles: default, clean and site.
  • Each lifecycle is made up of lifecycle phases and in all, there are 28 phases – 21 in default (validate, ..., compile, ..., package, ..., install, deploy), 3 in clean (pre-clean, clean, post-clean) and 4 in site (pre-site, site, post-site, site-deploy).
  • When a lifecycle phase is invoked using the mvn command, all preceding phases are executed sequentially one after another.
  • lifecycle phases by themselves don’t have the capability to accomplish any task, and they rely on plugins to carry out the task.
  • depending on the project and packaging type, Maven binds various plugin goals to lifecycle phases and goals carry out the task entrusted to them.

When we run mvn package in a Java Project, Maven binds plugin goals to lifecycle phases as shown in the figure below:

mvn-plugins-package-goal

Hephzibah answered 14/3, 2017 at 20:24 Comment(3)
The material you've mentioned is pretty good. Thank you!Gingham
@"The Maven command mvn can accept only Lifecycle Phase or Plugin Goal as argument." is not correct. It accepts options as well.Silkworm
"When we run "mvn package" in a Java Project, Maven binds plugin goals to lifecycle phases" is not true. Goal binding happens long before running mvn ...: In default-bindings.xml or in a POM and it's not done by Maven but by a human.Silkworm
F
7

So to explain a little further as outlined here

Maven builds are split in life-cycles these are:

  • clean
  • build (default)
  • site

Each of this cycles is split into phases. For instance build is split into phases like:

  • prepare resources
  • compile
  • package
  • install

Phases have goals to run prior pre- or after post- a phase, for instance:

  • pre-clean - will be executed prior the clean phase
  • post-clean - will be executed after the clean phase

You can view goals as additional "inserted" phases if you like. Read up here or take a look at @Gerolds answer for details.

Fractionize answered 28/10, 2014 at 18:27 Comment(3)
This answer isn't correct in its entirety. See my answer.Silkworm
O boy 3 years since you have answered this question ... and still not letting it go .. you won ... now move on.Fractionize
It's not about winning. Don't you revise old questions, answers and comments if you stumble across them later on?Silkworm
F
4

LifeCycle vs Phases: Life Cycle is a collection of phases. When you call a phase, it will also call all phases that come before it. Maven comes with 3 built-in build life cycles as:

  1. Clean lifecycle- this involves cleaning of the project (for a fresh build & deployment)
  2. Default/build lifecycle- this handles the complete deployment of the project
  3. Site lifecycle- this handles generating the java documentation of the project. enter image description here

Clean lifecycle has 3 phases: pre-clean, clean and post-clean. Default and site lifecycles' phases are same as shown in the picture.

Fabio answered 22/9, 2017 at 8:27 Comment(3)
Your last paragraph is misleading. Especially the first and the last sentence. Goals and phases are totally different things. You must not confuse them because some of them have identical names. Re "Goals are the phases you see in the picture above.": There is not a single goal mentioned in the picture. These are all phases. Re "You write the phase name as 'goal' when you have to perform certain goal.": While it is possible to run a plugin's goal explicitely the usual way is to perform a build up to a certain phase with mvn <phase>. See my answer herein.Silkworm
Thanks, I have removed "Plugin vs Goal" part. I will update it soon.Fabio
@1. "... & deployment" – Deploying isn't a necessity and deployment also is a subset of a build. If you look at the picture there are many more phases to stop a build before the deploy phase. @2. "handles the complete deployment" – Same as before: The default lifecycle handles a build of a project. Deployment is just one, the last phase.Silkworm

© 2022 - 2024 — McMap. All rights reserved.