What is the difference/relation between Maven goals and phases? How they are related to each other?
The definitions are detailed at the Maven site's page Introduction to the Build Lifecycle, but I have tried to summarize:
Maven defines 4 items of a build process:
Lifecycle
Three built-in lifecycles (aka build lifecycles): default
, clean
, site
. (Lifecycle Reference)
Phase
Each lifecycle is made up of phases, e.g. for the default
lifecycle: compile
, test
, package
, install
, etc.
Plugin
An artifact that provides one or more goals.
Based on packaging type (jar
, war
, etc.) plugins' goals are bound to phases by default. (Built-in Lifecycle Bindings)
Goal
The task (action) that is executed. A plugin can have one or more goals.
One or more goals need to be specified when configuring a plugin in a POM. Additionally, in case a plugin does not have a default phase defined, the specified goal(s) can be bound to a phase.
Maven can be invoked with:
clean
, package
)dependency:copy-dependencies
)<plugin-group-id>:<plugin-artifact-id>[:<plugin-version>]:<goal>
(e.g. org.apache.maven.plugins:maven-compiler-plugin:3.7.0:compile
)with one or more combinations of any or all, e.g.:
mvn clean dependency:copy-dependencies package
The chosen answer is great, but still I would like to add something small to the topic. An illustration.
It clearly demonstrates how the different phases binded to different plugins and the goals that those plugins expose.
So, let's examine a case of running something like mvn compile
:
mvn compile
it's mapped to a specific goal, the compile goal.mvn compiler:compile
Therefore, phase is made up of plugin goals.
Link to the reference
Credit to Sandeep Jindal and Premraj. Their explanation help me to understand after confused about this for a while.
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.
In short from the link, You should not try to understand all three at once, first you should understand the relationship in these groups:
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 phase 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!)
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
More detail and full examples are in https://www.surasint.com/maven-life-cycle-phase-and-goal-easy-explained/
There are following three built-in build lifecycles:
Lifecycle default -> [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]
Lifecycle clean -> [pre-clean, clean, post-clean]
Lifecycle site -> [pre-site, site, post-site, site-deploy]
The flow is sequential, for example, for default lifecycle, it starts with validate, then initialize and so on...
You can check the lifecycle by enabling debug mode of mvn
i.e., mvn -X <your_goal>
Goals are executed in phases which help determine the order goals get executed in. The best understanding of this is to look at the default Maven lifecycle bindings which shows which goals get run in which phases by default. The compile
phase goals will always be executed before the test
phase goals, which will always be executed before the package
phase goals and so on.
Part of the confusion is exacerbated by the fact that when you execute Maven you can specify a goal or a phase. If you specify a phase then Maven will run all phases up to the phase you specified in order (e.g. if you specify package it will first run through the compile phase and then the test phase and finally the package phase) and for each phase it will run all goals attached to that phase.
When you create a plugin execution in your Maven build file and you only specify the goal then it will bind that goal to a given default phase. For example, the jaxb:xjc
goal binds by default to the generate-resources
phase. However, when you specify the execution you can also explicitly specify the phase for that goal as well.
If you specify a goal when you execute Maven then it will run that goal and only that goal. In other words, if you specify the jar:jar
goal it will only run the jar:jar
goal to package your code into a jar. If you have not previously run the compile goal or prepared your compiled code in some other way this may very likely fail.
I believe a good answer is already provided, but I would like to add an easy-to-follow diagram of the different 3 life-cycles (build
, clean
, and site
) and the phases in each.
The phases in bold - are the main phases commonly used.