This is an automated email from the ASF dual-hosted git repository.

gnodet pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven.git


The following commit(s) were added to refs/heads/master by this push:
     new 2532ee8f37 fix: guard SPI listener discovery against LookupException 
and add EventSpy-level tests (#13941)
2532ee8f37 is described below

commit 2532ee8f37c26ddbf5707d61b6b82e6263c61435
Author: CHARAN GOWDA M D <[email protected]>
AuthorDate: Tue Nov 10 22:56:29 2026 +0530

    fix: guard SPI listener discovery against LookupException and add 
EventSpy-level tests (#13941)
    
    * [#13805] Auto-discover Listener implementations that implement SpiService
    
    * address review: log LookupException at WARN, remove impl classes from API 
Javadoc, and add test
    
    ---------
    
    Co-authored-by: Charan Gowda <[email protected]>
---
 .../org/apache/maven/api/ExecutionListener.java    |  5 +-
 .../main/java/org/apache/maven/api/Listener.java   |  7 ++
 .../org/apache/maven/api/RepositoryListener.java   |  6 +-
 .../main/java/org/apache/maven/api/Session.java    |  2 +
 .../maven/internal/impl/EventSpyImplTest.java      | 86 ++++++++++++++++++++++
 .../org/apache/maven/impl/AbstractSession.java     | 14 +++-
 6 files changed, 114 insertions(+), 6 deletions(-)

diff --git 
a/api/maven-api-core/src/main/java/org/apache/maven/api/ExecutionListener.java 
b/api/maven-api-core/src/main/java/org/apache/maven/api/ExecutionListener.java
index 2d7d141e80..0e08b697dd 100644
--- 
a/api/maven-api-core/src/main/java/org/apache/maven/api/ExecutionListener.java
+++ 
b/api/maven-api-core/src/main/java/org/apache/maven/api/ExecutionListener.java
@@ -27,8 +27,11 @@
  * Each method corresponds to a specific {@link ExecutionEventType} and has a 
no-op default implementation,
  * so implementations only need to override the methods they care about.
  * <p>
- * Register an {@code ExecutionListener} via {@link 
Session#registerListener(Listener)}.
+ * Register an {@code ExecutionListener} via {@link 
Session#registerListener(Listener)} or by implementing
+ * {@code org.apache.maven.api.spi.SpiService} for auto-discovery.
  * Implementations must support concurrent notification during parallel builds.
+ * Runtime exceptions thrown by a listener are caught and logged at {@code 
WARN} level by the event dispatcher;
+ * they do not stop build execution or notification of other listeners, and 
cannot abort the build.
  *
  * @see ExecutionEvent
  * @see ExecutionEventType
diff --git 
a/api/maven-api-core/src/main/java/org/apache/maven/api/Listener.java 
b/api/maven-api-core/src/main/java/org/apache/maven/api/Listener.java
index 9f6a6f02d1..3aece6529d 100644
--- a/api/maven-api-core/src/main/java/org/apache/maven/api/Listener.java
+++ b/api/maven-api-core/src/main/java/org/apache/maven/api/Listener.java
@@ -48,6 +48,10 @@
  *       {@code META-INF/services/org.apache.maven.api.spi.SpiService}. Maven 
will
  *       discover and register such listeners automatically at session 
initialization.</li>
  * </ul>
+ * <p>
+ * Implementations should handle their own errors. Any {@link 
RuntimeException} thrown by {@link #onEvent(Event)}
+ * is caught and logged at {@code WARN} level by event dispatchers to protect 
build stability and ensure that other
+ * listeners are still notified. A listener cannot abort or fail the build by 
throwing an exception.
  *
  * @since 4.0.0
  */
@@ -57,6 +61,9 @@
 public interface Listener {
     /**
      * Receives a build execution event.
+     * <p>
+     * Any {@link RuntimeException} thrown by this method is caught and logged 
at {@code WARN} level
+     * by event dispatchers to protect build stability; it will not abort the 
build.
      *
      * @param event the execution event
      */
diff --git 
a/api/maven-api-core/src/main/java/org/apache/maven/api/RepositoryListener.java 
b/api/maven-api-core/src/main/java/org/apache/maven/api/RepositoryListener.java
index 845ef247ff..faa859ba1a 100644
--- 
a/api/maven-api-core/src/main/java/org/apache/maven/api/RepositoryListener.java
+++ 
b/api/maven-api-core/src/main/java/org/apache/maven/api/RepositoryListener.java
@@ -27,9 +27,11 @@
  * Each method corresponds to a specific {@link RepositoryEventType} and has a 
no-op default implementation,
  * so implementations only need to override the methods they care about.
  * <p>
- * Register a {@code RepositoryListener} via {@link 
Session#registerListener(Listener)}.
+ * Register a {@code RepositoryListener} via {@link 
Session#registerListener(Listener)} or by implementing
+ * {@code org.apache.maven.api.spi.SpiService} for auto-discovery.
  * Implementations must be thread-safe because callbacks may occur 
concurrently.
- * Runtime exceptions thrown by a listener do not stop repository processing 
or notification of other listeners.
+ * Runtime exceptions thrown by a listener are caught and logged at {@code 
WARN} level by the repository event dispatcher;
+ * they do not stop repository processing or notification of other listeners, 
and cannot abort the build.
  *
  * @see RepositoryEvent
  * @see RepositoryEventType
diff --git a/api/maven-api-core/src/main/java/org/apache/maven/api/Session.java 
b/api/maven-api-core/src/main/java/org/apache/maven/api/Session.java
index 87b1bfc5f2..a3fb39b2bd 100644
--- a/api/maven-api-core/src/main/java/org/apache/maven/api/Session.java
+++ b/api/maven-api-core/src/main/java/org/apache/maven/api/Session.java
@@ -210,6 +210,8 @@ default int getDegreeOfConcurrency() {
     /**
      * Registers a listener for execution events, repository events, or both.
      * Typed listeners receive only their event-specific callbacks. Legacy 
listeners receive execution events.
+     * Listeners implementing {@code org.apache.maven.api.spi.SpiService} are 
also auto-discovered during
+     * session initialization.
      * Registration is shared by Maven sessions using the same underlying 
repository system session.
      *
      * @param listener the listener to register
diff --git 
a/impl/maven-core/src/test/java/org/apache/maven/internal/impl/EventSpyImplTest.java
 
b/impl/maven-core/src/test/java/org/apache/maven/internal/impl/EventSpyImplTest.java
index 0ba976c851..c8d7109dfc 100644
--- 
a/impl/maven-core/src/test/java/org/apache/maven/internal/impl/EventSpyImplTest.java
+++ 
b/impl/maven-core/src/test/java/org/apache/maven/internal/impl/EventSpyImplTest.java
@@ -31,6 +31,9 @@
 import org.apache.maven.api.RepositoryEvent;
 import org.apache.maven.api.RepositoryListener;
 import org.apache.maven.api.Session;
+import org.apache.maven.api.services.Lookup;
+import org.apache.maven.api.services.LookupException;
+import org.apache.maven.api.spi.SpiService;
 import org.apache.maven.execution.DefaultMavenExecutionRequest;
 import org.apache.maven.execution.MavenSession;
 import org.apache.maven.impl.InternalSession;
@@ -191,6 +194,89 @@ void capturesExecutionDetailsWhenEventIsCreated() {
         assertSame(failure, event.getException().orElseThrow());
     }
 
+    @Test
+    void autoDiscoversListenerImplementingSpiService() throws Exception {
+        class SpiListener implements ExecutionListener, SpiService {
+            int startedCalls;
+
+            @Override
+            public void sessionStarted(ExecutionEvent event) {
+                startedCalls++;
+            }
+        }
+
+        SpiListener spiListener = new SpiListener();
+        SpiService nonListenerSpi = new SpiService() {};
+        Lookup lookup = mock(Lookup.class);
+        
when(lookup.lookupList(SpiService.class)).thenReturn(List.of(nonListenerSpi, 
spiListener));
+
+        DefaultRepositorySystemSession resolver = new 
DefaultRepositorySystemSession(h -> false);
+        MavenSession maven = new MavenSession(null, resolver, new 
DefaultMavenExecutionRequest(), null);
+        DefaultSession session = new DefaultSession(maven, 
mock(RepositorySystem.class), List.of(), null, lookup, null);
+        maven.setSession(session);
+        resolver.getData().set(InternalSession.class, session);
+
+        assertEquals(1, session.getListeners().size());
+        assertTrue(session.getListeners().contains(spiListener));
+
+        Session derived = session.withRemoteRepositories(List.of());
+        assertEquals(1, derived.getListeners().size());
+        assertTrue(derived.getListeners().contains(spiListener));
+
+        org.apache.maven.execution.ExecutionEvent event = 
mock(org.apache.maven.execution.ExecutionEvent.class);
+        when(event.getSession()).thenReturn(maven);
+        
when(event.getType()).thenReturn(org.apache.maven.execution.ExecutionEvent.Type.SessionStarted);
+        new EventSpyImpl().onEvent(event);
+
+        assertEquals(1, spiListener.startedCalls);
+    }
+
+    @Test
+    void autoDiscoversRepositoryListenerImplementingSpiService() {
+        class SpiRepoListener implements RepositoryListener, SpiService {
+            int resolvingCalls;
+
+            @Override
+            public void artifactResolving(RepositoryEvent event) {
+                resolvingCalls++;
+            }
+        }
+
+        SpiRepoListener spiListener = new SpiRepoListener();
+        Lookup lookup = mock(Lookup.class);
+        
when(lookup.lookupList(SpiService.class)).thenReturn(List.of(spiListener));
+
+        DefaultRepositorySystemSession resolver = new 
DefaultRepositorySystemSession(h -> false);
+        MavenSession maven = new MavenSession(null, resolver, new 
DefaultMavenExecutionRequest(), null);
+        DefaultSession session = new DefaultSession(maven, 
mock(RepositorySystem.class), List.of(), null, lookup, null);
+        maven.setSession(session);
+        resolver.getData().set(InternalSession.class, session);
+
+        assertEquals(1, session.getListeners().size());
+        assertTrue(session.getListeners().contains(spiListener));
+
+        new MavenRepositoryListener()
+                .artifactResolving(new 
org.eclipse.aether.RepositoryEvent.Builder(
+                                resolver, 
org.eclipse.aether.RepositoryEvent.EventType.ARTIFACT_RESOLVING)
+                        .build());
+
+        assertEquals(1, spiListener.resolvingCalls);
+    }
+
+    @Test
+    void handlesLookupExceptionDuringSpiDiscoveryGracefully() {
+        Lookup lookup = mock(Lookup.class);
+        when(lookup.lookupList(SpiService.class)).thenThrow(new 
LookupException("Simulated provisioning failure"));
+
+        DefaultRepositorySystemSession resolver = new 
DefaultRepositorySystemSession(h -> false);
+        MavenSession maven = new MavenSession(null, resolver, new 
DefaultMavenExecutionRequest(), null);
+        DefaultSession session = new DefaultSession(maven, 
mock(RepositorySystem.class), List.of(), null, lookup, null);
+        maven.setSession(session);
+        resolver.getData().set(InternalSession.class, session);
+
+        assertTrue(session.getListeners().isEmpty());
+    }
+
     private static class TestContext {
         final DefaultRepositorySystemSession resolver = new 
DefaultRepositorySystemSession(h -> false);
         final MavenSession maven = new MavenSession(null, resolver, new 
DefaultMavenExecutionRequest(), null);
diff --git 
a/impl/maven-impl/src/main/java/org/apache/maven/impl/AbstractSession.java 
b/impl/maven-impl/src/main/java/org/apache/maven/impl/AbstractSession.java
index f99a428c31..a35343176c 100644
--- a/impl/maven-impl/src/main/java/org/apache/maven/impl/AbstractSession.java
+++ b/impl/maven-impl/src/main/java/org/apache/maven/impl/AbstractSession.java
@@ -111,12 +111,16 @@
 import org.eclipse.aether.repository.ArtifactRepository;
 import org.eclipse.aether.resolution.ArtifactResult;
 import org.eclipse.aether.transfer.TransferResource;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 import static java.util.Objects.requireNonNull;
 import static org.apache.maven.impl.ImplUtils.map;
 
 public abstract class AbstractSession implements InternalSession {
 
+    private static final Logger LOGGER = 
LoggerFactory.getLogger(AbstractSession.class);
+
     protected final RepositorySystemSession session;
     protected final RepositorySystem repositorySystem;
     protected final List<RemoteRepository> repositories;
@@ -170,10 +174,14 @@ private static List<Listener> 
getListeners(RepositorySystemSession session, Look
         return (List<Listener>) 
session.getData().computeIfAbsent(Listener.class, () -> {
             List<Listener> list = new CopyOnWriteArrayList<>();
             if (lookup != null) {
-                for (SpiService spi : lookup.lookupList(SpiService.class)) {
-                    if (spi instanceof Listener listener && 
!list.contains(listener)) {
-                        list.add(listener);
+                try {
+                    for (SpiService spi : lookup.lookupList(SpiService.class)) 
{
+                        if (spi instanceof Listener listener && 
!list.contains(listener)) {
+                            list.add(listener);
+                        }
                     }
+                } catch (LookupException e) {
+                    LOGGER.warn("Failed to discover Listener implementations 
via SPI", e);
                 }
             }
             return list;

Reply via email to