Deep Dive into the Default (Build) Lifecycle in Maven
The Default (Build) Lifecycle is the most important lifecycle in Maven. It automates compiling code, running tests, packaging artifacts, and preparing them for deployment.
1️⃣ Default Lifecycle Overview
The Default Lifecycle consists of 23 phases, each responsible for a specific step in the build process.
Lifecycle: Default (Build)
│
├── Phase: validate
│ ├── Goal: (No default goal, plugins can be added)
│ ├── Task: Check project structure and `pom.xml` correctness.
│
├── Phase: compile
│ ├── Goal: maven-compiler-plugin:compile
│ ├── Task: Compile Java source code from `src/main/java`.
│
├── Phase: test
│ ├── Goal: maven-surefire-plugin:test
│ ├── Task: Run unit tests using JUnit/TestNG.
│
├── Phase: package
│ ├── Goal: maven-jar-plugin:jar (or `maven-war-plugin:war` for web apps)
│ ├── Task: Package compiled code into JAR/WAR.
│
├── Phase: install
│ ├── Goal: maven-install-plugin:install
│ ├── Task: Copy packaged JAR/WAR to local repository (`~/.m2/repository`).
│
└── Phase: deploy
├── Goal: maven-deploy-plugin:deploy
├── Task: Upload artifact to remote repository.
🚀 Command Example:
mvn package
👉 Runs all phases up to and including package
.
2️⃣ Default Lifecycle Phases, Goals, and Tasks
Below is the full breakdown of all 23 phases in the Default Lifecycle.
🔹 Phase 1: validate
📌 Purpose:
- Checks if the project structure is valid before building.
📌 Goals:
- No default goal.
- Can be extended with validation plugins.
📌 Tasks:
- Verify the existence of
pom.xml
. - Ensure required properties and dependencies are set.
🔹 Phase 2: initialize
📌 Purpose:
- Sets up initial build settings before execution.
📌 Goals:
- No default goal.
- Custom goals can be added (e.g., setting up properties).
📌 Tasks:
- Create necessary directories (if needed).
- Initialize system properties.
🔹 Phase 3: generate-sources
📌 Purpose:
- Generates additional source code before compilation.
📌 Goals:
- Used in projects that need generated code, like:
- JPA metamodel generation.
- Lombok annotation processing.
📌 Tasks:
- Generate source code files dynamically.
🔹 Phase 4: process-sources
📌 Purpose:
- Applies modifications to existing source code.
📌 Tasks:
- Apply formatting or transformation tools.
🔹 Phase 5: generate-resources
📌 Purpose:
- Generates additional resource files (e.g., property files).
📌 Tasks:
- Dynamically generate
application.properties
,config.xml
, etc.
🔹 Phase 6: process-resources
📌 Purpose:
- Copies non-source files to the output directory (
target/
).
📌 Goals:
maven-resources-plugin:resources
📌 Tasks:
- Copy all resources from
src/main/resources
totarget/classes
.
🔹 Phase 7: compile
📌 Purpose:
- Compiles Java source code into
.class
files.
📌 Goal:
maven-compiler-plugin:compile
📌 Tasks:
- Read Java source files from
src/main/java
. - Compile them into
.class
files insidetarget/classes
.
🔹 Phase 8: process-classes
📌 Purpose:
- Runs additional processing on compiled
.class
files.
📌 Tasks:
- Modify bytecode if necessary (e.g., aspect-oriented programming).
🔹 Phase 9: generate-test-sources
📌 Purpose:
- Generates test sources (e.g., mock data).
🔹 Phase 10: process-test-sources
📌 Purpose:
- Processes test source files before compilation.
🔹 Phase 11: generate-test-resources
📌 Purpose:
- Generates test-related resource files.
🔹 Phase 12: process-test-resources
📌 Purpose:
- Copies test resources (from
src/test/resources
totarget/test-classes
).
🔹 Phase 13: test-compile
📌 Purpose:
- Compiles test source code.
📌 Goal:
maven-compiler-plugin:testCompile
📌 Tasks:
- Compile Java test files inside
src/test/java
.
🔹 Phase 14: process-test-classes
📌 Purpose:
- Applies bytecode modifications to compiled test classes.
🔹 Phase 15: test
📌 Purpose:
- Runs unit tests.
📌 Goal:
maven-surefire-plugin:test
📌 Tasks:
- Execute JUnit/TestNG test cases.
🚀 Run tests manually:
mvn test
🔹 Phase 16: prepare-package
📌 Purpose:
- Prepares compiled code for packaging.
🔹 Phase 17: package
📌 Purpose:
- Packages compiled code into a JAR/WAR.
📌 Goal:
maven-jar-plugin:jar
-
maven-war-plugin:war
(for web apps)
📌 Tasks:
- Create
target/my-app-1.0.jar
ortarget/my-app-1.0.war
.
🚀 Run manually:
mvn package
🔹 Phase 18: pre-integration-test
📌 Purpose:
- Prepares for integration tests (e.g., start a database server).
🔹 Phase 19: integration-test
📌 Purpose:
- Runs integration tests.
📌 Goal:
maven-failsafe-plugin:integration-test
📌 Tasks:
- Runs integration tests that depend on external systems (database, API).
🔹 Phase 20: post-integration-test
📌 Purpose:
- Cleans up after integration tests (e.g., stop test servers).
🔹 Phase 21: verify
📌 Purpose:
- Verifies package integrity (e.g., security, compliance checks).
🔹 Phase 22: install
📌 Purpose:
- Stores the JAR/WAR in the local repository (
~/.m2/repository
).
📌 Goal:
maven-install-plugin:install
🚀 Run manually:
mvn install
🔹 Phase 23: deploy
📌 Purpose:
- Uploads the JAR/WAR to a remote repository.
📌 Goal:
maven-deploy-plugin:deploy
🚀 Run manually:
mvn deploy
3️⃣ Summary
Phase | Main Goal | Purpose |
---|---|---|
compile |
maven-compiler-plugin:compile |
Compile Java code |
test |
maven-surefire-plugin:test |
Run unit tests |
package |
maven-jar-plugin:jar |
Create JAR/WAR |
install |
maven-install-plugin:install |
Store in local repo |
deploy |
maven-deploy-plugin:deploy |
Upload to remote repo |
Happy Coding! 🚀
Top comments (0)