Interview Prep/Build Tools (Java, Node.js, Python, .NET)/Java builds (Maven, Gradle, JAR, WAR)

What is the purpose of mvn clean?

BeginnerConceptJava builds (Maven, Gradle, JAR, WAR)
Answer

Short answer

mvn clean deletes the target/ folder — all compiled classes, test reports and old JARs — so the next build starts from scratch. It prevents stale files from an earlier build ending up in your artifact.

Example

mvn clean            # just delete target/
mvn clean package    # delete, then build fresh

Why it matters

Imagine you deleted a class from the source code. Without clean, the old .class file is still in target/classes and could end up in the JAR. With clean, only what's in the source today gets built.

In pipelines

CI agents are often reused, so pipelines almost always run mvn clean package or mvn clean verify. On fresh containers (like GitHub Actions runners) target/ doesn't exist yet, but clean is still a harmless, good habit.

What to say in the interview

"mvn clean removes the target directory so we don't package stale classes from a previous build. On reused CI agents that's important for reproducible builds."

All Build Tools (Java, Node.js, Python, .NET) questions Roadmap: Java apps: Maven, Gradle, JAR & WAR

More on Java builds (Maven, Gradle, JAR, WAR)