Explain the Maven lifecycle and its phases.

The Maven build lifecycle is a predefined, ordered sequence of phases that together define how a project moves from source code to a deployed artifact. Maven has three independent lifecycles — clean, default, and site — and running any phase automatically runs all preceding phases in that lifecycle.

Key Points: • The clean lifecycle removes previous build outputs (target directory) via the clean phase. • The default lifecycle handles the core build: validate, compile, test, package, verify, install, and deploy, in that order. • compile compiles source code; test runs unit tests via Surefire; package bundles compiled code into a JAR/WAR; install copies the artifact to the local repository; deploy uploads it to a remote repository. • The site lifecycle generates project documentation and reports, independent of the default lifecycle. • Running a later phase, like mvn package, automatically executes every earlier phase in that lifecycle first (validate, compile, test), so you rarely invoke phases individually out of order. • Plugins bind their goals to specific phases, which is how tools like Checkstyle or Surefire get triggered automatically at the right point.

Example: Running mvn install on a project automatically triggers validate, compile, test, package, and verify first, meaning a single command takes source code all the way to a tested, packaged JAR sitting in the local repository ready for other projects to consume.

Interview Tip: A concise interview answer is:

"Maven has three lifecycles — clean, default, and site — and the default lifecycle is the important one, running through validate, compile, test, package, verify, install, and deploy in order. Invoking any phase automatically runs everything before it in that lifecycle, which is why mvn install alone takes you from source to a tested, installed artifact."