Skip to content

Commit 3706aa1

Browse files
oehmegnodet
andauthored
[MNG-8670] Fix concurrent builder missing/wrong project events (#2251)
* [MNG-8670] Test case for missing/wrong project events Rather than adding another test, I've expanded on the one I previously wrote for ProjectStarted events. * [MNG-8670] Rework BuildPlanExecutor for better events --------- Co-authored-by: Guillaume Nodet <[email protected]>
1 parent 5d28926 commit 3706aa1

File tree

9 files changed

+298
-117
lines changed

9 files changed

+298
-117
lines changed

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

Lines changed: 193 additions & 100 deletions
Large diffs are not rendered by default.

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

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
import java.util.stream.Stream;
3131

3232
import org.apache.maven.api.Lifecycle;
33+
import org.apache.maven.api.annotations.Nonnull;
34+
import org.apache.maven.api.annotations.Nullable;
3335
import org.apache.maven.plugin.MojoExecution;
3436
import org.apache.maven.project.MavenProject;
3537

@@ -39,26 +41,43 @@ public class BuildStep {
3941
public static final int SCHEDULED = 2;
4042
public static final int EXECUTED = 3;
4143
public static final int FAILED = 4;
44+
public static final int SKIPPED = 5;
4245

4346
public static final String PLAN = "$plan$";
4447
public static final String SETUP = "$setup$";
4548
public static final String TEARDOWN = "$teardown$";
4649

50+
@Nonnull
4751
final MavenProject project;
52+
53+
@Nonnull
4854
final String name;
55+
56+
@Nullable
4957
final Lifecycle.Phase phase;
58+
5059
final Map<Integer, Map<String, MojoExecution>> mojos = new TreeMap<>();
5160
final Collection<BuildStep> predecessors = new HashSet<>();
5261
final Collection<BuildStep> successors = new HashSet<>();
5362
final AtomicInteger status = new AtomicInteger();
5463
final AtomicBoolean skip = new AtomicBoolean();
64+
volatile Exception exception;
5565

5666
public BuildStep(String name, MavenProject project, Lifecycle.Phase phase) {
57-
this.name = name;
58-
this.project = project;
67+
this.name = Objects.requireNonNull(name, "name cannot be null");
68+
this.project = Objects.requireNonNull(project, "project cannot be null");
5969
this.phase = phase;
6070
}
6171

72+
public boolean isCreated() {
73+
return status.get() == CREATED;
74+
}
75+
76+
public boolean isDone() {
77+
int state = status.get();
78+
return state == EXECUTED || state == FAILED || state == SKIPPED;
79+
}
80+
6281
public Stream<BuildStep> allPredecessors() {
6382
return preds(new HashSet<>()).stream();
6483
}
Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@
2222

2323
import org.junit.jupiter.api.Test;
2424

25-
public class MavenITmng8648ProjectStartedEventsTest extends AbstractMavenIntegrationTestCase {
25+
public class MavenITmng8648ProjectEventsTest extends AbstractMavenIntegrationTestCase {
2626

27-
public MavenITmng8648ProjectStartedEventsTest() {
27+
public MavenITmng8648ProjectEventsTest() {
2828
super("[4.0.0-rc-4,)");
2929
}
3030

@@ -40,16 +40,28 @@ public void test() throws Exception {
4040
File projectDir = extractResources("/mng-8648/project");
4141

4242
verifier = newVerifier(projectDir.getAbsolutePath());
43-
verifier.addCliArguments("compile", "-b", "concurrent");
44-
verifier.execute();
45-
verifier.verifyErrorFreeLog();
43+
verifier.addCliArguments("compile", "-b", "concurrent", "-T5");
44+
try {
45+
verifier.execute();
46+
} catch (VerificationException expected) {
47+
}
4648

47-
verifier.verifyTextInLog("org.apache.maven.its.mng8648:root:pom:1-SNAPSHOT started");
48-
verifier.verifyTextInLog("org.apache.maven.its.mng8648:root:pom:1-SNAPSHOT finished");
49-
verifier.verifyTextInLog("org.apache.maven.its.mng8648:subproject-a:jar:1-SNAPSHOT started");
50-
verifier.verifyTextInLog("org.apache.maven.its.mng8648:subproject-a:jar:1-SNAPSHOT finished");
51-
verifier.verifyTextInLog("org.apache.maven.its.mng8648:subproject-b:jar:1-SNAPSHOT started");
52-
verifier.verifyTextInLog("org.apache.maven.its.mng8648:subproject-b:jar:1-SNAPSHOT finished");
49+
// The root project is marked as successful with the traditional builder
50+
// With the concurrent builder, it gets no finish event and in the reactor summary it is listed as "skipped"
51+
verifier.verifyTextInLog("org.apache.maven.its.mng8648:root:pom:1-SNAPSHOT ProjectStarted");
52+
verifier.verifyTextInLog("org.apache.maven.its.mng8648:root:pom:1-SNAPSHOT ProjectSucceeded");
53+
verifier.verifyTextInLog("org.apache.maven.its.mng8648:subproject-a:jar:1-SNAPSHOT ProjectStarted");
54+
verifier.verifyTextInLog("org.apache.maven.its.mng8648:subproject-a:jar:1-SNAPSHOT ProjectSucceeded");
55+
verifier.verifyTextInLog("org.apache.maven.its.mng8648:subproject-b:jar:1-SNAPSHOT ProjectStarted");
56+
verifier.verifyTextInLog("org.apache.maven.its.mng8648:subproject-b:jar:1-SNAPSHOT ProjectSucceeded");
57+
verifier.verifyTextInLog("org.apache.maven.its.mng8648:subproject-c:jar:1-SNAPSHOT ProjectStarted");
58+
verifier.verifyTextInLog("org.apache.maven.its.mng8648:subproject-c:jar:1-SNAPSHOT ProjectFailed");
59+
// With the traditional builder, project D is not reported at all (it is never even started),
60+
// and in the reactor summary it is listed as "skipped".
61+
// With the concurrent builder, it should be started and later reported as "skipped"
62+
verifier.verifyTextInLog("org.apache.maven.its.mng8648:subproject-d:jar:1-SNAPSHOT ProjectStarted");
63+
verifier.verifyTextInLog("org.apache.maven.its.mng8648:subproject-d:jar:1-SNAPSHOT ProjectSkipped");
64+
// Make sure there's no problem with the event spy
5365
verifier.verifyTextNotInLog("Failed to notify spy org.apache.maven.its.mng8648.ProjectEventSpy");
5466
}
5567
}

its/core-it-suite/src/test/java/org/apache/maven/it/TestSuiteOrdering.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public TestSuiteOrdering() {
107107
suite.addTestSuite(MavenITmng8598JvmConfigSubstitutionTest.class);
108108
suite.addTestSuite(MavenITmng8653AfterAndEachPhasesWithConcurrentBuilderTest.class);
109109
suite.addTestSuite(MavenITmng5668AfterPhaseExecutionTest.class);
110-
suite.addTestSuite(MavenITmng8648ProjectStartedEventsTest.class);
110+
suite.addTestSuite(MavenITmng8648ProjectEventsTest.class);
111111
suite.addTestSuite(MavenITmng8645ConsumerPomDependencyManagementTest.class);
112112
suite.addTestSuite(MavenITmng8594AtFileTest.class);
113113
suite.addTestSuite(MavenITmng8561SourceRootTest.class);

its/core-it-suite/src/test/resources/mng-8648/extension/src/main/java/org/apache/maven/its/mng8648/ProjectEventSpy.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,17 @@ public void onEvent(Object event) {
3838
MavenProject project = executionEvent.getProject();
3939
switch (executionEvent.getType()) {
4040
case ProjectStarted:
41-
System.out.println(project.getId() + " started");
42-
projects.put(project.getId(), project);
41+
System.out.println(project.getId() + " " + executionEvent.getType());
42+
MavenProject existing = projects.put(project.getId(), project);
43+
if (existing != null) {
44+
throw new IllegalStateException("Project " + project.getId() + " was already started");
45+
}
4346
break;
4447
case ProjectSucceeded:
48+
case ProjectFailed:
49+
case ProjectSkipped:
50+
System.out.println(project.getId() + " " + executionEvent.getType());
4551
MavenProject mavenProject = projects.get(project.getId());
46-
System.out.println(project.getId() + " finished");
4752
if (mavenProject == null) {
4853
throw new IllegalStateException("Project " + project.getId() + " was never started");
4954
}

its/core-it-suite/src/test/resources/mng-8648/project/pom.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,7 @@
1111
<modules>
1212
<module>subproject-a</module>
1313
<module>subproject-b</module>
14+
<module>subproject-c</module>
15+
<module>subproject-d</module>
1416
</modules>
1517
</project>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/maven-v4_0_0.xsd">
3+
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<parent>
7+
<groupId>org.apache.maven.its.mng8648</groupId>
8+
<artifactId>root</artifactId>
9+
<version>1-SNAPSHOT</version>
10+
</parent>
11+
<artifactId>subproject-c</artifactId>
12+
</project>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
public class Foo implements Object {}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/maven-v4_0_0.xsd">
3+
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<parent>
7+
<groupId>org.apache.maven.its.mng8648</groupId>
8+
<artifactId>root</artifactId>
9+
<version>1-SNAPSHOT</version>
10+
</parent>
11+
<artifactId>subproject-d</artifactId>
12+
<dependencies>
13+
<dependency>
14+
<groupId>${project.groupId}</groupId>
15+
<artifactId>subproject-c</artifactId>
16+
<version>${project.version}</version>
17+
</dependency>
18+
</dependencies>
19+
</project>

0 commit comments

Comments
 (0)