Maven Build Life Cycle
The Maven build lifecycle defines the process for building and publishing a project, encompassing three standard lifecycles:
* clean: Cleans the project
* default (or build): The core build process
* site: Generates project documentation
!(#)
Each lifecycle consists of multiple phases (phase), which are executed in sequence.
### 1γclean Lifecycle
The clean lifecycle is responsible for cleaning up temporary files and directories (primarily `target/`) from the project. It includes the following main phases:
| Phase | Description | Automatically Bound Plugin Goal |
| --- | --- | --- |
| `pre-clean` | Preparatory work before cleaning | No default binding |
| `clean` | Deletes the `target/` directory | `maven-clean-plugin:clean` |
| `post-clean` | Finalization work after cleaning | No default binding |
**Common Commands:**
mvn clean # Executes the clean phase (includes pre-clean and clean)
### 2γdefault Lifecycle
The default lifecycle is Maven's primary lifecycle, encompassing all core steps for building and deploying a project.
A typical Maven build lifecycle consists of the following sequence of phases:
!(#)
| Phase | Processing | Description |
| --- | --- | --- |
| validate | Validates the project | Verifies that the project is correct and all necessary information is available. |
| compile | Executes compilation | Compiles the source code. |
| test | Tests | Runs tests using an appropriate unit testing framework (e.g., JUnit). |
| package | Packages | Packages the compiled code into a distributable format, such as JAR or WAR. |
| verify | Verifies | Checks the results of integration tests to ensure quality standards are met. |
| install | Installs | Installs the packaged project into the local repository for use by other projects. |
| deploy | Deploys | Copies the final package to a remote repository for sharing with other developers and projects. |
To complete the default lifecycle, these phases (including other lifecycle phases not listed above) will be executed in sequence.
**Phase Dependencies:**
!(#)
**Common Commands:**
mvn compile # Compiles main source code mvn test # Runs tests mvn package # Packages (generates target/*.jar) mvn install # Installs to local repository (~/.m2/repository) mvn deploy # Deploys to remote repository (requires distributionManagement configuration)
### 3γsite Lifecycle
The site lifecycle is used to generate project site documentation:
| Phase | Description | Automatically Bound Plugin Goal |
| --- | --- | --- |
| `pre-site` | Preparation before site generation | No default binding |
| `site` | Generates project site documentation | `maven-site-plugin:site` |
| `post-site` | Finalization after site generation | No default binding |
| `site-deploy` | Deploys the site to a server | `maven-site-plugin:deploy` |
**Common Commands:**
mvn site # Generates site (output in target/site/) mvn site-deploy # Deploys site (requires site URL configuration)
### Lifecycle Summary
Maven has the following three standard lifecycles:
**1γClean Lifecycle:**
* **clean**: Deletes compiled output files from the target directory. This is typically executed before a build to ensure the project starts from a clean state.
**2γDefault Lifecycle (also known as the Build Lifecycle):**
* **validate**: Validates the correctness of the project, such as checking if the project version is correct.
* **compile**: Compiles the project's source code.
* **test**: Runs the project's unit tests.
* **package**: Packages the compiled code into a distributable format, such as JAR or WAR.
* **verify**: Performs additional checks on the project to ensure quality.
* **install**: Installs the project's build output into the local Maven repository for use by other projects.
* **deploy**: Copies the project's build output to a remote repository for use by other developers or teams.
**3γSite Lifecycle:**
* **site**: Generates project documentation and site information.
* **deploy-site**: Publishes the generated site information to a remote server to share project documentation.
### Build Phases are Composed of Plugin Goals
A plugin goal represents a specific task (more granular than a build phase), which aids in building and managing the project. These goals may be bound to multiple phases or not bound at all. Goals not bound to any build phase can be executed outside the build lifecycle by direct invocation. The execution order of these goals depends on the order of goal invocation and build phases.
For example, consider the following command:
clean and package are build phases, dependency:copy-dependencies is a goal
mvn clean dependency:copy-dependencies package
Here, the clean phase will be executed first, then the dependency:copy-dependencies goal will be executed, and finally the package phase will be executed.
* * *
## 1γClean Lifecycle
When we execute the `mvn post-clean` command, Maven invokes the clean lifecycle, which includes the following phases:
* pre-clean: Performs some work that needs to be done before cleaning
* clean: Removes all files generated by the previous build
* post-clean: Performs some work that needs to be done immediately after cleaning
The `clean` in `mvn clean` is the clean phase mentioned above. In a lifecycle, when a phase is executed, all phases preceding it are also executed. Therefore, if you execute `mvn clean`, the following two lifecycle phases will be run:
pre-clean, clean
If we run `mvn post-clean`, then the following three lifecycle phases will be run:
pre-clean, clean, post-clean
We can modify the behavior of this part by defining goals at any phase of the clean lifecycle above.
In the following example, we add the `maven-antrun-plugin:run` goal to the pre-clean, clean, and post-clean phases. This allows us to display text messages at various stages of the clean lifecycle.
We have created a `pom.xml` file in the `C:MVNproject` directory.
4.0.0com.companyname.projectgroupproject1.0org.apache.maven.pluginsmaven-antrun-plugin1.1id.pre-cleanpre-cleanrunpre-clean phaseid.cleancleanrunclean phaseid.post-cleanpost-cleanrunpost-clean phase
Now open the command console, navigate to the directory containing `pom.xml`, and execute the following `mvn` command.
C:MVNproject>mvn post-clean
Maven will start processing and display all phases of the clean lifecycle.
Scanning for projects... ------------------------------------------------------------------ Building Unnamed - com.companyname.projectgroup:project:jar:1.0 task-segment: ------------------------------------------------------------------ [antrun:run {execution: id.pre-clean}] Executing tasks pre-clean phase Executed tasks [clean:clean {execution: default-clean}] [antrun:run {execution: id.clean}] Executing tasks clean phase Executed tasks [antrun:run {execution: id.post-clean}] Executing tasks post-clean phase Executed tasks ------------------------------------------------------------------ BUILD SUCCESSFUL ------------------------------------------------------------------ Total time: < 1 second Finished at: Sat Jul 07 13:38:59 IST 2012 Final Memory: 4M/44M ------------------------------------------------------------------
You can try modifying the `mvn clean` command to display pre-clean and clean, while performing no action in the post-clean phase.
* * *
## 2γDefault (Build) Lifecycle
This is Maven's primary lifecycle, used for building applications, and includes the following 23 phases:
| Lifecycle Phase | Description |
| --- | --- |
| validate (Validate) | Validates that the project is correct and all necessary information is available to complete the build process. |
| initialize (Initialization) | Initializes the build state, such as setting property values. |
| generate-sources (Generate Source Code) | Generates any source code to be included in the compilation phase. |
| process-sources (Process Source Code) | Processes the source code, for example, filtering any values. |
| generate-resources (Generate Resource Files) | Generates resource files to be included in the package. |
| process-resources (Process Resource Files) | Copies and processes resources to the target directory, preparing for the packaging phase. |
| compile (Compile) | Compiles the project's source code. |
| process-classes (Process Classes) | Processes the compiled files, for example, performing bytecode enhancement or optimization on Java class files. |
| generate-test-sources (Generate Test Source Code) | Generates any test source code to be included in the compilation phase. |
| process-test-sources (Process Test Source Code) | Processes the test source code, for example, filtering any values. |
| generate-test-resources (Generate Test Resource Files) | Creates resource files for testing. |
| process-test-resources (Process Test Resource Files) | Copies and processes test resources to the target directory. |
| test-compile (CompileTestSource Code) | Compiles test source code to the test target directory. |
| process-test-classes (Process Test Classes) | Processes the files generated from test source code compilation. |
| test (Test) | Runs tests using an appropriate unit testing framework (JUnit is one of them). |
| prepare-package (Prepare Package) | Performs any necessary actions to prepare for packaging before the actual packaging occurs. |
| package (Package) | Packages the compiled code into a distributable format, such as JAR, WAR, or EAR files. |
| pre-integration-test (Pre-integration Test) | Performs necessary actions before running integration tests. For example, setting up the required environment. |
| integration-test (Integration Test) | Processes and deploys the project to an environment where integration tests can be run. |
| post-integration-test (Integration TestPost) | Performs necessary actions after running integration tests. For example, cleaning up the integration test environment. |
| verify (validation) | Runs any checks to verify that the project package is valid and meets quality standards. |
| install (Install) | Installs the project package into the local repository so that it can be used as a dependency for other local projects. |
| deploy (Deploy) | Copies the final project package to a remote repository for sharing with other developers and projects. |
There are some important concepts related to the Maven lifecycle to explain:
When a phase is invoked via a Maven command, such as `mvn compile`, only that phase and all phases preceding it (including that phase) will be executed.
Different Maven goals will be bound to different Maven lifecycle phases depending on the packaging type (JAR / WAR / EAR).
In the following example, we add the `maven-antrun-plugin:run` goal to some phases of the Build lifecycle. This allows us to display text messages for the lifecycle phases.
We have updated the `pom.xml` file in the `C:MVNproject` directory.
4.0.0com.companyname.projectgroupproject1.0org.apache.maven.pluginsmaven-antrun-plugin1.1id.validatevalidaterunvalidate phaseid.compilecompileruncompile phaseid.testtestruntest phaseid.packagepackagerunpackage phaseid.deploydeployrundeploy phase
Now open the command console, navigate to the directory containing `pom.xml`, and execute the following `mvn` command.
C:MVNproject>mvn compile
Maven will start processing and display the build lifecycle phases up to the compile phase.
Scanning for projects... ------------------------------------------------------------------ Building Unnamed - com.companyname.projectgroup:project:jar:1.0 task-segment: ------------------------------------------------------------------ [antrun:run {execution: id.validate}] Executing tasks validate phase Executed tasks [resources:resources {execution: default-resources}] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent! skip non existing resourceDirectory C:MVNprojectsrcmainresources [compiler:compile {execution: default-compile}] Nothing to compile - all classes are up to date [antrun:run {execution: id.compile}] Executing tasks compile phase Executed tasks ------------------------------------------------------------------ BUILD SUCCESSFUL ------------------------------------------------------------------ Total time: 2 seconds Finished at: Sat Jul 07 20:18:25 IST 2012 Final Memory: 7M/64M ------------------------------------------------------------------
### Command Line Invocation
In a development environment, use the following command to build and install the project to the local repository:
mvn install
This command executes the phases of the default lifecycle (validate, compile, package, etc.) in sequence before executing the install phase. We only need to invoke the last phase, which in this case is install.
In a build environment, use the following invocation to cleanly build and deploy the project to a shared repository:
mvn clean deploy
This command can also be used in multi-module scenarios, i.e., projects containing multiple sub-projects. Maven will execute the `clean` command in each sub-project, followed by the `deploy` command.
* * *
## 3γSite Lifecycle
The Maven Site plugin is generally used to create new report documents, deploy sites, etc.
* pre-site: Performs some work that needs to be done before generating site documentation
* site: Generates the project's site documentation
* post-site: Performs some work that needs to be done after generating site documentation and prepares for deployment
* site-deploy: Deploys the generated site documentation to a specific server
The phases commonly used here are the site phase and the site-deploy phase, used to generate and publish the Maven site. This is a powerful feature of Maven that managers like, as documentation and statistical data are automatically generated and look good. In the following example, we add the `maven-antrun-plugin:run` goal to all phases of the Site lifecycle. This allows us to display all text messages for the lifecycle phases.
We have updated the `pom.xml` file in the `C:MVNproject` directory.
4.0.0com.companyname.projectgroupproject1.0org.apache.maven.pluginsmaven-antrun-plugin1.1id.pre-sitepre-siterunpre-site phaseid.sitesiterunsite phaseid.post-sitepost-siterunpost-site phaseid.site-deploysite-deployrunsite-deploy phase
Now open the command console, navigate to the directory containing `pom.xml`, and execute the following `mvn` command.
C:MVNproject>mvn site
Maven will start processing and display the site lifecycle phases up to the site phase.
Scanning for projects... ------------------------------------------------------------------ Building Unnamed - com.companyname.projectgroup:project:jar:1.0 task-segment: ------------------------------------------------------------------ [antrun:run {execution: id.pre-site}]
YouTip