Skip to content

Allow Listener implementations to be auto-discovered as SPI services #13805

Open

Description

@gnodet

Problem

Currently, Listener (and its subtypes ExecutionListener, RepositoryListener) must be registered programmatically via Session.registerListener(). This requires the caller to already have a live Session instance, which means core extensions loaded at startup via META-INF/services/ have no way to register themselves as listeners — they have no Session yet when they are instantiated.

Proposal

Auto-discover Listener implementations that also implement SpiService: during session initialization, Maven would iterate the already-discovered SpiService instances, filter those that are instanceof Listener, and call session.registerListener() on each automatically.

This requires no interface inheritance between Listener and SpiService (which would create a circular module dependency since maven-api-spi depends on maven-api-core). The two interfaces remain independent; the convention is purely in Maven's bootstrap wiring.

What this enables

A core extension in maven-api-spi could implement both SpiService and ExecutionListener (or RepositoryListener) and receive typed event callbacks without any explicit registration, simply by being declared in META-INF/services/.

What this does not change

  • Listener stays in maven-api-core, SpiService stays in maven-api-spi — no module restructuring needed.
  • Programmatic session.registerListener() continues to work as before.
  • No new interface hierarchy is introduced.

Additional work: document exception-handling contract on Listener

The current Listener/ExecutionListener/RepositoryListener Javadoc says nothing about what happens when onEvent() throws. In practice, both dispatchers (EventSpyImpl for execution events, MavenRepositoryListener for repository events) catch RuntimeException and log a WARN, swallowing the exception so that one misbehaving listener cannot affect others or the build. This contract should be made explicit in the Listener interface Javadoc so extension authors know they cannot signal build failures through a thrown exception — for that, afterProjectsRead() on a lifecycle participant SPI (see #13647) is the appropriate mechanism.

Context

This came up during review of #13647 (Maven 4 lifecycle participation SPI). The LifecycleParticipant SPI introduced there extends SpiService directly and uses its own dispatcher, which duplicates what the Listener dispatch infrastructure already provides for execution-phase callbacks. Auto-discovering Listener+SpiService implementations would allow that SPI to compose cleanly with the existing event system.

Activity

  1. added
    enhancement
    on Nov 3, 2026
  2. added a commit that references this issue on Nov 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions