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.
Top comments (0)