Back to results for “UI Testing”
Java › Build Tools: Maven & Gradle

What is the Maven build lifecycle, and what do the common phases (validate, compile, test, package, install, deploy) each do?

Intermediate

Answer

Maven's default lifecycle is a fixed, ordered sequence of phases, where running any given phase automatically executes all preceding phases in order: validate (checks project structure is correct), compile (compiles source code), test (runs unit tests via a test framework plugin like Surefire), package (bundles compiled code into a JAR/WAR), install (copies the package into the local ~/.m2 repository for use by other local projects), and deploy (publishes the package to a remote repository for sharing with other developers/environments) -- each phase is itself bound to specific plugin goals that do the actual work.
# Running 'mvn package' automatically runs validate, compile, and test first
mvn package

# Running 'mvn install' runs everything through package first, then installs to ~/.m2
mvn install

# Skip tests during a phase (use sparingly)
mvn package -DskipTests
Real-world example A CI pipeline runs `mvn clean install` on every pull request, which automatically compiles, tests, and packages the code in one command, failing the build immediately if any earlier phase (like a failing unit test) doesn't succeed, preventing broken code from progressing further in the pipeline.

Common follow-ups: What's the difference between skipping tests with -DskipTests versus -Dmaven.test.skip=true?;How would you bind a custom plugin goal to run during a specific lifecycle phase?