Lifecycle
Maven is basically built round the concept of lifecycle, which means the process for compiling, testing, building and distributing of a particular artifact is clearly defined. There are three built-in build lifecycles: default
, clean
and site
. Each lifecycle is made up of multiple phases
. The execution of lifecycle is sequential, which means Maven will run all the phase(s) in every project/subproject upto the target phase in order.
default, which is responsible for project deployment, consists of the following phases.
- 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
clean cleans up artifacts created by prior builds.
- pre-clean
- clean
- post-clean
site generates site documentation for this project.
- pre-site
- site
- post-site
- site-deploy
The phases named with hyphenated-words (which are not highlighted in the above lists) produce intermediate results for the lifecycle, which allows various plugins, such as JaCoCo, may bind plugin goals
to those phases to prepare execution environment. But those phases are normally not called from commnad line direclty for the reason that it may leave processes hanging and even Maven not terminating properly.
This command execute each lifecycle phases in order: clean, validate, compile, test, package, verify, install and finally deploy.
Goal
Goal (plugin goal) is the smallest unit to construct a Maven build phase, which specifies a task to build and manage a project. It can either be bound to one or more build phases, or be executed directly independent of any lifecycle/phase.
clean and package are both build phases, while dependency:copy-dependencies is a build goal, which is executed after clean and before package. In addition, if dependency:copy-dependencies is attached to any other phases, it will also be triggered when the specific phase(s) are executed.
Binding
Binding is a way to attach execution goals to specific phases in Maven lifecycles. There are two major ways to achieve this via configuration: packaing
and plugins
.
packaging
is the most common way to add multiple goals to multiple phases, by adding the following configuration to pom.xml root.
Some of the <packaging>
configuration value are: pom
, jar
, ejb
, war
, ear
and rar
. The default value is jar
if the configuration is absent. A complete list of standard bindings can be found here.
On top of packaging
, plugin
is another way to add goals to phases. It binds a goal directly to a set of pre-defined phase(s), just like packaing. Alternatively, it can be added to a specific phase, like the following