Here’s a complete list of all Maven lifecycle phases, categorized by each lifecycle:
🚀 1. Clean Lifecycle (Removes Old Builds)
Phase | What It Does |
---|---|
pre-clean |
Executes custom tasks before cleaning (if configured). |
clean |
Deletes the target/ directory (removes compiled code, JARs, test reports, etc.). |
post-clean |
Executes custom tasks after cleaning (if configured). |
🛠 Command to run:
mvn clean
👉 Runs only the clean
lifecycle (no compilation or packaging).
⚙️ 2. Default (Build) Lifecycle (The Main One)
Phase | What It Does |
---|---|
validate |
Checks that the project structure and pom.xml are correct. |
initialize |
Sets up the project (e.g., initializes properties, creates directories). |
generate-sources |
Generates source code if needed (e.g., annotation processing, codegen tools). |
process-sources |
Processes existing source code (e.g., applying formatting or transformations). |
generate-resources |
Generates additional resource files if needed. |
process-resources |
Copies and processes resource files (from src/main/resources to target/ ). |
compile |
Compiles Java source code (src/main/java ) into .class files. |
process-classes |
Processes compiled classes (e.g., bytecode enhancement). |
generate-test-sources |
Generates test source files if needed. |
process-test-sources |
Processes test source files (e.g., annotations, generated classes). |
generate-test-resources |
Generates test resource files if needed. |
process-test-resources |
Copies test resources (src/test/resources ). |
test-compile |
Compiles test source code (src/test/java ). |
process-test-classes |
Processes compiled test classes (e.g., instrumentation). |
test |
Runs unit tests using a framework like JUnit or TestNG. |
prepare-package |
Prepares the project for packaging (e.g., signing artifacts, modifying manifests). |
package |
Bundles the compiled code into a JAR, WAR, or other format. |
pre-integration-test |
Prepares for integration testing (e.g., starting a database or server). |
integration-test |
Runs integration tests (tests that require an external environment). |
post-integration-test |
Cleans up after integration tests (stopping services, rolling back changes). |
verify |
Checks the packaged artifact for correctness (e.g., compliance, security checks). |
install |
Copies the packaged artifact (JAR/WAR) into the local repository (~/.m2/repository/ ). |
deploy |
Uploads the final artifact to a remote repository (e.g., Nexus, Artifactory). |
🛠 Common commands and what they do:
mvn compile
👉 Runs: validate → initialize → process-sources → process-resources → compile
mvn package
👉 Runs: Everything up to package
, creating a .jar
or .war
file.
mvn install
👉 Runs: Everything up to install
, storing the artifact in your local Maven repository.
mvn deploy
👉 Runs: Everything up to deploy
, uploading the artifact to a remote repository.
📄 3. Site Lifecycle (Documentation & Reports)
Phase | What It Does |
---|---|
pre-site |
Runs tasks before generating documentation. |
site |
Generates project documentation (e.g., javadocs, reports). |
post-site |
Runs tasks after documentation is generated. |
site-deploy |
Uploads the generated site to a remote server. |
🛠 Command to generate documentation:
mvn site
👉 Creates a target/site/index.html
with project reports.
📝 Summary
Lifecycle | Phases | Runs Before Every Phase? |
---|---|---|
Clean | pre-clean → clean → post-clean |
No (only runs clean tasks). |
Default (Build) | validate → compile → test → package → install → deploy |
Yes (runs all previous phases). |
Site | pre-site → site → post-site → site-deploy |
No (only runs site-related tasks). |
👉 Maven only runs up to the phase you request, including all previous phases—but does not run future phases.
👉 The Default Lifecycle is the main one used in every project.
💡 Example: Full Maven Workflow
Imagine you’re building a Java project with unit tests and want to install it locally.
You run:
mvn install
Maven automatically executes:
- validate → Checks project structure.
- compile → Compiles Java files.
- test → Runs unit tests.
-
package → Creates a
.jar
or.war
. -
install → Stores the
.jar/.war
in your local repository (~/.m2/repository
).
If you then want to publish the artifact to a remote repository, run:
mvn deploy
This executes:
- deploy → Uploads the artifact to a central repository.
🎯 Key Takeaways
- The Default Lifecycle is the most important (it handles compilation, testing, and packaging).
- Every phase runs all previous phases in its lifecycle.
- Maven only runs what’s needed—it won’t run extra phases unless required.
- The Clean Lifecycle runs separately (it won’t trigger compilation).
- The Site Lifecycle is for documentation and reports.