gnodet-bot commented on code in PR #13612:
URL: https://github.com/apache/maven/pull/13612#discussion_r4525375576
##########
impl/maven-core/src/main/java/org/apache/maven/lifecycle/internal/concurrent/BuildPlanExecutor.java:
##########
@@ -232,6 +263,32 @@ class BuildContext implements AutoCloseable {
this.plan = null;
}
+ BuildContext(MavenSession session) {
+ this.session = session;
+ this.reactorContext = null;
+ this.threads = 1;
+ this.executor = null;
Review Comment:
⚠️ **Latent NPE in `close()`** — plan-only `BuildContext` has `executor =
null`
This new constructor sets `executor = null`. The existing `close()` method
(line 424) calls `this.executor.close()` unconditionally — no null guard.
`BuildContext` implements `AutoCloseable`, which invites callers to use
try-with-resources. Any embedder who writes `try (var ctx =
executor.createProjectContext(...)) { ... }` will get a NullPointerException on
close.
Add a null guard in `close()`:
```java
@Override
public void close() {
if (executor != null) executor.close();
}
```
##########
impl/maven-core/src/main/java/org/apache/maven/internal/impl/DefaultLifecycleExecutor.java:
##########
@@ -0,0 +1,203 @@
+/*
+ * 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.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.Set;
+import java.util.function.Predicate;
+
+import org.apache.maven.api.LifecyclePlan;
+import org.apache.maven.api.MojoExecution;
+import org.apache.maven.api.Service;
+import org.apache.maven.api.annotations.Nonnull;
+import org.apache.maven.api.services.LifecycleExecutionRequest;
+import org.apache.maven.api.services.LifecycleExecutionResult;
+import org.apache.maven.api.services.LifecycleExecutor;
+import org.apache.maven.api.services.LifecycleExecutorException;
+import org.apache.maven.api.services.LifecyclePlanRequest;
+import org.apache.maven.api.services.LifecyclePlanResult;
+import org.apache.maven.execution.MavenSession;
+import org.apache.maven.lifecycle.LifecycleExecutionException;
+import org.apache.maven.lifecycle.internal.LifecycleTaskSegmentCalculator;
+import org.apache.maven.lifecycle.internal.TaskSegment;
+import org.apache.maven.lifecycle.internal.concurrent.BuildPlanExecutor;
+import org.apache.maven.lifecycle.internal.concurrent.BuildStep;
+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 BuildPlanExecutor buildPlanExecutor;
+
+ @Inject
+ public DefaultLifecycleExecutor(
+ LifecycleTaskSegmentCalculator taskSegmentCalculator,
BuildPlanExecutor buildPlanExecutor) {
+ this.taskSegmentCalculator = taskSegmentCalculator;
+ this.buildPlanExecutor = buildPlanExecutor;
+ }
+
+ @Nonnull
+ @Override
+ public LifecyclePlanResult plan(@Nonnull LifecyclePlanRequest request) {
+ requireNonNull(request, "request");
+ InternalMavenSession internalSession =
InternalMavenSession.from(request.getSession());
+ MavenSession mavenSession = internalSession.getMavenSession();
+ MavenProject mavenProject =
+ cast(DefaultProject.class, request.project(),
"project").getProject();
+ MavenProject previousProject = mavenSession.getCurrentProject();
+
+ mavenSession.setCurrentProject(mavenProject);
+ try {
+ List<TaskSegment> taskSegments =
taskSegmentCalculator.calculateTaskSegments(mavenSession, request.tasks());
+ BuildPlanExecutor.BuildContext buildContext =
+ buildPlanExecutor.createProjectContext(mavenSession,
mavenProject, taskSegments);
+
+ List<org.apache.maven.plugin.MojoExecution> internalExecutions =
+ buildContext.getPlan().sortedNodes().stream()
+ .flatMap(BuildStep::executions)
+ .toList();
+ List<MojoExecution> executions = internalExecutions.stream()
+ .map(execution -> (MojoExecution) new
DefaultMojoExecution(internalSession, execution))
+ .toList();
+ Map<org.apache.maven.plugin.MojoExecution, MojoExecution> mapping =
+ DefaultLifecyclePlan.buildMapping(internalExecutions,
executions);
+
+ DefaultLifecyclePlan plan =
+ new DefaultLifecyclePlan(request.project(),
request.tasks(), executions, buildContext, mapping);
+ return new DefaultLifecyclePlanResult(request, plan);
+ } catch (Exception e) {
+ throw new LifecycleExecutorException("Unable to calculate
lifecycle plan", e);
+ } finally {
+ mavenSession.setCurrentProject(previousProject);
+ }
+ }
+
+ @Nonnull
+ @Override
+ public LifecycleExecutionResult execute(@Nonnull LifecycleExecutionRequest
request) {
+ requireNonNull(request, "request");
+ InternalMavenSession.from(request.getSession());
+ MavenSession mavenSession =
+
InternalMavenSession.from(request.getSession()).getMavenSession();
Review Comment:
⚠️ **Dead call** — refactoring artifact
The first `InternalMavenSession.from(request.getSession())` discards its
return value. This is a leftover from the previous version where the result was
stored as `internalSession`. The second call on the next line is the one
actually used. The `internalSession` variable is not needed in `execute()`
(unlike `plan()`), so just remove the dead call.
```suggestion
MavenSession mavenSession =
InternalMavenSession.from(request.getSession()).getMavenSession();
```
--
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]