Maven Tutorial - Maven Build Life Cycles

A Build Lifecycle is a sequence of tasks we used to build a software. For example, compile, test, test more, package and publish or deploy are all tasks we need to do to build a software.

A Maven build lifecycle is a sequence of phases we need to go through in order to finishing building the software.

The following table lists some of the build lifecycle.

Lifecycle Description
validatevalidate the project is correct and all necessary information is available
compilecompile the source code
testtest the compiled source code using a unit testing
packagetake the compiled code and package it in its distributable format, such as a JAR
integration-testdeploy the package into an environment where integration tests can be run
verifyverify the package is valid and meets quality criteria
installinstall the package into the local repository
deploypublish to integration or release environment

Maven has following three standard lifecycles:

  • clean
  • default (or build)
  • site

These build phases are executed sequentially to complete the default lifecycle.

Given the build phases above, when the default lifecycle is used, Maven will

  1. validate the project
  2. compile the sources
  3. run those against the tests
  4. package the binaries (e.g. jar)
  5. run integration tests against that package
  6. verify the package
  7. install the verifed package to the local repository
  8. deploy the installed package in a specified environment

To do all those, you only need to call the last build phase to be executed, in this case, deploy:

mvn deploy

Calling a build phase will execute not only that build phase, but also every build phase prior to the called build phase.

Thus, doing

mvn integration-test

will do every build phase before it (validate, compile, package, etc.), before executing integration-test.

The same command can be used in a multi-module with one or more subprojects. For example:

mvn clean install

This command will traverse into all of the subprojects and run clean, then install including all of the prior steps.

Clean Lifecycle Reference

pre-clean executes processes needed prior to the actual project cleaning
clean remove all files generated by the previous build
post-clean executes processes needed to finalize the project cleaning

Default Lifecycle Reference

validate validate the project and ensure that all necessary information is available.
initialize initialize build state, set properties or create directories.
generate-sources generate any source code.
process-sources process the source code.
generate-resources generate resources.
process-resources copy and process the resources into the destination directory for packaging.
compile compile the source code.
process-classes post-process the generated files from compilation.
generate-test-sources generate any test source code.
process-test-sources process the test source code.
generate-test-resources create resources for testing.
process-test-resources copy and process the resources into the test destination directory.
test-compile compile the test source code
process-test-classes post-process the generated files from test compilation.
test run tests using a unit testing framework.
prepare-package perform any operations necessary to prepare a package before the packaging.
package package the compiled code into its distributable format, such as a JAR.
pre-integration-test perform actions required before integration tests are executed.
integration-test process and deploy the package into an environment where integration tests can be run.
post-integration-test perform actions required after integration tests have been executed.
verify run any checks to verify the package is valid.
install install the package into the local repository.
deploy publish the project.

Site Lifecycle Reference

pre-site executes processes prior to the project site generation
site generates the project's site documentation
post-site executes processes to finalize the site generation
site-deploy deploys the generated site to the web server