Maven Build Life Cycle
When working with Apache Maven, every command you execute follows a predefined sequence of steps known as the build lifecycle.
Understanding this lifecycle is essential for any Java developer who wants to build, test, and deploy applications efficiently.
In this guide, we’ll explore:
- Maven’s three standard lifecycles
- The most important phases (
validate
,compile
,test
,package
,verify
,install
,deploy
) - How goals are tied to lifecycle phases
1. Maven’s Three Standard Lifecycles
Maven comes with three built-in lifecycles, each designed for a different aspect of project management.
1.1 Default Lifecycle
- Handles the entire project build process.
- Includes compiling code, running tests, packaging, installing, and deploying.
- The lifecycle you’ll use most often in Java development.
1.2 Clean Lifecycle
- Takes care of cleaning up build outputs from previous runs.
- Removes the
target/
directory and any generated files. - Example:mvn clean
1.3 Site Lifecycle
- Used to generate project documentation and reports.
- Useful when you want to create a site that describes your project.
- Example:mvn site
2. Maven Build Lifecycle Phases
A phase represents a specific stage in the build lifecycle.
When you run a phase, Maven automatically runs all the phases before it.
Here are the key phases in the default lifecycle:
Phase | What It Does |
---|---|
validate | Checks if the project structure is correct and required information is available. |
compile | Compiles source code from src/main/java into .class files in target/classes . |
test | Runs unit tests in src/test/java using a framework like JUnit. |
package | Packages compiled code into a distributable format (JAR, WAR, or EAR). |
verify | Runs checks to ensure the package meets quality criteria. |
install | Installs the package into your local Maven repository (~/.m2/repository ) for reuse. |
deploy | Copies the final package to a remote repository for sharing with teammates or CI/CD pipelines. |
📌 Example:
Running
mvn package
will trigger:
validate → compile → test → package
3. Goals and Their Relationship to Phases
A goal is a specific task Maven performs (e.g., compiling code, running tests).
Phases are just stages, while goals are the actual work units bound to those stages.
- The compile phase has the
compiler:compile
goal. - The test phase has the
surefire:test
goal. - The package phase may trigger
jar:jar
orwar:war
depending on your packaging type.
Running Goals Directly
You can run goals independently:
mvn compiler:compile
But in practice, most developers run phases like install
or package
, and Maven runs all the bound goals automatically.
4. Visual Representation of the Lifecycle
Default Lifecycle:
validate → compile → test → package → verify → install → deploy
Each arrow represents phases executed in sequence, with goals bound to them.
5. Common Maven Commands You’ll Use
- Clean and Build Project:
mvn clean install
. - Skip Tests During Build:
mvn install -DskipTests
. - Generate Project Site Documentation:
mvn site
.
Key Takeaways
- Maven has three lifecycles: default (build), clean (remove old files), and site (documentation).
- Each lifecycle has phases that represent stages in the build process.
- Phases are tied to goals, which do the actual work.
- Running a later phase automatically runs all earlier phases.