gnodet opened a new issue, #13989: URL: https://github.com/apache/maven/issues/13989
## Problem Build extensions (`<build><extensions>` and plugins with `<extensions>true</extensions>`) cannot use pure maven-di. They go through the `SisuDiBridgeModule` which creates an ephemeral `BridgeInjectorImpl` — beans are bridged into Sisu/Guice but are NOT registered in the root maven-di injector. In contrast: - **Core extensions** (`.mvn/extensions.xml`) get full maven-di support: `PlexusContainerCapsuleFactory` calls `container.lookup(Injector.class).discover(extensionRealm)` on the root injector. Beans are visible to both maven-di and Sisu. - **V4 plugins** (mojos) get a clean standalone `Injector.create()` in `loadV4Mojo()`. Build extensions are the missing piece. This matters because: 1. **Maven 5 will drop Sisu** — every extension that works today through the bridge needs a pure maven-di path. 2. **Plugin-as-extension is essential** — plugins like `maven-war-plugin` provide packaging types via `<extensions>true</extensions>`. Users should not be forced to declare these in `.mvn/extensions.xml`. 3. **The `gh-13306` integration test** already demonstrates plugins providing `PackagingProvider` via `@org.apache.maven.api.di.Named` — but it relies on the Sisu bridge, not a native maven-di path. ## Proposed Design ### Detection - **Plugin-as-extension:** the `PluginDescriptor` already has `isV4Api()` (set when `plugin.xml` uses the `PLUGIN/2.0.0` namespace). When `<extensions>true</extensions>` is set and the plugin is V4, use the maven-di path. - **Pure extension:** extend `META-INF/maven/extension.xml` with a new namespace (e.g. `EXTENSION/2.0.0`) to signal V4. Today this descriptor only carries `exportedPackages`/`exportedArtifacts`. ### DI Discovery Replicate the core extension pattern: in `setupExtensionsRealm()`, for V4 extensions, call: ```java container.lookup(Injector.class).discover(extensionRealm); ``` This registers beans directly in the root maven-di injector. Per-project scoping is already handled by `ExtensibleEnumRegistries.isVisibleFrom()`, which checks whether the bean's classloader is reachable from the current TCCL (set to the project's realm). Since only the declaring project's realm imports the extension realm, scoping is automatic. For legacy extensions, continue using the `SisuDiBridgeModule` bridge (unchanged). ### Configuration Core extensions already handle this via `ExtensionConfigurationModule`. Build extensions have a `<configuration>` element (added in model 4.1.0). The same pattern should apply: bind the `XmlNode` configuration into the injector so V4 extension beans can access it. ### Code Changes The primary changes are in `DefaultMavenPluginManager`: 1. **`setupExtensionsRealm()`** — after creating the extension realm, detect V4 (via `PluginDescriptor.isV4Api()` for plugins, or new descriptor namespace for pure extensions). Branch to a new discovery path that uses the root injector. 2. **`discoverPluginComponents()`** — either split this method or add a V4 branch that skips the `SisuDiBridgeModule` and instead discovers directly into the root injector, similar to `PlexusContainerCapsuleFactory`'s core extension handling. 3. **`ExtensionDescriptor`** — add namespace versioning to signal V4 extensions (for pure extensions, not plugin-as-extension). ## Traps / Open Questions ### Double discovery for plugin-as-extension A V4 plugin with `<extensions>true</extensions>` has its SPI beans discovered into the root injector (for extension components like `PackagingProvider`). Later, when a mojo from that plugin executes, `loadV4Mojo()` calls `injector.discover(pluginRealm)` on a standalone injector — this re-scans the same realm. Need to ensure SPI beans aren't instantiated twice or that double discovery is harmless. ### Bean lifecycle and realm cleanup Beans discovered in the root injector hold references to the extension's classloader. In large reactors, this may prevent extension realm garbage collection. Need to verify this matches core extension behavior (which has the same characteristic). ### Discovery ordering If two build extensions in the same project both provide a `PackagingProvider`, the discovery order must be deterministic (POM declaration order). The root injector needs to preserve this ordering in `isVisibleFrom()`-filtered lookups. ### Built-in packaging migration The hardcoded `WarLifecycleMappingProvider`, `EarLifecycleMappingProvider`, etc. in `maven-core` use the legacy `LifecycleMapping` interface, not the `PackagingProvider` SPI. Migrating these to `PackagingProvider` is a separate task but validates the design and eats our own dog food. --- _Claude Code on behalf of Guillaume Nodet_ -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
