mvn clean and mvn install are two different Maven lifecycle goals: clean wipes out previous build output, while install builds the project and publishes the resulting artifact to the local repository. They are often run together but serve distinct purposes in the build process.
Key Points: • mvn clean deletes the target directory, removing compiled classes, packaged jars, and other generated build artifacts. • mvn install runs the full default lifecycle up through compile, test, and package before copying the artifact to the local Maven repository (~/.m2/repository). • Running mvn install alone does not remove stale files from a previous build; combining it as mvn clean install guarantees a fresh build. • install makes the artifact available as a dependency for other local projects, whereas clean only tidies up the workspace.
Example: On a typical CI job, a developer runs mvn clean install so that every build starts from a clean target directory and ends with a freshly installed jar that a downstream multi-module project can immediately depend on.
Interview Tip: A concise interview answer is:
"mvn clean removes previously generated build output like the target folder, while mvn install compiles, tests, packages the project, and copies the resulting artifact into the local repository so other projects can use it. In practice they're usually chained as mvn clean install to ensure a clean, reproducible build."