The Maven Build Lifecycle is a sequence of phases that define the order in which the goals (tasks) are executed to build and manage a project. Maven follows a well-defined standard lifecycle, which simplifies the build process and provides consistency across different projects.
What is a Build Lifecycle?
A build lifecycle is essentially a well-defined sequence of phases that need to be executed to build a project. Each phase represents a stage in the lifecycle.
Maven has three built-in lifecycles:
default – handles your project deployment (most commonly used).
clean – handles project cleaning (deletes old builds).
site – handles the creation of your project’s documentation.
This is the main lifecycle and consists of 23 phases, out of which the most commonly used ones are:
| Phase | Description |
|---|---|
| validate | Validates the project is correct and all necessary information is available. |
| initialize | Initializes build state, sets properties. |
| generate-sources | Generates any source code needed before compilation. |
| process-sources | Processes the source code, like filtering. |
| generate-resources | Generates resources for inclusion in the package. |
| process-resources | Copies and processes resources (like properties files) to the output directory. |
| compile | Compiles the source code. |
| process-classes | Post-processes the compiled classes (e.g., bytecode enhancement). |
| generate-test-sources | Generates test source code. |
| process-test-sources | Processes the test source code. |
| test-compile | Compiles test source code. |
| process-test-classes | Post-processes compiled test classes. |
| test | Runs tests using a suitable unit testing framework. |
| prepare-package | Performs operations before packaging like war/jar customization. |
| package | Packages the compiled code into a JAR/WAR. |
| pre-integration-test | Setup steps before running integration tests. |
| integration-test | Runs integration tests. |
| post-integration-test | Cleanup after integration tests. |
| verify | Runs checks to ensure the quality and correctness of the package. |
| install | Installs the package into the local Maven repository. |
| deploy | Copies the final package to the remote repository. |
Used to clean the project before building. It has 3 phases:
| Phase | Description |
|---|---|
| pre-clean | Executed before the actual project cleaning. |
| clean | Deletes the target/ directory (build output). |
| post-clean | Executed after the cleaning is complete. |
Used to generate documentation for the project. It has 4 phases:
| Phase | Description |
|---|---|
| pre-site | Executed before generating the site. |
| site | Generates the project documentation. |
| post-site | Executes operations needed after the site is generated. |
| site-deploy | Deploys the site to a web server. |
mvn clean install
Above command will:
- clean: delete the old
target/directory.
- install: run the full default lifecycle (validate → compile → test → package → install).