CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-org-apache-maven--maven-core

Maven Core classes - the core engine of Apache Maven build system

Pending
Overview
Eval results
Files

core-execution.mddocs/

Core Execution

Maven Core execution provides the primary interfaces for orchestrating Maven build processes. This includes the main Maven execution engine, session management, and request/response handling for build operations.

Core Maven Execution

Maven Interface

The primary entry point for executing Maven builds programmatically.

public interface Maven {
    /**
     * Execute a Maven build with the given request configuration.
     *
     * @param request the execution request containing build configuration
     * @return execution result containing build outcomes and any exceptions
     */
    MavenExecutionResult execute(MavenExecutionRequest request);
    
    @Deprecated
    String POMv4 = "4.0.0";
}

Usage Example:

import org.apache.maven.Maven;
import org.apache.maven.execution.DefaultMavenExecutionRequest;
import org.apache.maven.execution.MavenExecutionRequest;
import org.apache.maven.execution.MavenExecutionResult;

@Component
private Maven maven;

public void executeBuild() throws Exception {
    MavenExecutionRequest request = new DefaultMavenExecutionRequest();
    request.setPom(new File("pom.xml"));
    request.setGoals(Arrays.asList("clean", "compile", "test"));
    request.setBaseDirectory(new File("."));
    
    MavenExecutionResult result = maven.execute(request);
    
    if (result.hasExceptions()) {
        throw new Exception("Build failed", result.getExceptions().get(0));
    }
}

Default Maven Implementation

public class DefaultMaven implements Maven {
    // Primary implementation of Maven execution logic
    // Handles request processing, session management, and result aggregation
}

Execution Request Configuration

MavenExecutionRequest Interface

Comprehensive configuration interface for Maven execution requests.

public interface MavenExecutionRequest {
    // Build Configuration
    MavenExecutionRequest setPom(File pom);
    File getPom();
    
    MavenExecutionRequest setGoals(List<String> goals);
    List<String> getGoals();
    
    MavenExecutionRequest setSelectedProjects(List<String> selectedProjects);
    List<String> getSelectedProjects();
    
    MavenExecutionRequest setResumeFrom(String resumeFrom);
    String getResumeFrom();
    
    MavenExecutionRequest setMakeBehavior(String makeBehavior);
    String getMakeBehavior();
    
    // Directory Configuration
    MavenExecutionRequest setBaseDirectory(File baseDirectory);
    File getBaseDirectory();
    
    MavenExecutionRequest setMultiModuleProjectDirectory(File multiModuleProjectDirectory);
    File getMultiModuleProjectDirectory();
    
    // Repository Configuration
    MavenExecutionRequest setLocalRepository(ArtifactRepository localRepository);
    ArtifactRepository getLocalRepository();
    
    MavenExecutionRequest setRemoteRepositories(List<ArtifactRepository> repositories);
    List<ArtifactRepository> getRemoteRepositories();
    
    MavenExecutionRequest setPluginArtifactRepositories(List<ArtifactRepository> repositories);
    List<ArtifactRepository> getPluginArtifactRepositories();
    
    // Reactor Configuration
    static final String REACTOR_FAIL_FAST = "fail-fast";
    static final String REACTOR_FAIL_AT_END = "fail-at-end";
    static final String REACTOR_FAIL_NEVER = "fail-never";
    
    MavenExecutionRequest setReactorFailureBehavior(String failureBehavior);
    String getReactorFailureBehavior();
    
    // Threading Configuration
    MavenExecutionRequest setDegreeOfConcurrency(int degreeOfConcurrency);
    int getDegreeOfConcurrency();
    
    // Logging Configuration
    MavenExecutionRequest setLoggingLevel(int loggingLevel);
    int getLoggingLevel();
    
    MavenExecutionRequest setShowErrors(boolean showErrors);
    boolean isShowErrors();
    
    // Properties Configuration
    MavenExecutionRequest setUserProperties(Properties userProperties);
    Properties getUserProperties();
    
    MavenExecutionRequest setSystemProperties(Properties systemProperties);
    Properties getSystemProperties();
    
    // Profile Configuration
    MavenExecutionRequest setActiveProfiles(List<String> activeProfiles);
    List<String> getActiveProfiles();
    
    MavenExecutionRequest setInactiveProfiles(List<String> inactiveProfiles);
    List<String> getInactiveProfiles();
    
    // Execution Listeners
    MavenExecutionRequest setExecutionListener(ExecutionListener executionListener);
    ExecutionListener getExecutionListener();
    
    // Toolchains
    MavenExecutionRequest setToolchains(Map<String, List<ToolchainModel>> toolchains);
    Map<String, List<ToolchainModel>> getToolchains();
}

Configuration Example:

MavenExecutionRequest request = new DefaultMavenExecutionRequest();

// Basic build configuration
request.setPom(new File("pom.xml"));
request.setBaseDirectory(new File("."));
request.setGoals(Arrays.asList("clean", "install"));

// Repository configuration
request.setLocalRepository(localRepo);
request.setRemoteRepositories(remoteRepos);

// Reactor behavior for multi-module projects
request.setReactorFailureBehavior(MavenExecutionRequest.REACTOR_FAIL_FAST);

// Enable parallel builds
request.setDegreeOfConcurrency(4);

// Profile activation
request.setActiveProfiles(Arrays.asList("development", "integration-tests"));

// Properties
Properties userProps = new Properties();
userProps.setProperty("maven.test.skip", "false");
userProps.setProperty("project.version", "1.0.0-SNAPSHOT");
request.setUserProperties(userProps);

Execution Results

MavenExecutionResult Interface

public interface MavenExecutionResult {
    /**
     * Get the root project from the execution.
     */
    MavenProject getProject();
    
    /**
     * Get all projects in topologically sorted order.
     */
    List<MavenProject> getTopologicallySortedProjects();
    
    /**
     * Get dependency resolution results.
     */
    DependencyResolutionResult getDependencyResolutionResult();
    
    /**
     * Get all exceptions that occurred during execution.
     */
    List<Throwable> getExceptions();
    
    /**
     * Get build summary for a specific project.
     */
    Map<MavenProject, BuildSummary> getBuildSummary(MavenProject project);
    
    /**
     * Check if any exceptions occurred.
     */
    boolean hasExceptions();
    
    /**
     * Add an exception to the result.
     */
    MavenExecutionResult addException(Throwable exception);
}

Result Processing Example:

MavenExecutionResult result = maven.execute(request);

// Check for build failures
if (result.hasExceptions()) {
    System.err.println("Build failed with " + result.getExceptions().size() + " errors:");
    for (Throwable exception : result.getExceptions()) {
        exception.printStackTrace();
    }
} else {
    System.out.println("Build successful!");
    
    // Process built projects
    for (MavenProject project : result.getTopologicallySortedProjects()) {
        System.out.println("Built: " + project.getGroupId() + ":" + 
                         project.getArtifactId() + ":" + project.getVersion());
    }
}

// Access dependency resolution results
DependencyResolutionResult depResult = result.getDependencyResolutionResult();
if (depResult != null) {
    for (Dependency dep : depResult.getDependencies()) {
        System.out.println("Resolved: " + dep.getArtifact());
    }
}

Session Management

MavenSession Class

Represents the state and context of a Maven execution session.

public class MavenSession {
    /**
     * Get the current project being processed.
     */
    public MavenProject getCurrentProject();
    
    /**
     * Set the current project.
     */
    public void setCurrentProject(MavenProject currentProject);
    
    /**
     * Get all projects in the reactor.
     */
    public List<MavenProject> getProjects();
    
    /**
     * Set all projects in the reactor.
     */
    public void setProjects(List<MavenProject> projects);
    
    /**
     * Get the execution request that started this session.
     */
    public MavenExecutionRequest getRequest();
    
    /**
     * Get the repository system session for artifact operations.
     */
    public RepositorySystemSession getRepositorySession();
    
    /**
     * Set the repository system session.
     */
    public void setRepositorySession(RepositorySystemSession repositorySession);
    
    /**
     * Get user-defined properties.
     */
    public Map<String, Object> getUserProperties();
    
    /**
     * Get system properties.
     */
    public Map<String, Object> getSystemProperties();
    
    /**
     * Get session-scoped values.
     */
    public Map<String, Object> getPluginContext(PluginDescriptor plugin, MavenProject project);
    
    /**
     * Check if parallel execution is enabled.
     */
    public boolean isParallel();
    
    /**
     * Get project dependency graph.
     */
    public ProjectDependencyGraph getProjectDependencyGraph();
    
    /**
     * Set project dependency graph.
     */
    public void setProjectDependencyGraph(ProjectDependencyGraph projectDependencyGraph);
    
    /**
     * Get plugin context for session-scoped data.
     */
    public Map<String, Object> getPluginContext(PluginDescriptor pluginDescriptor, MavenProject project);
}

Session Usage Example:

@Component
private Maven maven;

public void processSession() throws Exception {
    MavenExecutionRequest request = new DefaultMavenExecutionRequest();
    // ... configure request
    
    MavenExecutionResult result = maven.execute(request);
    
    // The session is available through Maven's execution context
    // In a plugin or component, you can access it via:
    
    import org.apache.maven.plugins.annotations.Parameter;
    
    @Parameter(defaultValue = "${session}", readonly = true, required = true)
    private MavenSession session;
    
    // Access session information
    MavenProject currentProject = session.getCurrentProject();
    List<MavenProject> allProjects = session.getProjects();
    
    // Check execution mode
    if (session.isParallel()) {
        System.out.println("Running in parallel mode");
    }
    
    // Access properties
    String skipTests = (String) session.getUserProperties().get("maven.test.skip");
    
    // Work with project dependencies
    ProjectDependencyGraph graph = session.getProjectDependencyGraph();
    List<MavenProject> upstreamProjects = graph.getUpstreamProjects(currentProject, false);
}

Event System Integration

ExecutionListener Interface

public interface ExecutionListener {
    void projectDiscoveryStarted(ExecutionEvent event);
    void sessionStarted(ExecutionEvent event);
    void sessionEnded(ExecutionEvent event);
    void projectStarted(ExecutionEvent event);
    void projectSucceeded(ExecutionEvent event);
    void projectFailed(ExecutionEvent event);
    void mojoStarted(ExecutionEvent event);
    void mojoSucceeded(ExecutionEvent event);
    void mojoFailed(ExecutionEvent event);
    void forkStarted(ExecutionEvent event);
    void forkSucceeded(ExecutionEvent event);
    void forkFailed(ExecutionEvent event);
}

ExecutionEvent Class

public class ExecutionEvent {
    public static final Type PROJECT_DISCOVERY_STARTED = new Type("project-discovery-started");
    public static final Type SESSION_STARTED = new Type("session-started");
    public static final Type SESSION_ENDED = new Type("session-ended");
    public static final Type PROJECT_STARTED = new Type("project-started");
    public static final Type PROJECT_SUCCEEDED = new Type("project-succeeded");
    public static final Type PROJECT_FAILED = new Type("project-failed");
    public static final Type MOJO_STARTED = new Type("mojo-started");
    public static final Type MOJO_SUCCEEDED = new Type("mojo-succeeded");
    public static final Type MOJO_FAILED = new Type("mojo-failed");
    
    public Type getType();
    public MavenSession getSession();
    public MavenProject getProject();
    public MojoExecution getMojoExecution();
    public Throwable getException();
}

Event Listener Example:

public class BuildProgressListener implements ExecutionListener {
    
    @Override
    public void sessionStarted(ExecutionEvent event) {
        System.out.println("Build session started for " + 
            event.getSession().getProjects().size() + " projects");
    }
    
    @Override
    public void projectStarted(ExecutionEvent event) {
        MavenProject project = event.getProject();
        System.out.println("Building project: " + project.getName());
    }
    
    @Override
    public void mojoStarted(ExecutionEvent event) {
        MojoExecution mojo = event.getMojoExecution();
        System.out.println("Executing: " + mojo.getPlugin().getArtifactId() + 
                          ":" + mojo.getGoal());
    }
    
    @Override
    public void projectSucceeded(ExecutionEvent event) {
        MavenProject project = event.getProject();
        System.out.println("Successfully built: " + project.getName());
    }
    
    @Override
    public void projectFailed(ExecutionEvent event) {
        MavenProject project = event.getProject();
        Throwable exception = event.getException();
        System.err.println("Failed to build: " + project.getName() + 
                          " - " + exception.getMessage());
    }
}

// Configure listener in execution request
MavenExecutionRequest request = new DefaultMavenExecutionRequest();
request.setExecutionListener(new BuildProgressListener());

Types

public static class Type {
    private final String id;
    
    public Type(String id) {
        this.id = id;
    }
    
    public String toString() {
        return id;
    }
}

public interface ProjectDependencyGraph {
    List<MavenProject> getSortedProjects();
    List<MavenProject> getDownstreamProjects(MavenProject project, boolean transitive);
    List<MavenProject> getUpstreamProjects(MavenProject project, boolean transitive);
}

public interface DependencyResolutionResult {
    List<Dependency> getDependencies();
    List<Dependency> getUnresolvedDependencies();
    List<Exception> getCollectionErrors();
    DependencyNode getDependencyGraph();
}

public interface BuildSummary {
    MavenProject getProject();
    long getTime();
    Exception getException();
}

Install with Tessl CLI

npx tessl i tessl/maven-org-apache-maven--maven-core

docs

core-execution.md

exception-handling.md

index.md

lifecycle-management.md

plugin-system.md

project-management.md

repository-system.md

tile.json