DEV Community

Hunor Vadasz-Perhat
Hunor Vadasz-Perhat

Posted on

maven-004: deep-dive-into-default-build-lifecycle

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.
Enter fullscreen mode Exit fullscreen mode

🚀 Command Example:

mvn package
Enter fullscreen mode Exit fullscreen mode

👉 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:

  1. Verify the existence of pom.xml.
  2. 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:

  1. Create necessary directories (if needed).
  2. 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:

  1. Generate source code files dynamically.

🔹 Phase 4: process-sources

📌 Purpose:

  • Applies modifications to existing source code.

📌 Tasks:

  1. Apply formatting or transformation tools.

🔹 Phase 5: generate-resources

📌 Purpose:

  • Generates additional resource files (e.g., property files).

📌 Tasks:

  1. 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:

  1. Copy all resources from src/main/resources to target/classes.

🔹 Phase 7: compile

📌 Purpose:

  • Compiles Java source code into .class files.

📌 Goal:

  • maven-compiler-plugin:compile

📌 Tasks:

  1. Read Java source files from src/main/java.
  2. Compile them into .class files inside target/classes.

🔹 Phase 8: process-classes

📌 Purpose:

  • Runs additional processing on compiled .class files.

📌 Tasks:

  1. 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 to target/test-classes).

🔹 Phase 13: test-compile

📌 Purpose:

  • Compiles test source code.

📌 Goal:

  • maven-compiler-plugin:testCompile

📌 Tasks:

  1. 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:

  1. Execute JUnit/TestNG test cases.

🚀 Run tests manually:

mvn test
Enter fullscreen mode Exit fullscreen mode

🔹 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:

  1. Create target/my-app-1.0.jar or target/my-app-1.0.war.

🚀 Run manually:

mvn package
Enter fullscreen mode Exit fullscreen mode

🔹 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:

  1. 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
Enter fullscreen mode Exit fullscreen mode

🔹 Phase 23: deploy

📌 Purpose:

  • Uploads the JAR/WAR to a remote repository.

📌 Goal:

  • maven-deploy-plugin:deploy

🚀 Run manually:

mvn deploy
Enter fullscreen mode Exit fullscreen mode

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)