skip to content

Describe the clean and site lifecycles, including their phases, and explain why 'mvn clean package' is a common command.

level: middleimportance: should knowfreq 45%

answer

  1. clean: pre-clean/clean/post-clean
  2. clean deletes target
  3. site: pre-site/site/post-site/site-deploy
  4. clean package = fresh reproducible build
  5. lifecycles independent so name clean

basics

~20 s

The clean lifecycle (pre-clean, clean, post-clean) deletes the target directory. The site lifecycle (pre-site, site, post-site, site-deploy) builds project docs. 'mvn clean package' is common because it deletes old output first, then does a fresh build.

solid answer

~40 s

The clean lifecycle has three phases: pre-clean, clean, post-clean. Its clean phase is bound to maven-clean-plugin:clean, which deletes the target directory so stale class files or old artifacts don't pollute the next build. The site lifecycle has pre-site, site, post-site, and site-deploy; maven-site-plugin generates an HTML documentation site with reports (javadoc, test results, dependency info) and site-deploy can publish it. 'mvn clean package' is idiomatic because package alone reuses whatever is in target; running clean first guarantees a reproducible, from-scratch build that won't include leftover or removed files. In CI you typically run 'mvn clean verify' or 'mvn clean deploy' for the same reason. Because lifecycles are independent, you must name clean explicitly — package never triggers it.

code

bash · 3 lines
bash
mvn clean package   # clean lifecycle (delete target) then default through package
mvn site            # generate docs into target/site
mvn site-deploy     # publish generated site

go deeper

for a junior

Know clean deletes target and site builds docs.

for a middle

List the phases of each and explain why clean package is used.

for a senior

Weigh build time vs reproducibility: when to skip clean locally but enforce it in CI.

for a principal

Standardize CI commands org-wide and decide whether site generation is part of the pipeline.

## The clean lifecycle Purpose: remove the outputs of previous builds. Phases in order: - **pre-clean** — hook for work before cleaning. - **clean** — bound to `maven-clean-plugin:clean`, deletes the `target/` directory. - **post-clean** — hook after cleaning. Why it matters: an incremental build reuses `target/`. If you renamed or deleted a source file, its old compiled `.class` can linger and mask problems, or an old artifact version can be packaged. A clean guarantees a deterministic, from-scratch build. ## The site lifecycle Purpose: generate a project documentation website. Phases in order: - **pre-site** - **site** — `maven-site-plugin:site` renders project info, reports (Javadoc, Surefire test reports, dependency reports) into `target/site`. - **post-site** - **site-deploy** — publishes the generated site to a server/repository. The site lifecycle is less used day-to-day but valued for auto-generated docs in larger projects. ## Why 'mvn clean package' is idiomatic Because the three lifecycles are **independent**, `mvn package` does NOT clean first — it builds on top of whatever already sits in `target/`. Prepending `clean` ensures: - no stale compiled classes from removed/renamed sources, - no leftover artifacts of a previous version, - reproducible results, especially in CI. ```bash mvn clean package # delete target, then full default build through package mvn clean verify # common CI command (adds integration tests) mvn clean install # fresh build into the local repo ``` ## Gotcha `clean` and the default phase are processed left to right: Maven runs the clean lifecycle to its `clean` phase, then the default lifecycle to `package`.

  • Why not just always rely on incremental builds without clean?
    Incremental builds can keep stale .class files from deleted/renamed sources or old artifacts; clean guarantees a deterministic from-scratch build, important for CI reproducibility.
  • What does site-deploy do versus deploy?
    site-deploy publishes the generated documentation site; the default lifecycle's deploy publishes the built artifact to an artifact repository.

saying these in an interview costs you the question

  • Saying clean compiles or tests the project
  • Confusing site-deploy (docs) with deploy (artifact)
  • Assuming package automatically cleans target
  • Listing clean phases in the wrong order

context