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)