Maven Build Life Cycle

What is build life cycle? The sequence of steps which is defined in order to execute the tasks and goals of any maven project is known as build life cycle in maven. Maven 2.0 version is basically a build life cycle oriented and clearly says that these steps are well defined to get the desired output after the successful execution of the build life cycle.

Maven comes with 3 built-in build life cycles as shown below :

  • Clean - this phase involves cleaning of the project (for a fresh build & deployment)
  • Default - this phase handles the complete deployment of the project
  • Site - this phase handles the generating the java documentation of the project.

Now we will dig more into the detailed phases involved in the above mentioned built-in build life cycles.

Build Life Cycle of clean phase

As mentioned above, this clean phase is used to clean up the project and make it ready for the fresh compile and deployment. The command used for the same is mvn post-clean. When this command is invoked, maven executes the below tasks via executing the below commands internally :

  1. mvn pre-clean
  2. mvn clean
  3. mvn post-clean

This maven's clean is a goal and on executing it cleans up the output directory (target folder) by deleting all the compiled files.

NOTE : whenever a maven command for any life cycle is invoked, maven executes the phases till and up to the invoked phase. E.g. when 'mvn clean' is invoked, maven will execute only the phase clean. But, no compile/deployment/site phase is invoked.

Build life cycle of clean phase

Build Lifecycle (Default)

Below is the list of phases in the build lifecycle (default) of maven. These phases will be invoked through the maven commands.

Lifecycle PhaseDescription
validateValidates and ensures that the project is fine and perfect considering all the required information is made available for the build
generate-sourcesGenerating any source code to include the same in the compilation process
process-sourcesProcessing the source code in case some filter needs to be applied
generate-sourcesGenerating any source code to include the package
process-resourcesProcess of copying the resources to the destination folder and getting ready for the packaging
compileCompilation of the project source code.
process-classesTo perform the bytecode enhancements for the class files generated from the compilation
generate-test-sourcesCopying and processing the resources in the test destination directory.
test-compileCompile the source code in the test destination directory
testExecuting/running the tests using some suitable test framework. Note: these test cases are not considered for packaging and deploying
prepare-packageTo perform any final changes/validations before it is sent for final packaging.
packagePackaging the successfully compiled and tested code to some distributable format - JAR, WAR, EAR
pre-integration-testTo perform actions before integration tests are executed. This may require to set up some environmental changes for the app.
integration-testDeploy the application to an environment where integration tests can be run.
post-integration-testBasically, cleaning up the environment which was made in pre-integration-test phase.
verifyPerforms action for a quality check and ensures the required criteria being met
installInstalling the application in the local repository. Any other project can use this as a dependency.
deployThe final package will be copied to a remote repository, may be as a formal release and also made available to the other developers too.

Site Lifecycle (site)

Apart from cleaning, compiling the source code, building a deployable format of the application, maven has phase which does more than these phases. This phase is one of the vital features provided by maven which generates the detailed documentation of any java project.

This project documentation has a dedicated phases involved as listed below :

  • pre-site
  • site
  • post-site
  • site-deploy

The command used in maven to generate javadocs for a given project is 'mvn site'. Basically, when this command is invoked, maven calls 'Doxia' document generation and other report generating plugins.

Doxia is basically a framework used for content generation by maven. This generates contents both in static and dynamic ways.

Site Lifecycle