gnodet-bot commented on code in PR #13647: URL: https://github.com/apache/maven/pull/13647#discussion_r4527868315
########## api/maven-api-spi/src/main/java/org/apache/maven/api/spi/LifecycleParticipant.java: ########## @@ -0,0 +1,51 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.maven.api.spi; + +import org.apache.maven.api.Session; +import org.apache.maven.api.annotations.Consumer; +import org.apache.maven.api.annotations.Experimental; +import org.apache.maven.api.annotations.Nonnull; +import org.apache.maven.api.annotations.ThreadSafe; + +/** + * Allows build extensions to participate in the Maven build lifecycle after all reactor + * projects have been read. This is the v4 API equivalent of + * {@code AbstractMavenLifecycleParticipant.afterProjectsRead()}. + * <p> + * Extensions may update project state through services such as + * {@link org.apache.maven.api.services.ProjectManager}. + * + * @since 4.2.0 + */ +@Experimental +@Consumer +@ThreadSafe +public interface LifecycleParticipant extends SpiService { + + /** + * Invoked after all reactor projects have been read and before the reactor is sorted for execution. + * Extensions may update project state through services such as + * {@link org.apache.maven.api.services.ProjectManager}. + * + * @param session the build session + * @throws LifecycleParticipantException if the build cannot continue + */ + default void afterProjectsRead(@Nonnull Session session) throws LifecycleParticipantException {} Review Comment: 💡 The default implementation is a no-op, but that isn't stated in the Javadoc. Add a note (e.g. `The default implementation does nothing.`) so implementors don't have to read the source to know there's nothing to delegate through. ########## impl/maven-core/src/main/java/org/apache/maven/internal/impl/LifecycleParticipantDispatcher.java: ########## @@ -0,0 +1,90 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.maven.internal.impl; + +import java.util.Collection; +import java.util.List; + +import org.apache.maven.MavenExecutionException; +import org.apache.maven.api.SessionData; +import org.apache.maven.api.spi.LifecycleParticipant; +import org.apache.maven.api.spi.LifecycleParticipantException; +import org.apache.maven.execution.MavenSession; + +/** + * Bridges lifecycle callbacks from the legacy execution engine to the Maven 4 SPI. + */ +public final class LifecycleParticipantDispatcher { + + @SuppressWarnings({"rawtypes", "unchecked"}) + private static final SessionData.Key<List<LifecycleParticipant>> PARTICIPANTS = + (SessionData.Key) SessionData.key(List.class, LifecycleParticipant.class); + + private LifecycleParticipantDispatcher() {} + + public static void setParticipants(MavenSession session, Collection<LifecycleParticipant> participants) { + session.getSession().getData().set(PARTICIPANTS, List.copyOf(participants)); + } + + public static void afterProjectsRead(MavenSession session) throws MavenExecutionException { + try { + call(session, participant -> participant.afterProjectsRead(session.getSession())); + } catch (LifecycleParticipantException e) { + throw new MavenExecutionException("Lifecycle participant failed after projects were read", e); + } + } + + private static void call(MavenSession session, ParticipantCall call) throws LifecycleParticipantException { + List<LifecycleParticipant> participants = participants(session); + if (participants == null) { + return; + } + Thread thread = Thread.currentThread(); + ClassLoader originalClassLoader = thread.getContextClassLoader(); + LifecycleParticipantException failure = null; + try { + for (LifecycleParticipant participant : participants) { Review Comment: ⚠️ **Fault isolation**: only `LifecycleParticipantException` is caught here. A `RuntimeException` thrown by a buggy extension will escape this catch, exit the loop early, and skip all remaining participants — breaking the "visit all, collect failures" contract the outer loop is trying to implement. Consider also catching `RuntimeException` and wrapping it: ```java } catch (RuntimeException e) { LifecycleParticipantException wrapped = new LifecycleParticipantException("Unexpected exception from lifecycle participant", e); if (failure == null) { failure = wrapped; } else { failure.addSuppressed(wrapped); } } ``` -- 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]
