What is Maven Lifecycle?
Maven's build lifecycle is a well-defined sequence of phases that dictate how a project is built and distributed. Understanding these phases helps you control and customize your build process.
Three Built-in Lifecycles
- default - Handles project build and deployment
- clean - Handles project cleaning
- site - Handles project documentation
Default Lifecycle Phases
# Complete list of default lifecycle phases (in order):
validate # Validate project is correct
initialize # Initialize build state
generate-sources # Generate source code
process-sources # Process source code
generate-resources # Generate resources
process-resources # Copy resources to output directory
compile # Compile source code
process-classes # Post-process compiled classes
generate-test-sources # Generate test source code
process-test-sources # Process test source code
generate-test-resources # Generate test resources
process-test-resources # Copy test resources
test-compile # Compile test source code
process-test-classes # Post-process test classes
test # Run unit tests
prepare-package # Prepare for packaging
package # Create JAR/WAR package
pre-integration-test # Setup for integration tests
integration-test # Run integration tests
post-integration-test # Cleanup after integration tests
verify # Run checks to verify package
install # Install to local repository
deploy # Deploy to remote repository
Key Phases You'll Use Most
# Commonly used phases:
mvn compile # Compile main source code
mvn test # Compile and run unit tests
mvn package # Create JAR/WAR file
mvn verify # Run all checks including integration tests
mvn install # Install to local ~/.m2 repository
mvn deploy # Deploy to remote repository
Phase Execution
When you run a phase, Maven executes all preceding phases. Running mvn package executes: validate → compile → test → package.
Clean Lifecycle
# Clean lifecycle phases:
pre-clean # Execute before cleaning
clean # Remove target/ directory
post-clean # Execute after cleaning
# Usage:
mvn clean # Delete target/ directory
mvn clean package # Clean then build package
mvn clean install # Clean then build and install
Site Lifecycle
# Site lifecycle phases:
pre-site # Execute before site generation
site # Generate project documentation
post-site # Execute after site generation
site-deploy # Deploy site to web server
# Usage:
mvn site # Generate site in target/site/
mvn site:run # Start local server to view site
Binding Goals to Phases
<!-- Plugin goals bound to specific phases -->
<build>
<plugins>
<!-- Runs during 'test' phase by default -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.1.2</version>
</plugin>
<!-- Custom binding: run checkstyle during validate phase -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>3.3.0</version>
<executions>
<execution>
<id>validate</id>
<phase>validate</phase> <!-- Bind to phase -->
<goals>
<goal>check</goal> <!-- Goal to run -->
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Common Plugin-Phase Bindings
# Default bindings for JAR packaging:
process-resources → resources:resources
compile → compiler:compile
process-test-resources → resources:testResources
test-compile → compiler:testCompile
test → surefire:test
package → jar:jar
install → install:install
deploy → deploy:deploy
Running Specific Goals
# Run a specific plugin goal (not a phase)
mvn compiler:compile # Just compile, no preceding phases
mvn surefire:test # Just test, no preceding phases
mvn jar:jar # Just package JAR
# Useful standalone goals
mvn dependency:tree # Show dependency tree
mvn dependency:analyze # Analyze dependency usage
mvn versions:display-dependency-updates # Check for updates
mvn help:effective-pom # Show resolved POM
mvn help:active-profiles # Show active profiles
Skipping Phases
# Skip tests
mvn package -DskipTests # Compile tests but don't run
mvn package -Dmaven.test.skip=true # Don't compile or run tests
# Skip specific plugins
mvn package -Dcheckstyle.skip=true # Skip checkstyle
mvn package -Dpmd.skip=true # Skip PMD
mvn package -Dspotbugs.skip=true # Skip SpotBugs
# Skip in POM (not recommended for CI)
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
Integration Test Phases
<!-- Failsafe plugin for integration tests -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>3.1.2</version>
<executions>
<execution>
<goals>
<goal>integration-test</goal> <!-- Run during integration-test phase -->
<goal>verify</goal> <!-- Verify during verify phase -->
</goals>
</execution>
</executions>
</plugin>
<!-- Test naming conventions -->
<!-- Surefire (unit tests): *Test.java, Test*.java -->
<!-- Failsafe (integration): *IT.java, IT*.java -->
# Commands
mvn test # Run unit tests only
mvn verify # Run unit AND integration tests
mvn integration-test # Run integration tests (may not verify)
Multi-Module Builds
# Building multi-module projects
mvn install # Build all modules in order
mvn install -pl module-a # Build only module-a
mvn install -pl module-a -am # Build module-a AND its dependencies
mvn install -pl module-a -amd # Build module-a AND modules depending on it
mvn install -rf :module-b # Resume from module-b
# Options:
# -pl (--projects) : Build specific modules
# -am (--also-make) : Also build required modules
# -amd (--also-make-dependents) : Also build dependent modules
# -rf (--resume-from) : Resume from specified module
Lifecycle Reference
| Phase | Purpose | Common Command |
|---|---|---|
| validate | Check project is correct | mvn validate |
| compile | Compile source code | mvn compile |
| test | Run unit tests | mvn test |
| package | Create JAR/WAR | mvn package |
| verify | Run integration tests | mvn verify |
| install | Install to local repo | mvn install |
| deploy | Deploy to remote repo | mvn deploy |