Where to Find the MavenSession Class in Apache Maven
The MavenSession class resides in 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:
- MavenSessionBuilder assembles the execution request, repository system session, and component lookup data
- MavenSessionBuilderSupplier (located in
impl/maven-impl/src/main/java/org/apache/maven/impl/resolver/MavenSessionBuilderSupplier.java) exposes the builder to Plexus components - The
build()method produces a fully configuredMavenSessioninstance
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
MavenExecutionRequestcontaining goals, active profiles, user properties, and command-line options - Current Project Access: Provides the
MavenProjectinstance viagetCurrentProject()that represents the project currently being built - Repository System Integration: Contains the
RepositorySystemSessionused for artifact resolution, deployment, and local repository management - Result Tracking: Accumulates a
MavenResultobject 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
MojoExecutionandProjectBuildingRequestfor 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:
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:
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– Constructs and configuresMavenSessioninstancesimpl/maven-impl/src/main/java/org/apache/maven/impl/resolver/MavenSessionBuilderSupplier.java– Supplies the builder to Plexus componentsapi/maven-api/src/main/java/org/apache/maven/execution/MavenExecutionRequest.java– Defines the request data passed into the sessionapi/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.javain theorg.apache.maven.executionpackage - It serves as the central state container holding the
MavenExecutionRequest, currentMavenProject,RepositorySystemSession, andMavenResult - 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-apidefine the request and result contracts while the implementation resides inimpl/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.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →