{"record":{"id":"7b59eaa1e23338d8","repo":"apache/maven","slug":"the-plugincontext-is-only-available-during-a-mojo","errorCode":null,"errorMessage":"The PluginContext is only available during a mojo execution","messagePattern":"The PluginContext is only available during a mojo execution","errorType":"exception","errorClass":"MavenException","httpStatus":null,"severity":"error","filePath":"impl/maven-core/src/main/java/org/apache/maven/internal/impl/DefaultSession.java","lineNumber":193,"sourceCode":"    }\n\n    @Nonnull\n    @Override\n    public List<Project> getProjects() {\n        return getProjects(getMavenSession().getProjects());\n    }\n\n    @Nonnull\n    @Override\n    public Map<String, Object> getPluginContext(Project project) {\n        requireNonNull(project, \"project\" + \" cannot be null\");\n        try {\n            MojoExecution mojoExecution = lookup.lookup(MojoExecution.class);\n            MojoDescriptor mojoDescriptor = mojoExecution.getMojoDescriptor();\n            PluginDescriptor pluginDescriptor = mojoDescriptor.getPluginDescriptor();\n            return getMavenSession().getPluginContext(pluginDescriptor, ((DefaultProject) project).getProject());\n        } catch (LookupException e) {\n            throw new MavenException(\"The PluginContext is only available during a mojo execution\", e);\n        }\n    }\n\n    @Override\n    protected Session newSession(RepositorySystemSession repoSession, List<RemoteRepository> repositories) {\n        MavenSession t = getMavenSession();\n        final MavenSession ms = requireNonNull(t);\n        final MavenSession mss;\n        if (repoSession != ms.getRepositorySession()) {\n            mss = new MavenSession(repoSession, ms.getRequest(), ms.getResult());\n        } else {\n            mss = ms;\n        }\n        return newSession(mss, repositories);\n    }\n\n    protected Session newSession(MavenSession mavenSession, List<RemoteRepository> repositories) {\n        return new DefaultSession(","sourceCodeStart":175,"sourceCodeEnd":211,"githubUrl":"https://github.com/apache/maven/blob/e4093d4e120eac99d6bdce5ba67cace2f3085c97/impl/maven-core/src/main/java/org/apache/maven/internal/impl/DefaultSession.java#L175-L211","documentation":"MavenException thrown by Session.getPluginContext(project) when the container lookup of MojoExecution fails — i.e. the call happens outside a mojo execution. The plugin context is a per-plugin/per-project map that only exists while a Maven-configured mojo runs; DefaultSession derives it by looking up MojoExecution in the current Sisu scope and reading its PluginDescriptor.","triggerScenarios":"Calling session.getPluginContext(...) outside Mojo#execute(): during project building, lifecycle planning, import-scope dependency resolution, or direct embedder/API use where no mojo execution scope is active (LookupException is wrapped into this MavenException).","commonSituations":"Mojos handing Session to background threads/work that outlives execute(); API consumers assuming Session always carries plugin state; utility code shared between mojo and non-mojo contexts calling getPluginContext unconditionally.","solutions":["Call getPluginContext only inside Mojo.execute(); capture the map there and pass it on explicitly","For state that must outlive the execution, use your own store keyed by project instead of plugin context","Guard call sites in shared utilities with a try/catch MavenException or a flag indicating mojo context"],"exampleFix":"// before: called outside a mojo execution\nMap<String, Object> ctx = session.getPluginContext(project); // MavenException\n\n// after: capture during execution, pass explicitly\npublic void execute() {\n    Map<String, Object> ctx = getPluginContext(); // inside Mojo\n    worker.accept(ctx);\n}","handlingStrategy":"try-catch","validationCode":"// Detect mojo-execution context before calling\nboolean inMojoExecution;\ntry {\n    lookup.lookup(MojoExecution.class);\n    inMojoExecution = true;\n} catch (LookupException e) {\n    inMojoExecution = false;\n}","typeGuard":null,"tryCatchPattern":"try {\n    Map<String, Object> ctx = session.getPluginContext(project);\n} catch (MavenException e) {\n    if (\"The PluginContext is only available during a mojo execution\".equals(e.getMessage())) {\n        ctx = Map.of(); // degrade gracefully outside executions\n    } else throw e;\n}","preventionTips":["Capture the plugin context inside execute() and pass the map onward","Never call getPluginContext from constructors, builders, or background threads","Design mojos so state does not need plugin context after execution ends"],"tags":["maven","plugin-context","maven-api","mojo-execution","scope"],"backgroundTag":"execution-context-unavailable","analyzedSha":"e4093d4e120eac99d6bdce5ba67cace2f3085c97","analyzedAt":"2026-08-21T22:58:24.034Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}