> ## Content Index
> Fetch the complete content index at: https://www.javahandbook.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# Common Maven Commands
- URL: https://www.javahandbook.com/maven-course/common-maven-commands/
- Published: 2025-08-17T04:02:44.000Z
- Updated: 2025-08-17T06:12:02.000Z
- Author: Gopi Gorantala
- Tags: Maven Project, #docs, #maven-course

Apache Maven is one of the most popular **build automation and dependency management tools** in the Java ecosystem.  
While Maven offers a wide range of commands, a handful of them are used daily by Java developers when building, testing, and managing projects.

In this lesson, we’ll cover the **most common Maven commands**:

- `mvn clean install`
- `mvn package`
- `mvn test`
- `mvn dependency:tree`

## **1\. `mvn clean install`**

This is probably the most widely used Maven command. It performs **two significant steps**:

1. `clean` → Deletes the `target/` directory from previous builds (removes compiled classes, JARs, reports, etc.).
2. `install` → Compiles, tests, packages the project, and then installs the final artifact (JAR/WAR) into the **local Maven repository** (`~/.m2/repository`).

This makes the artifact available for reuse by other local Maven projects.

Example: 

```java
mvn clean install
```

When to Use It?

- Before deploying a project locally for use in other projects
- In **CI/CD pipelines**, to ensure fresh builds without leftover files
- When you want to test the full lifecycle: clean → compile → test → package → install

## **2\. `mvn package`**

The `package` command compiles source code, runs unit tests, and then packages the compiled code into a **distributable format** like:

- `.jar` (Java Archive)
- `.war` (Web Application Archive)
- `.ear` (Enterprise Archive)

The packaged file will be placed inside the `target/` folder.

Example

```bash
mvn package
```

When to use it?

- When you only need to generate the final artifact (JAR/WAR) but don’t need to install it to the local repo
- Helpful in creating build artifacts during development

## **3\. `mvn test`**

The `test` command runs all **unit tests** in the project using the **Surefire Plugin** by default.

- Test classes are located in `src/test/java`
- Test resources are in `src/test/resources`

Example

```bash
mvn test
```

When to Use It?

- To verify your code changes are working before packaging or installing
- As part of a CI pipeline for **continuous testing**
- To ensure new features or bug fixes don’t break existing code

> **Tip:* To skip tests, run:* `mvn install -DskipTests`

---

## **4\. `mvn dependency:tree`**

This command shows a **hierarchical view of dependencies** in your project.  
It helps developers:

- See which libraries are included (direct and transitive dependencies)
- Identify version conflicts
- Debug dependency-related issues

Example:

```bash
mvn dependency:tree
```

**Sample Output:**

```log
[INFO] com.example:my-app:jar:1.0-SNAPSHOT
[INFO] └─ org.springframework.boot:spring-boot-starter-web:jar:3.1.0:compile
[INFO]    ├─ org.springframework.boot:spring-boot-starter:jar:3.1.0:compile
[INFO]    ├─ org.springframework.boot:spring-boot:jar:3.1.0:compile
[INFO]    └─ com.fasterxml.jackson.core:jackson-databind:jar:2.15.2:compile

```

When to use it?

- To **debug dependency conflicts** (multiple versions of the same library)
- To optimize and remove **unused dependencies**
- To understand transitive dependencies automatically added by Maven

## **Key Takeaways**

- **`mvn clean install`** → Cleans old builds and installs artifacts into the local repository
- **`mvn package`** → Compiles and packages your project into JAR/WAR
- **`mvn test`** → Runs unit tests with Surefire plugin
- **`mvn dependency:tree`** → Shows dependencies and resolves conflicts

Mastering these commands will make you more efficient and help you build reliable Java applications with Maven.