> For the complete documentation index, see [llms.txt](https://www.pranaypourkar.co.in/the-programmers-guide/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://www.pranaypourkar.co.in/the-programmers-guide/maven/dependency-management/dependency-scope/maven-lifecycle-and-dependency-scope.md).

# Maven Lifecycle and Dependency Scope

## About

Maven organizes the **build process** into well-defined phases, allowing structured execution of tasks like **compilation, testing, packaging, and deployment**.

Maven has three primary lifecycles:

* **Clean**: Removes previous build artifacts.
* **Default (Build)**: Compiles, tests, packages, and deploys the project.
* **Site**: Generates documentation.

Among these, the **default lifecycle** consists of the following key phases:

<table data-header-hidden><thead><tr><th width="120.73046875"></th><th></th></tr></thead><tbody><tr><td><strong>Phase</strong></td><td><strong>Description</strong></td></tr><tr><td><code>validate</code></td><td>Ensures the project is correct and necessary information is available.</td></tr><tr><td><code>compile</code></td><td>Compiles source code of the project.</td></tr><tr><td><code>test</code></td><td>Runs unit tests using a testing framework (e.g., JUnit, TestNG).</td></tr><tr><td><code>package</code></td><td>Bundles compiled code into a distributable format (JAR, WAR, etc.).</td></tr><tr><td><code>verify</code></td><td>Runs integration tests to ensure package correctness.</td></tr><tr><td><code>install</code></td><td>Installs the package in the local repository (~/.m2) for use by other projects.</td></tr><tr><td><code>deploy</code></td><td>Uploads the package to a remote repository for sharing with others.</td></tr></tbody></table>

## Dependency Scope vs. Lifecycle Phases

Maven dependencies are **activated** based on their **scope** at different phases of the build lifecycle.

### **1. `compile` Phase**

* Converts `.java` source files to `.class` bytecode.
* Dependencies in **compile**, **provided**, and **system** scope are available.
* **test** and **runtime** dependencies are **not available**.

**Example:**

```sh
mvn compile
```

{% hint style="success" %}
Available Scopes: **compile, provided, system**\
Ignored Scopes: **test, runtime**
{% endhint %}

### **2. `test` Phase**

* Executes **unit tests** using JUnit/TestNG.
* Requires compiled source code from `compile` phase.
* Dependencies in **compile, provided, runtime, and test** scopes are available.

**Example:**

```sh
mvn test
```

{% hint style="success" %}
Available Scopes: **compile, provided, runtime, test**\
Ignored Scopes: **system** (not propagated)
{% endhint %}

**Example `pom.xml`:**

```xml
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.13.2</version>
    <scope>test</scope>
</dependency>
```

* JUnit is available **only** during test execution.
* It **won't be included** in the final JAR/WAR.

### **3. `package` Phase**

* Packages compiled code into a **JAR/WAR/ZIP**.
* Dependencies in **compile, runtime** scope are included in the package.
* **provided and test** dependencies are **not included**.

**Example:**

```sh
mvn package
```

{% hint style="success" %}
Available Scopes: **compile, runtime**\
Ignored Scopes: **test, provided**
{% endhint %}

### **4. `verify` Phase**

* Runs **integration tests** to verify the packaged application.
* Often used with frameworks like **Selenium, Cucumber, or REST Assured**.
* Dependencies in **compile, runtime, test** scopes are available.

**Example:**

```sh
mvn verify
```

{% hint style="success" %}
Available Scopes: **compile, runtime, test**\
Ignored Scopes: **provided, system**
{% endhint %}

### **5. `install` Phase**

* Deploys the built JAR/WAR into the **local Maven repository** (`~/.m2`).
* Useful for sharing dependencies across local projects.
* Dependencies in **compile, runtime** scope are packaged.

**Example:**

```sh
mvn install
```

{% hint style="success" %}
Available Scopes: **compile, runtime**\
Ignored Scopes: **test, provided, system**
{% endhint %}

### **6. `deploy` Phase**

* Uploads the package to a **remote repository** (like Nexus, Artifactory, or Maven Central).
* Used in **CI/CD pipelines** for distributing artifacts.
* Dependencies in **compile, runtime** scope are deployed.

**Example:**

```sh
mvn deploy
```

{% hint style="success" %}
Available Scopes: **compile, runtime**\
Ignored Scopes: **test, provided, system**
{% endhint %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://www.pranaypourkar.co.in/the-programmers-guide/maven/dependency-management/dependency-scope/maven-lifecycle-and-dependency-scope.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
