DEV Community

Hunor Vadasz-Perhat
Hunor Vadasz-Perhat

Posted on

maven-002: maven-lifecycle-phases-by-lifecycles

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

πŸ‘‰ 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
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ Runs: validate β†’ initialize β†’ process-sources β†’ process-resources β†’ compile

mvn package
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ Runs: Everything up to package, creating a .jar or .war file.

mvn install
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ Runs: Everything up to install, storing the artifact in your local Maven repository.

mvn deploy
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ 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
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ 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
Enter fullscreen mode Exit fullscreen mode

Maven automatically executes:

  1. validate β†’ Checks project structure.
  2. compile β†’ Compiles Java files.
  3. test β†’ Runs unit tests.
  4. package β†’ Creates a .jar or .war.
  5. 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
Enter fullscreen mode Exit fullscreen mode

This executes:

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

Top comments (0)