# Where to Find the MavenSession Class in Apache Maven

> Locate the MavenSession class in Apache Maven at its official repository path. Understand its role in managing build execution context and project data for efficient Maven development.

- Repository: [The Apache Software Foundation/maven](https://github.com/apache/maven)
- Tags: internals
- Published: 2026-07-06

---

**The `MavenSession` class resides in [`impl/maven-core/src/main/java/org/apache/maven/execution/MavenSession.java`](https://github.com/apache/maven/blob/main/impl/maven-core/src/main/java/org/apache/maven/execution/MavenSession.java) and acts as the central runtime state container that carries execution context, project data, and repository information throughout a Maven build lifecycle.**

The `MavenSession` class is the core representation of a Maven build within the **apache/maven** repository. Located in the `org.apache.maven.execution` package, this class holds the execution request, current project, repository system session, and build results needed by plugins and core components during a build.

## MavenSession Location in the Source Tree

### Core Implementation File

The primary source file for the `MavenSession` class is:

```

impl/maven-core/src/main/java/org/apache/maven/execution/MavenSession.java

```

This file contains the main class definition that implements the session contract. It is part of the **impl/maven-core** module, which houses Maven's core implementation classes.

### Package and Module Structure

`MavenSession` belongs to the `org.apache.maven.execution` package. This package aggregates execution-related classes including the request, result, and session builders. The module structure places the implementation in `impl/maven-core`, separating it from the API definitions found in `api/maven-api`.

## How MavenSession Is Constructed

Maven does not instantiate `MavenSession` directly through constructors. Instead, the framework uses the **`MavenSessionBuilder`** class to assemble the session.

The builder pattern follows this workflow:

1. **MavenSessionBuilder** assembles the execution request, repository system session, and component lookup data
2. **MavenSessionBuilderSupplier** (located in [`impl/maven-impl/src/main/java/org/apache/maven/impl/resolver/MavenSessionBuilderSupplier.java`](https://github.com/apache/maven/blob/main/impl/maven-impl/src/main/java/org/apache/maven/impl/resolver/MavenSessionBuilderSupplier.java)) exposes the builder to Plexus components
3. The `build()` method produces a fully configured `MavenSession` instance

This construction ensures that all required runtime state—including the `RepositorySystemSession` and `MavenExecutionRequest`—is properly initialized before plugins or lifecycle phases execute.

## Key Responsibilities of MavenSession

The `MavenSession` class manages several critical aspects of build state:

- **Execution Request Storage**: Holds the original `MavenExecutionRequest` containing goals, active profiles, user properties, and command-line options
- **Current Project Access**: Provides the `MavenProject` instance via `getCurrentProject()` that represents the project currently being built
- **Repository System Integration**: Contains the `RepositorySystemSession` used for artifact resolution, deployment, and local repository management
- **Result Tracking**: Accumulates a `MavenResult` object that records build success or failure, execution time, and collected exceptions
- **Component Lookup**: Acts as a Plexus container proxy, enabling plugins to obtain services and dependencies through the session
- **Thread-Local Context**: Stores transient data such as the current `MojoExecution` and `ProjectBuildingRequest` for multi-threaded builds

## Working with MavenSession in Code

### Creating a MavenSession Instance

While typically constructed by Maven's core internals, the following example illustrates how `MavenSessionBuilder` assembles a session programmatically:

```java
import org.apache.maven.execution.MavenSession;
import org.apache.maven.execution.MavenSessionBuilder;
import org.apache.maven.execution.MavenExecutionRequest;
import org.apache.maven.execution.DefaultMavenExecutionRequest;
import org.apache.maven.repository.internal.MavenRepositorySystemUtils;
import org.eclipse.aether.RepositorySystem;
import org.eclipse.aether.repository.LocalRepository;

import java.io.File;
import java.util.List;

// 1. Build the execution request (normally parsed from CLI arguments)
MavenExecutionRequest request = new DefaultMavenExecutionRequest();
request.setBaseDirectory(new File("."));
request.setGoals(List.of("clean", "install"));

// 2. Prepare the repository system session
RepositorySystem repoSystem = // obtained from Plexus container
LocalRepository localRepo = new LocalRepository("target/local-repo");
org.eclipse.aether.RepositorySystemSession repoSession =
        MavenRepositorySystemUtils.newSession()
                .setLocalRepositoryManager(repoSystem.newLocalRepositoryManager(
                        MavenRepositorySystemUtils.newSession(), localRepo));

// 3. Build the MavenSession
MavenSessionBuilder builder = new MavenSessionBuilder()
        .setRequest(request)
        .setRepositorySession(repoSession);
MavenSession session = builder.build();

// Typical usage inside a plugin/mojo:
MavenProject current = session.getCurrentProject();
System.out.println("Building project: " + current.getArtifactId());

```

### Accessing Session Data in a Mojo

Plugin developers receive the session through dependency injection using the `${session}` expression:

```java
import org.apache.maven.execution.MavenSession;
import org.apache.maven.execution.MavenResult;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;

@Mojo(name = "example")
public class ExampleMojo extends AbstractMojo {

    @Parameter(defaultValue = "${session}", readonly = true)
    private MavenSession session;

    public void execute() throws MojoExecutionException {
        // Access current project
        MavenProject project = session.getCurrentProject();
        getLog().info("Project version: " + project.getVersion());

        // Check build result
        MavenResult result = session.getResult();
        if (result.hasExceptions()) {
            result.getExceptions().forEach(e -> getLog().error(e));
        }
    }
}

```

## Related Source Files

Several companion classes work alongside `MavenSession` to manage build execution:

- **[`impl/maven-core/src/main/java/org/apache/maven/execution/MavenSessionBuilder.java`](https://github.com/apache/maven/blob/main/impl/maven-core/src/main/java/org/apache/maven/execution/MavenSessionBuilder.java)** – Constructs and configures `MavenSession` instances
- **[`impl/maven-impl/src/main/java/org/apache/maven/impl/resolver/MavenSessionBuilderSupplier.java`](https://github.com/apache/maven/blob/main/impl/maven-impl/src/main/java/org/apache/maven/impl/resolver/MavenSessionBuilderSupplier.java)** – Supplies the builder to Plexus components
- **[`api/maven-api/src/main/java/org/apache/maven/execution/MavenExecutionRequest.java`](https://github.com/apache/maven/blob/main/api/maven-api/src/main/java/org/apache/maven/execution/MavenExecutionRequest.java)** – Defines the request data passed into the session
- **[`api/maven-api/src/main/java/org/apache/maven/execution/MavenResult.java`](https://github.com/apache/maven/blob/main/api/maven-api/src/main/java/org/apache/maven/execution/MavenResult.java)** – Captures build outcomes and exceptions

## Summary

- The **MavenSession class** is located at [`impl/maven-core/src/main/java/org/apache/maven/execution/MavenSession.java`](https://github.com/apache/maven/blob/main/impl/maven-core/src/main/java/org/apache/maven/execution/MavenSession.java) in the `org.apache.maven.execution` package
- It serves as the central state container holding the `MavenExecutionRequest`, current `MavenProject`, `RepositorySystemSession`, and `MavenResult`
- Instances are constructed via **MavenSessionBuilder** and supplied through **MavenSessionBuilderSupplier**
- Plugin developers access the session through the `${session}` parameter injection to retrieve project data and build status
- Related classes in `api/maven-api` define the request and result contracts while the implementation resides in `impl/maven-core`

## Frequently Asked Questions

### What package contains the MavenSession class in Apache Maven?

The `MavenSession` class resides in the `org.apache.maven.execution` package within the `impl/maven-core` module. This package groups execution-related classes including the session builder and result collectors.

### How is MavenSession created during a build?

Maven creates the session through the `MavenSessionBuilder` class, which assembles the execution request, repository system session, and component lookup data. The `MavenSessionBuilderSupplier` provides this builder to the Plexus container, which then produces the session instance before the build lifecycle begins.

### Can plugin developers modify the MavenSession during execution?

Plugin developers can read from the session via parameter injection, but modifying core session state is generally discouraged. The session is marked as read-only in most contexts to ensure build consistency, though certain advanced extensions may access mutator methods on specific session components.

### What is the difference between MavenSession and MavenExecutionRequest?

`MavenExecutionRequest` represents the static configuration and command-line input provided at build start, while `MavenSession` is the dynamic runtime container that evolves during the build. The session holds the request, but also maintains the current project, repository session, and accumulated results as the build progresses through lifecycle phases.