This content originally appeared on DEV Community and was authored by DevCorner2
When working with Maven, the lifecycle can feel abstract — “phases,” “goals,” “plugins” — it’s easy to get lost.
But here’s the reality: you don’t run lifecycle phases directly in day-to-day work.
Instead, you run commands like mvn package
or mvn install
, and Maven quietly executes an ordered sequence of phases behind the scenes.
This blog post will break it down, step by step, based on the actual commands you run.
The Big Idea
Maven has three lifecycles:
- default – the main build (compiling, testing, packaging, deploying)
-
clean – handles cleanup (deletes
target/
) - site – generates project documentation
When you run a command like mvn package
, Maven doesn’t just package — it runs all phases up to package in the correct order.
Commands and Their Lifecycle Phases
1. mvn validate
- Runs only: validate
Ensures the project structure and
pom.xml
are valid.
2. mvn compile
- Runs:
validate → initialize → generate-sources → process-sources → generate-resources → process-resources → compile
Compiles main source code into
target/classes
.
3. mvn test-compile
- Runs everything up to compile + test-related phases:
process-classes → generate-test-sources → process-test-sources → generate-test-resources → process-test-resources → test-compile
Compiles test sources into
target/test-classes
.
4. mvn test
- Runs everything up to test-compile +:
process-test-classes → test
Executes unit tests (JUnit, TestNG).
5. mvn package
- Runs everything up to test +:
prepare-package → package
Packages the compiled code into a distributable format (JAR/WAR/EAR).
6. mvn verify
- Runs everything up to package +:
pre-integration-test → integration-test → post-integration-test → verify
Runs integration tests and checks.
7. mvn install
- Runs everything up to verify +:
install
Installs the artifact into your local Maven repository (
~/.m2/repository
).
8. mvn deploy
- Runs everything up to install +:
deploy
Pushes the final artifact to a remote repository (e.g., Nexus, Artifactory).
Clean Lifecycle Commands
mvn clean
- Runs:
pre-clean → clean → post-clean
Deletes the
target/
directory to ensure a fresh build.
Site Lifecycle Commands
mvn site
- Runs:
pre-site → site → post-site → site-deploy
Generates a project documentation site.
Quick Reference Table
Command | Lifecycle Phases Executed | Output |
---|---|---|
mvn validate |
validate | Validates project + POM |
mvn compile |
up to compile | Compiled classes in target/classes
|
mvn test-compile |
up to test-compile | Compiled test classes |
mvn test |
up to test | Runs unit tests |
mvn package |
up to package | JAR/WAR/EAR package |
mvn verify |
up to verify | Runs integration tests, verification |
mvn install |
up to install | Artifact in local repo |
mvn deploy |
up to deploy | Artifact in remote repo |
mvn clean |
clean lifecycle | Deletes target/
|
mvn site |
site lifecycle | Generates documentation site |
Closing Thoughts
The key takeaway:
When you run
mvn <phase>
, you’re not just running that phase. Maven executes all earlier phases in the lifecycle.
That’s why mvn package
also compiles and tests, and why mvn install
implicitly does everything from validation through testing before dropping the artifact into your local repo.
Understanding this flow helps you avoid confusion, optimize builds, and know exactly what Maven is doing under the hood.
This content originally appeared on DEV Community and was authored by DevCorner2