Maven Lifecycle Phases Explained
Fast recall
Quick revision
Core answer
Whether you understand Maven's opinionated build model and can predict what happens when you run a specific phase.
Thinking `mvn test` only runs tests — it also runs validate, initialize, generate-sources, process-sources, generate-resources, process-resources, and compile before reaching the test phase.
Mention the three built-in lifecycles (default, clean, site), that plugin goals bind to specific phases, and that `mvn install -DskipTests` skips test execution but not compilation of test sources.
Interview-ready guide
Interview answer
Maven's lifecycle is the mental model that explains why running mvn test also compiles your code, and why running mvn package runs your tests. Phases are not independent commands — they cascade.
The cascade rule
- Running any phase executes every preceding phase in the lifecycle first —
mvn testtriggers validate, compile, process-test-resources, test-compile, and then test; a compile error surfaces when you runmvn testbecause compile runs first - The three lifecycles are independent and do not cascade into each other:
default(build and deploy),clean(remove target/), andsite(documentation);mvn clean packageis two separate lifecycle invocations, not one - This is why
mvn clean packageis the CI standard and not justmvn package— withoutclean, leftover compiled classes from a renamed or deleted source file can silently stay in the build
Phases are empty hooks bound by plugins
- A phase by itself does nothing; it is a slot that plugin goals bind to —
maven-compiler-plugin:compilebinds tocompile,maven-surefire-plugin:testbinds totest - You bind custom goals to phases in the POM: adding JaCoCo to the
testphase generates coverage data automatically without a separate Maven invocation mvn help:describe -Dcmd=testlists every goal bound to a given phase, which is how you diagnose why a phase is slower than expected
The most useful phase distinctions
packagecreates the JAR without installing to~/.m2;installadds it to the local repository so other local projects can depend on it;deployuploads to a remote repository for other teamsverifyruns integration tests and quality checks afterpackage— usemvn clean verifyin CI, notmvn clean package, so integration tests and coverage gates are part of every build
Maven Lifecycle Phases Explained
Direct Answer
Maven's default lifecycle has 23 phases executed in a fixed order. The key phases are validate (check project is correct), compile (compile source code), test (run unit tests), package (create JAR/WAR), verify (run integration tests), install (copy to local repository), and deploy (push to remote repository).
Common Mistakes & Pitfalls
Thinking mvn test only runs tests — it also runs validate, initialize, generate-sources, process-sources, generate-resources, process-resources, and compile before reaching the test phase.
Check your understanding
Answer these without looking back at the article.
- What is the difference between the `verify` and `install` phases?
- How do you bind a custom plugin goal to a specific lifecycle phase?
- What is the difference between `-DskipTests` and `-Dmaven.test.skip=true`?
Related interview prep categories
1Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.