Maven has three built-in lifecycles. Name them and explain how 'clean' and 'site' relate to the default lifecycle and its goal bindings.
answer
- clean / default / site
- independent phase lists
- clean -> clean:clean
- site -> site:site
- packaging only affects default
- clean not implicit
basics
~10 sThe three lifecycles are clean, default, and site. They are independent. mvn clean package runs the clean lifecycle then the default lifecycle. clean binds clean:clean; site binds site:site.
solid answer
~40 sMaven ships three separate lifecycles: **clean** (pre-build cleanup), **default** (the main build: compile -> test -> package -> install -> deploy), and **site** (project documentation). They are independent sequences with their own phases and their own default goal bindings. The clean lifecycle's `clean` phase binds `clean:clean` (deletes `target/`). The site lifecycle's `site` phase binds `site:site`, and `site-deploy` binds `site:deploy`. On the command line you list phases from any lifecycle and Maven runs each in order, so `mvn clean install` first runs the clean lifecycle up to `clean`, then the default lifecycle up to `install`. Crucially, invoking a default phase does NOT auto-run clean — you must request it explicitly. Each lifecycle is its own ordered phase list with packaging- or plugin-supplied bindings.
code
bash · 3 lines# Each token is a phase from some lifecycle; run left to right:
mvn clean install # clean lifecycle, then default lifecycle
mvn site # only the site lifecyclego deeper
Name the three lifecycles and know clean deletes target.
Explain they are independent and how mvn clean install chains two of them.
Reason about when to skip clean for incremental speed vs force a clean build.
Define team norms (e.g., CI always cleans, local iterates dirty) and understand lifecycle extension for custom packagings.
## Three independent lifecycles Maven defines three built-in lifecycles. Each is a separate ordered list of phases with its own default goal bindings: ### 1. clean lifecycle Phases: `pre-clean` -> `clean` -> `post-clean`. Default binding: `clean` phase -> **`clean:clean`** (maven-clean-plugin), which deletes the `target/` build directory. ### 2. default lifecycle The main build. Phases (abbreviated): `validate`, `compile`, `test`, `package`, `verify`, `install`, `deploy`. Its bindings come from `<packaging>` (e.g., jar binds `compiler:compile`, `surefire:test`, `jar:jar`, `install:install`, `deploy:deploy`). The default lifecycle is the **only** one whose bindings depend on packaging. ### 3. site lifecycle Phases: `pre-site` -> `site` -> `post-site` -> `site-deploy`. Default bindings: `site` -> **`site:site`** (generate docs/reports), `site-deploy` -> `site:deploy` (publish them). ## They are independent Running a phase only advances **its own** lifecycle. `mvn install` runs the default lifecycle up to install; it does **not** run clean. To combine, list phases from multiple lifecycles in one command: ```bash mvn clean install # clean lifecycle (up to clean) THEN default (up to install) mvn clean verify site # clean, then default up to verify, then site up to site ``` Maven processes the requested phases left to right, fully resolving each lifecycle's chain. ## Why clean is separate Cleaning is destructive and slow (full recompile next time), so Maven never does it implicitly. Incremental builds (`mvn package` without `clean`) reuse `target/`. You opt into a fresh build with `clean`. ## Bindings recap by lifecycle | Lifecycle | Key phase | Default goal | |-----------|-----------|--------------| | clean | clean | clean:clean | | default | package | jar:jar / war:war (per packaging) | | site | site | site:site | Understanding the three-lifecycle model explains why `clean` and `package` are written together and why packaging only affects the default lifecycle's bindings.
- Does `mvn package` delete the target directory first?No. Cleaning is a separate lifecycle; you must add `clean` explicitly (`mvn clean package`) to delete target/.
- Which lifecycle's bindings depend on the packaging type?Only the default lifecycle. clean and site have fixed bindings (clean:clean, site:site) regardless of packaging.
saying these in an interview costs you the question
- Saying there is one big lifecycle that includes clean
- Thinking `mvn install` cleans first
- Believing packaging changes the clean or site bindings