Maven is a build automation tool used primarily for Java projects. It follows a lifecycle-based approach, where tasks are executed in predefined phases.
- Automates project build, testing, and deployment
- Uses a standard lifecycle for consistency
- Simplifies dependency and project management
Maven Build Lifecycle
Maven has three built-in lifecycles. When you run a command, you are triggering one of these.
1. The default Lifecycle (The Main Build)
The default lifecycle is the core Maven lifecycle that handles everything from compiling the source code to deploying the application. It consists of multiple phases (around 23 in total), but the following are the 7 most important phases you should know:

- validate: Checks if the project structure is correct and all dependencies are downloaded.
- compile: Compiles the source code (src/main/java) into bytecode (target/classes).
- test: Runs unit tests (src/test/java) using frameworks like JUnit. These tests should not require the code to be packaged.
- package: Takes the compiled code and bundles it into its distributable format, such as a JAR or WAR file.
- verify: Runs integration tests to verify the quality of the package.
- install: Installs the package into your Local Repository (~/.m2), making it available for other projects on your machine to use as a dependency.
- deploy: Copies the final package to a Remote Repository (like Nexus or Artifactory) for sharing with other developers.
Key Concept: The lifecycle is sequential. If you run
mvn install, Maven automatically runsvalidate,compile,test,package, andverifyfirst. You don't need to type them all.
2. The clean Lifecycle
- This handles project cleanup.
- clean: Deletes the target/ directory, removing all files generated by the previous build. This ensures you are starting from a fresh state.
3. The site Lifecycle
- This generates project documentation.
- site: Generates a website based on the project's information (dependencies, unit test reports, etc.).
Phases vs Goals
This is where most beginners get confused.
- Phase: A step in the lifecycle (e.g., compile). It is a logical concept, like "Cooking Dinner."
- Goal: The actual task that does the work (e.g., compiler:compile). It is the specific action, like "Chopping Vegetables."
A Phase is just a placeholder. Maven binds specific Plugin Goals to these phases to do the work.
- The
compilePhase triggers thecompiler:compileGoal. - The
testPhase triggers thesurefire:testGoal.
Essential Maven Commands Cheatsheet
| Command | What it Does | When to Use |
|---|---|---|
mvn clean | Deletes the target folder. | Before any new build to ensure no stale files remain. |
mvn compile | Compiles source code only. | When you just want to check for syntax errors. |
mvn package | Compiles + Tests + Builds JAR/WAR. | When you want to run the app locally or send the JAR to a colleague. |
mvn install | Packages + Copies to ~/.m2. | When another project on your machine depends on this one. |
mvn deploy | Packages + Uploads to Nexus/Artifactory. | When you are releasing code for the rest of the team (CI/CD server usually does this). |
mvn clean install | The "Gold Standard" command. | Cleans everything and rebuilds from scratch. Use this 90% of the time. |
mvn test | Compiles + Runs Unit Tests. | When you are doing TDD or verifying changes. |
Important Command Variations
Clean + Build
Generally when we run any of the above commands, we add the mvn clean step so that the target folder generated from the previous build is removed before running a newer build. This is how the command would look on integrating the clean step with install phase:
mvn clean install
Debug Mode
Similarly, if we want to run the step in debug mode for more detailed build information and logs, we will add -X to the actual command. Hence, the install step with debug mode on will have the following command:
mvn -X install
Skip Tests
Consider a scenario where we do not want to run the tests while packaging or installing the Java project. In this case, we use -DskipTests along with the actual command. If we need to run the install step by skipping the tests associated with the project, the command would be:
mvn install -DskipTests