When you run 'mvn package', what exactly executes, and why? Explain the cumulative nature of phases.
answer
- name a phase = run it + all before it
- fail-fast: test failure stops before package
- skipTests vs maven.test.skip
- other lifecycles not auto-included
- goals run in binding order
basics
~10 sInvoking a phase runs that phase AND every phase before it in the same lifecycle, in order. So 'mvn package' runs validate, compile, and test before package, executing all goals bound to each.
solid answer
~50 sMaven phases are cumulative within a lifecycle. When you name a phase on the command line, Maven executes every phase from the start of that lifecycle up to and including the one you named, in their defined order, running all plugin goals bound to each phase. So 'mvn package' triggers validate → compile → test → package: it compiles main code, runs unit tests, then packages. This is why a failing unit test stops the build before any jar is produced. The same applies to 'mvn deploy', which runs the entire default sequence. It is also why people sometimes use '-DskipTests' — to reach package/install without paying for the test phase. Note phases from OTHER lifecycles are not pulled in: 'mvn package' does not clean, so you add 'clean' explicitly. Each lifecycle is traversed independently when multiple are requested.
code
bash · 3 linesmvn package # runs validate, compile, test, package
mvn clean package # adds the clean lifecycle first
mvn package -DskipTests # reach package but skip running testsgo deeper
State that running a phase runs all earlier phases too.
Explain fail-fast, skipTests vs maven.test.skip, and that other lifecycles aren't included.
Discuss CI command design (clean verify vs install vs deploy) and when skipping tests is acceptable.
Define standard pipeline phase targets and guardrails so teams don't ship with tests skipped.
## Phases are cumulative The single most important behavioral rule: **invoking a phase executes all preceding phases in that lifecycle, in order, then the phase itself.** You don't list every step — you name the furthest point you want to reach. So for the default lifecycle: - `mvn validate` → validate - `mvn compile` → validate, compile - `mvn test` → validate, compile, test - `mvn package` → validate, compile, test, package - `mvn verify` → … through verify - `mvn install` → … through install - `mvn deploy` → the whole chain At each phase, **every goal bound to it** runs (in binding order). The lifecycle is the ordering mechanism; goals do the work. ## Consequences - A **failed unit test** at the `test` phase aborts the build, so `package` never runs and no jar is created. This is intentional fail-fast behavior. - To skip tests but still build, use `-DskipTests` (compiles tests but skips running) or `-Dmaven.test.skip=true` (skips compiling+running). Use sparingly. - Other lifecycles are NOT pulled in. `mvn package` does not run `clean`. You write `mvn clean package` to get both. ## Multiple lifecycles on one command You can mix phases from different lifecycles. Maven runs each requested lifecycle in turn, each cumulatively: ```bash mvn clean deploy # clean lifecycle: pre-clean, clean # default lifecycle: validate ... package, verify, install, deploy ``` ## Multi-module note In a reactor (multi-module) build, Maven computes module order and then runs the requested phase across modules; within each module the cumulative rule still applies.
- Why does a broken unit test prevent a jar from being built with 'mvn package'?Because test runs before package; phases are cumulative and fail-fast, so a failure at test aborts the build before package executes.
- Does 'mvn package' delete the previous target directory?No. clean is a separate lifecycle and is not invoked automatically; use 'mvn clean package'.
Like floors in a building you must climb on foot: asking for floor 5 means you walk past floors 1-4 on the way up; you can't teleport to 5.
saying these in an interview costs you the question
- Saying you must list every phase explicitly
- Believing a later phase can run without its predecessors
- Assuming 'package' auto-cleans target
- Thinking -DskipTests also skips compiling tests (that's maven.test.skip)