Hiteshsai007 commented on code in PR #13612: URL: https://github.com/apache/maven/pull/13612#discussion_r4310535410
########## api/maven-api-core/src/main/java/org/apache/maven/api/services/LifecycleExecutor.java: ########## @@ -0,0 +1,59 @@ +/* + * 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.services; + +import java.util.List; +import java.util.function.Predicate; + +import org.apache.maven.api.MojoExecution; +import org.apache.maven.api.Project; +import org.apache.maven.api.Service; +import org.apache.maven.api.Session; +import org.apache.maven.api.annotations.Experimental; +import org.apache.maven.api.annotations.Nonnull; + +/** + * Calculates and executes Maven lifecycle plans. + * + * @since 4.1.0 + */ +@Experimental +public interface LifecycleExecutor extends Service { Review Comment: Renamed the public API to `LifecyclePlanExecutor` / `LifecyclePlanExecutionException` and the implementation to `DefaultLifecyclePlanExecutor`, avoiding the collision with the legacy lifecycle types. Addressed in 7fdf71d8f6. ########## impl/maven-core/src/main/java/org/apache/maven/internal/impl/DefaultLifecycleExecutor.java: ########## @@ -0,0 +1,106 @@ +/* + * 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 javax.inject.Inject; +import javax.inject.Named; +import javax.inject.Singleton; + +import java.util.ArrayList; +import java.util.List; +import java.util.function.Predicate; + +import org.apache.maven.api.MojoExecution; +import org.apache.maven.api.Project; +import org.apache.maven.api.Service; +import org.apache.maven.api.Session; +import org.apache.maven.api.annotations.Nonnull; +import org.apache.maven.api.services.LifecycleExecutionException; +import org.apache.maven.api.services.LifecycleExecutor; +import org.apache.maven.execution.MavenSession; +import org.apache.maven.lifecycle.MavenExecutionPlan; +import org.apache.maven.lifecycle.internal.LifecycleExecutionPlanCalculator; +import org.apache.maven.lifecycle.internal.LifecycleTaskSegmentCalculator; +import org.apache.maven.lifecycle.internal.MojoExecutor; +import org.apache.maven.lifecycle.internal.TaskSegment; +import org.apache.maven.project.MavenProject; +import org.eclipse.sisu.Typed; + +import static java.util.Objects.requireNonNull; +import static org.apache.maven.internal.impl.CoreUtils.cast; + +@Named +@Typed({LifecycleExecutor.class, Service.class}) +@Singleton +public class DefaultLifecycleExecutor implements LifecycleExecutor { + + private final LifecycleTaskSegmentCalculator taskSegmentCalculator; + private final LifecycleExecutionPlanCalculator executionPlanCalculator; + private final MojoExecutor mojoExecutor; + + @Inject + public DefaultLifecycleExecutor( + LifecycleTaskSegmentCalculator taskSegmentCalculator, + LifecycleExecutionPlanCalculator executionPlanCalculator, + MojoExecutor mojoExecutor) { + this.taskSegmentCalculator = taskSegmentCalculator; + this.executionPlanCalculator = executionPlanCalculator; + this.mojoExecutor = mojoExecutor; + } + + @Override + public void execute( + @Nonnull Session requestedSession, + @Nonnull Project project, + @Nonnull List<String> tasks, + @Nonnull Predicate<MojoExecution> filter) { + requireNonNull(requestedSession, "session"); + requireNonNull(project, "project"); + requireNonNull(tasks, "tasks").forEach(task -> requireNonNull(task, "task")); + requireNonNull(filter, "filter"); + + InternalMavenSession internalSession = InternalMavenSession.from(requestedSession); + MavenSession mavenSession = internalSession.getMavenSession(); + MavenProject mavenProject = + cast(DefaultProject.class, project, "project").getProject(); + MavenProject previousProject = mavenSession.getCurrentProject(); + + try { + mavenSession.setCurrentProject(mavenProject); + List<TaskSegment> taskSegments = taskSegmentCalculator.calculateTaskSegments(mavenSession, tasks); + List<org.apache.maven.plugin.MojoExecution> filteredPlan = new ArrayList<>(); + + for (TaskSegment taskSegment : taskSegments) { + MavenExecutionPlan executionPlan = executionPlanCalculator.calculateExecutionPlan( + mavenSession, mavenProject, taskSegment.getTasks()); + for (org.apache.maven.plugin.MojoExecution execution : executionPlan.getMojoExecutions()) { + if (filter.test(new DefaultMojoExecution(internalSession, execution))) { + filteredPlan.add(execution); + } + } + } + + mojoExecutor.execute(mavenSession, filteredPlan); + } catch (Exception e) { Review Comment: Separated the three stages: plan calculation failures are wrapped as `LifecyclePlanExecutionException`, predicate evaluation runs outside those catch blocks so its runtime exceptions propagate unchanged, and only Maven lifecycle execution failures are wrapped during execution. I also added `doesNotWrapFilterFailures()` to lock in that behavior. Addressed in 7fdf71d8f6. ########## impl/maven-core/src/test/java/org/apache/maven/internal/impl/DefaultLifecyclePlanExecutorTest.java: ########## @@ -0,0 +1,101 @@ +/* + * 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.List; + +import org.apache.maven.execution.MavenSession; +import org.apache.maven.lifecycle.MavenExecutionPlan; +import org.apache.maven.lifecycle.internal.LifecycleExecutionPlanCalculator; +import org.apache.maven.lifecycle.internal.LifecycleTask; +import org.apache.maven.lifecycle.internal.LifecycleTaskSegmentCalculator; +import org.apache.maven.lifecycle.internal.MojoExecutor; +import org.apache.maven.lifecycle.internal.TaskSegment; +import org.apache.maven.plugin.MojoExecution; +import org.apache.maven.plugin.descriptor.MojoDescriptor; +import org.apache.maven.plugin.descriptor.PluginDescriptor; +import org.apache.maven.project.MavenProject; +import org.junit.jupiter.api.Test; +import org.mockito.ArgumentCaptor; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.Mockito.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.same; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +class DefaultLifecycleExecutorTest { + + @Test + void executesFilteredMojosAsOnePlanAndRestoresCurrentProject() throws Exception { Review Comment: Added `restoresCurrentProjectOnExecutionFailure()`, which makes `MojoExecutor.execute()` fail and verifies that the prior current project is restored. The focused suite now has 3 passing tests. Addressed in 7fdf71d8f6. -- 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]
