Skip to content

Commit 6eba735

Browse files
fix #10415: catch errors thrown by a concurrent build step
the concurrent executor only caught Exception around a build step , so an Error such as OutOfMemoryError or NoClassDefFoundError never reached the step . the step stayed in the scheduled state , nothing was added to the execution result , and the build ended with no failure recorded at all . the old LifecycleModuleBuilder catches Throwable , and handleBuildError here already has a branch for errors and other throwables , so only the plumbing that feeds it needed widening .
1 parent ea4a417 commit 6eba735

3 files changed

Lines changed: 199 additions & 6 deletions

File tree

‎impl/maven-core/src/main/java/org/apache/maven/lifecycle/internal/concurrent/BuildPlanExecutor.java‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -401,9 +401,9 @@ private void processStep(BuildStep step) {
401401
try {
402402
executeStep(step);
403403
executePlan();
404-
} catch (Exception e) {
404+
} catch (Throwable e) {
405405
step.status.compareAndSet(SKIPPED, FAILED);
406-
// Store the exception in the step for handling in the TEARDOWN phase
406+
// Store the failure in the step for handling in the TEARDOWN phase
407407
step.exception = e;
408408
logger.debug("Stored exception for step {} to be handled in TEARDOWN phase", step, e);
409409
// Let the scheduler handle after:* phases and TEARDOWN in the next cycle
@@ -438,10 +438,10 @@ private void processStep(BuildStep step) {
438438
}
439439
}
440440
executePlan();
441-
} catch (Exception e) {
441+
} catch (Throwable e) {
442442
step.status.compareAndSet(SCHEDULED, FAILED);
443443

444-
// Store the exception in the step for handling in the TEARDOWN phase
444+
// Store the failure in the step for handling in the TEARDOWN phase
445445
step.exception = e;
446446
logger.debug("Stored exception for step {} to be handled in TEARDOWN phase", step, e);
447447

@@ -525,7 +525,7 @@ private void executeStep(BuildStep step) throws IOException, LifecycleExecutionE
525525
List<Throwable> failures = null;
526526
boolean allWorkExecuted = true;
527527
for (BuildStep projectStep : plan.steps(step.project).toList()) {
528-
Exception exception = projectStep.exception;
528+
Throwable exception = projectStep.exception;
529529
if (exception != null) {
530530
if (failures == null) {
531531
failures = new ArrayList<>();

‎impl/maven-core/src/main/java/org/apache/maven/lifecycle/internal/concurrent/BuildStep.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public class BuildStep {
6161
final Collection<BuildStep> successors = new HashSet<>();
6262
final AtomicInteger status = new AtomicInteger();
6363
final AtomicBoolean skip = new AtomicBoolean();
64-
volatile Exception exception;
64+
volatile Throwable exception;
6565

6666
public BuildStep(String name, MavenProject project, Lifecycle.Phase phase) {
6767
this.name = Objects.requireNonNull(name, "name cannot be null");
Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.maven.lifecycle.internal.concurrent;
20+
21+
import java.util.Collections;
22+
import java.util.List;
23+
import java.util.Map;
24+
25+
import org.apache.maven.execution.DefaultMavenExecutionRequest;
26+
import org.apache.maven.execution.DefaultMavenExecutionResult;
27+
import org.apache.maven.execution.MavenExecutionRequest;
28+
import org.apache.maven.execution.MavenSession;
29+
import org.apache.maven.execution.ProjectDependencyGraph;
30+
import org.apache.maven.execution.ProjectExecutionEvent;
31+
import org.apache.maven.execution.ProjectExecutionListener;
32+
import org.apache.maven.internal.impl.DefaultLifecycleRegistry;
33+
import org.apache.maven.internal.transformation.TransformerManager;
34+
import org.apache.maven.lifecycle.internal.LifecycleTask;
35+
import org.apache.maven.lifecycle.internal.ReactorBuildStatus;
36+
import org.apache.maven.lifecycle.internal.ReactorContext;
37+
import org.apache.maven.lifecycle.internal.TaskSegment;
38+
import org.apache.maven.lifecycle.internal.stub.ExecutionEventCatapultStub;
39+
import org.apache.maven.project.MavenProject;
40+
import org.eclipse.aether.DefaultRepositorySystemSession;
41+
import org.eclipse.aether.RepositorySystemSession;
42+
import org.eclipse.aether.deployment.DeployRequest;
43+
import org.eclipse.aether.installation.InstallRequest;
44+
import org.junit.jupiter.api.Test;
45+
46+
import static org.junit.jupiter.api.Assertions.assertEquals;
47+
import static org.junit.jupiter.api.Assertions.assertSame;
48+
import static org.junit.jupiter.api.Assertions.assertTrue;
49+
50+
class BuildPlanExecutorTest {
51+
52+
/**
53+
* A build step that throws an {@link Error} must be reported as a build failure, the same way the
54+
* single threaded builder reports it. Otherwise the build ends with no exception at all and Maven
55+
* prints BUILD SUCCESS while nothing was built.
56+
*/
57+
@Test
58+
void errorThrownByBuildStepIsRecordedAsBuildFailure() throws Exception {
59+
Error thrown = new NoClassDefFoundError("some/Class");
60+
MavenProject project = newProject();
61+
MavenSession session = newSession(project);
62+
63+
execute(session, project, event -> {
64+
throw thrown;
65+
});
66+
67+
List<Throwable> exceptions = session.getResult().getExceptions();
68+
assertEquals(1, exceptions.size(), "expected the error to be recorded, but got: " + exceptions);
69+
assertSame(thrown, exceptions.get(0));
70+
assertTrue(session.getResult().getBuildSummary(project) instanceof org.apache.maven.execution.BuildFailure);
71+
}
72+
73+
/**
74+
* The same for an exception, which already worked. This pins the existing behaviour so the widened
75+
* catch does not change it.
76+
*/
77+
@Test
78+
void exceptionThrownByBuildStepIsRecordedAsBuildFailure() throws Exception {
79+
RuntimeException thrown = new IllegalStateException("nope");
80+
MavenProject project = newProject();
81+
MavenSession session = newSession(project);
82+
83+
execute(session, project, event -> {
84+
throw thrown;
85+
});
86+
87+
List<Throwable> exceptions = session.getResult().getExceptions();
88+
assertEquals(1, exceptions.size(), "expected the exception to be recorded, but got: " + exceptions);
89+
assertSame(thrown, exceptions.get(0));
90+
assertTrue(session.getResult().getBuildSummary(project) instanceof org.apache.maven.execution.BuildFailure);
91+
}
92+
93+
private void execute(MavenSession session, MavenProject project, BeforeProjectExecution listener) throws Exception {
94+
ReactorContext reactorContext = new ReactorContext(
95+
session.getResult(),
96+
Thread.currentThread().getContextClassLoader(),
97+
new ReactorBuildStatus(session.getProjectDependencyGraph()));
98+
TaskSegment taskSegment = new TaskSegment(false);
99+
taskSegment.getTasks().add(new LifecycleTask("validate"));
100+
newExecutor(listener).execute(session, reactorContext, List.of(taskSegment));
101+
}
102+
103+
private BuildPlanExecutor newExecutor(ProjectExecutionListener listener) {
104+
return new BuildPlanExecutor(
105+
null,
106+
new ExecutionEventCatapultStub(),
107+
List.of(listener),
108+
new NoopTransformerManager(),
109+
new BuildPlanLogger(),
110+
Map.of(),
111+
null,
112+
null,
113+
new DefaultLifecycleRegistry(Collections.emptyList()));
114+
}
115+
116+
private MavenProject newProject() {
117+
MavenProject result = new MavenProject();
118+
result.setArtifactId("a");
119+
result.setCollectedProjects(List.of());
120+
return result;
121+
}
122+
123+
private MavenSession newSession(MavenProject project) {
124+
MavenExecutionRequest request = new DefaultMavenExecutionRequest();
125+
request.setGoals(List.of("validate"));
126+
MavenSession result = new MavenSession(
127+
null, new DefaultRepositorySystemSession(h -> false), request, new DefaultMavenExecutionResult());
128+
result.setProjectDependencyGraph(new SingleProjectDependencyGraph(project));
129+
result.setProjects(List.of(project));
130+
return result;
131+
}
132+
133+
private static final class SingleProjectDependencyGraph implements ProjectDependencyGraph {
134+
135+
private final List<MavenProject> projects;
136+
137+
private SingleProjectDependencyGraph(MavenProject project) {
138+
this.projects = List.of(project);
139+
}
140+
141+
@Override
142+
public List<MavenProject> getAllProjects() {
143+
return projects;
144+
}
145+
146+
@Override
147+
public List<MavenProject> getSortedProjects() {
148+
return projects;
149+
}
150+
151+
@Override
152+
public List<MavenProject> getDownstreamProjects(MavenProject project, boolean transitive) {
153+
return List.of();
154+
}
155+
156+
@Override
157+
public List<MavenProject> getUpstreamProjects(MavenProject project, boolean transitive) {
158+
return List.of();
159+
}
160+
}
161+
162+
private static final class NoopTransformerManager implements TransformerManager {
163+
164+
@Override
165+
public InstallRequest remapInstallArtifacts(RepositorySystemSession session, InstallRequest request) {
166+
return request;
167+
}
168+
169+
@Override
170+
public DeployRequest remapDeployArtifacts(RepositorySystemSession session, DeployRequest request) {
171+
return request;
172+
}
173+
174+
@Override
175+
public void injectTransformedArtifacts(RepositorySystemSession repositorySession, MavenProject project) {}
176+
}
177+
178+
@FunctionalInterface
179+
private interface BeforeProjectExecution extends ProjectExecutionListener {
180+
181+
@Override
182+
void beforeProjectExecution(ProjectExecutionEvent event);
183+
184+
@Override
185+
default void beforeProjectLifecycleExecution(ProjectExecutionEvent event) {}
186+
187+
@Override
188+
default void afterProjectExecutionSuccess(ProjectExecutionEvent event) {}
189+
190+
@Override
191+
default void afterProjectExecutionFailure(ProjectExecutionEvent event) {}
192+
}
193+
}

0 commit comments

Comments
 (0)