This is an automated email from the ASF dual-hosted git repository.

gnodet pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven.git


The following commit(s) were added to refs/heads/master by this push:
     new 5603b7b20a [MNG-8648] Concurrent executor fires wrong ProjectStarted 
event (#2189)
5603b7b20a is described below

commit 5603b7b20af12fe099f6ba55f0bdc3d454f4df16
Author: Stefan Oehme <[email protected]>
AuthorDate: Fri Mar 28 10:28:30 2025 +0100

    [MNG-8648] Concurrent executor fires wrong ProjectStarted event (#2189)
    
    * MNG-8648: Add reproducer test case
    * Make sure the project is available when calling listener
    
    ---------
    
    Co-authored-by: Guillaume Nodet <[email protected]>
---
 .../internal/concurrent/BuildPlanExecutor.java     | 10 +++-
 .../it/MavenITmng8648ProjectStartedEventsTest.java | 55 ++++++++++++++++++++
 .../org/apache/maven/it/TestSuiteOrdering.java     |  1 +
 .../src/test/resources/mng-8648/extension/pom.xml  | 38 ++++++++++++++
 .../apache/maven/its/mng8648/ProjectEventSpy.java  | 59 ++++++++++++++++++++++
 .../main/resources/META-INF/plexus/components.xml  | 29 +++++++++++
 .../resources/mng-8648/project/.mvn/extensions.xml |  7 +++
 .../src/test/resources/mng-8648/project/pom.xml    | 15 ++++++
 .../mng-8648/project/subproject-a/pom.xml          | 12 +++++
 .../mng-8648/project/subproject-b/pom.xml          | 11 ++++
 10 files changed, 235 insertions(+), 2 deletions(-)

diff --git 
a/impl/maven-core/src/main/java/org/apache/maven/lifecycle/internal/concurrent/BuildPlanExecutor.java
 
b/impl/maven-core/src/main/java/org/apache/maven/lifecycle/internal/concurrent/BuildPlanExecutor.java
index 664509d2a1..4d46080f38 100644
--- 
a/impl/maven-core/src/main/java/org/apache/maven/lifecycle/internal/concurrent/BuildPlanExecutor.java
+++ 
b/impl/maven-core/src/main/java/org/apache/maven/lifecycle/internal/concurrent/BuildPlanExecutor.java
@@ -359,12 +359,14 @@ private void executeStep(BuildStep step) throws 
IOException, LifecycleExecutionE
                     // Planning steps should be executed out of normal 
execution
                     throw new IllegalStateException();
                 case SETUP:
+                    attachToThread(step);
                     consumerPomArtifactTransformer.injectTransformedArtifacts(
                             session.getRepositorySession(), step.project);
                     projectExecutionListener.beforeProjectExecution(new 
ProjectExecutionEvent(session, step.project));
                     eventCatapult.fire(ExecutionEvent.Type.ProjectStarted, 
session, null);
                     break;
                 case TEARDOWN:
+                    attachToThread(step);
                     projectExecutionListener.afterProjectExecutionSuccess(
                             new ProjectExecutionEvent(session, step.project, 
Collections.emptyList()));
                     reactorContext
@@ -375,8 +377,7 @@ private void executeStep(BuildStep step) throws 
IOException, LifecycleExecutionE
                 default:
                     List<MojoExecution> executions = 
step.executions().collect(Collectors.toList());
                     if (!executions.isEmpty()) {
-                        attachToThread(step.project);
-                        session.setCurrentProject(step.project);
+                        attachToThread(step);
                         clock.start();
                         executions.forEach(mojoExecution -> {
                             
mojoExecutionConfigurator(mojoExecution).configure(step.project, mojoExecution, 
true);
@@ -390,6 +391,11 @@ private void executeStep(BuildStep step) throws 
IOException, LifecycleExecutionE
             step.status.compareAndSet(SCHEDULED, EXECUTED);
         }
 
+        private void attachToThread(BuildStep step) {
+            BuildPlanExecutor.attachToThread(step.project);
+            session.setCurrentProject(step.project);
+        }
+
         private Clock getClock(Object key) {
             return clocks.computeIfAbsent(key, p -> new Clock());
         }
diff --git 
a/its/core-it-suite/src/test/java/org/apache/maven/it/MavenITmng8648ProjectStartedEventsTest.java
 
b/its/core-it-suite/src/test/java/org/apache/maven/it/MavenITmng8648ProjectStartedEventsTest.java
new file mode 100644
index 0000000000..c117cbb4a8
--- /dev/null
+++ 
b/its/core-it-suite/src/test/java/org/apache/maven/it/MavenITmng8648ProjectStartedEventsTest.java
@@ -0,0 +1,55 @@
+/*
+ * 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.it;
+
+import java.io.File;
+
+import org.junit.jupiter.api.Test;
+
+public class MavenITmng8648ProjectStartedEventsTest extends 
AbstractMavenIntegrationTestCase {
+
+    public MavenITmng8648ProjectStartedEventsTest() {
+        super("[4.0.0-rc-4,)");
+    }
+
+    @Test
+    public void test() throws Exception {
+        File extensionDir = extractResources("/mng-8648/extension");
+
+        Verifier verifier = newVerifier(extensionDir.getAbsolutePath());
+        verifier.addCliArgument("install");
+        verifier.execute();
+        verifier.verifyErrorFreeLog();
+
+        File projectDir = extractResources("/mng-8648/project");
+
+        verifier = newVerifier(projectDir.getAbsolutePath());
+        verifier.addCliArguments("compile", "-b", "concurrent");
+        verifier.execute();
+        verifier.verifyErrorFreeLog();
+
+        
verifier.verifyTextInLog("org.apache.maven.its.mng8648:root:pom:1-SNAPSHOT 
started");
+        
verifier.verifyTextInLog("org.apache.maven.its.mng8648:root:pom:1-SNAPSHOT 
finished");
+        
verifier.verifyTextInLog("org.apache.maven.its.mng8648:subproject-a:jar:1-SNAPSHOT
 started");
+        
verifier.verifyTextInLog("org.apache.maven.its.mng8648:subproject-a:jar:1-SNAPSHOT
 finished");
+        
verifier.verifyTextInLog("org.apache.maven.its.mng8648:subproject-b:jar:1-SNAPSHOT
 started");
+        
verifier.verifyTextInLog("org.apache.maven.its.mng8648:subproject-b:jar:1-SNAPSHOT
 finished");
+        verifier.verifyTextNotInLog("Failed to notify spy 
org.apache.maven.its.mng8648.ProjectEventSpy");
+    }
+}
diff --git 
a/its/core-it-suite/src/test/java/org/apache/maven/it/TestSuiteOrdering.java 
b/its/core-it-suite/src/test/java/org/apache/maven/it/TestSuiteOrdering.java
index dcbfb085c6..ff3ec29fb2 100644
--- a/its/core-it-suite/src/test/java/org/apache/maven/it/TestSuiteOrdering.java
+++ b/its/core-it-suite/src/test/java/org/apache/maven/it/TestSuiteOrdering.java
@@ -101,6 +101,7 @@ public TestSuiteOrdering() {
          * the tests are to finishing. Newer tests are also more likely to 
fail, so this is
          * a fail fast technique as well.
          */
+        suite.addTestSuite(MavenITmng8648ProjectStartedEventsTest.class);
         suite.addTestSuite(MavenITmng8594AtFileTest.class);
         suite.addTestSuite(MavenITmng8561SourceRootTest.class);
         suite.addTestSuite(MavenITmng8523ModelPropertiesTest.class);
diff --git a/its/core-it-suite/src/test/resources/mng-8648/extension/pom.xml 
b/its/core-it-suite/src/test/resources/mng-8648/extension/pom.xml
new file mode 100644
index 0000000000..2a8ba47a99
--- /dev/null
+++ b/its/core-it-suite/src/test/resources/mng-8648/extension/pom.xml
@@ -0,0 +1,38 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+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.
+-->
+<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 
http://maven.apache.org/xsd/maven-4.0.0.xsd";>
+  <modelVersion>4.0.0</modelVersion>
+
+  <groupId>org.apache.maven.its.mng8648</groupId>
+  <artifactId>mng-8648-project-listener-extension</artifactId>
+  <version>1.0</version>
+  <packaging>jar</packaging>
+
+  <name>Maven IT Plugin :: mng-8648 extension</name>
+
+  <dependencies>
+    <dependency>
+      <groupId>org.apache.maven</groupId>
+      <artifactId>maven-core</artifactId>
+      <version>4.0.0-rc-4-SNAPSHOT</version>
+      <scope>provided</scope>
+    </dependency>
+  </dependencies>
+</project>
diff --git 
a/its/core-it-suite/src/test/resources/mng-8648/extension/src/main/java/org/apache/maven/its/mng8648/ProjectEventSpy.java
 
b/its/core-it-suite/src/test/resources/mng-8648/extension/src/main/java/org/apache/maven/its/mng8648/ProjectEventSpy.java
new file mode 100644
index 0000000000..6678757aca
--- /dev/null
+++ 
b/its/core-it-suite/src/test/resources/mng-8648/extension/src/main/java/org/apache/maven/its/mng8648/ProjectEventSpy.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.its.mng8648;
+
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+
+import org.apache.maven.eventspy.EventSpy;
+import org.apache.maven.execution.ExecutionEvent;
+import org.apache.maven.project.MavenProject;
+
+public class ProjectEventSpy implements EventSpy {
+    private final ConcurrentMap<String, MavenProject> projects = new 
ConcurrentHashMap<>();
+
+    @Override
+    public void init(Context context) {}
+
+    @Override
+    public void onEvent(Object event) {
+        if (event instanceof ExecutionEvent) {
+            ExecutionEvent executionEvent = (ExecutionEvent) event;
+            MavenProject project = executionEvent.getProject();
+            switch (executionEvent.getType()) {
+                case ProjectStarted:
+                    System.out.println(project.getId() + " started");
+                    projects.put(project.getId(), project);
+                    break;
+                case ProjectSucceeded:
+                    MavenProject mavenProject = projects.get(project.getId());
+                    System.out.println(project.getId() + " finished");
+                    if (mavenProject == null) {
+                        throw new IllegalStateException("Project " + 
project.getId() + " was never started");
+                    }
+                    break;
+                default:
+                    break;
+            }
+        }
+    }
+
+    @Override
+    public void close() {}
+}
diff --git 
a/its/core-it-suite/src/test/resources/mng-8648/extension/src/main/resources/META-INF/plexus/components.xml
 
b/its/core-it-suite/src/test/resources/mng-8648/extension/src/main/resources/META-INF/plexus/components.xml
new file mode 100644
index 0000000000..e6190b45f1
--- /dev/null
+++ 
b/its/core-it-suite/src/test/resources/mng-8648/extension/src/main/resources/META-INF/plexus/components.xml
@@ -0,0 +1,29 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+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.
+-->
+<component-set>
+  <components>
+
+    <component>
+      <role>org.apache.maven.eventspy.EventSpy</role>
+      <role-hint>mng8648</role-hint>
+      
<implementation>org.apache.maven.its.mng8648.ProjectEventSpy</implementation>
+    </component>
+  </components>
+</component-set>
diff --git 
a/its/core-it-suite/src/test/resources/mng-8648/project/.mvn/extensions.xml 
b/its/core-it-suite/src/test/resources/mng-8648/project/.mvn/extensions.xml
new file mode 100644
index 0000000000..3691e81311
--- /dev/null
+++ b/its/core-it-suite/src/test/resources/mng-8648/project/.mvn/extensions.xml
@@ -0,0 +1,7 @@
+<extensions>
+    <extension>
+        <groupId>org.apache.maven.its.mng8648</groupId>
+        <artifactId>mng-8648-project-listener-extension</artifactId>
+        <version>1.0</version>
+    </extension>
+</extensions>
diff --git a/its/core-it-suite/src/test/resources/mng-8648/project/pom.xml 
b/its/core-it-suite/src/test/resources/mng-8648/project/pom.xml
new file mode 100644
index 0000000000..acf5b1832c
--- /dev/null
+++ b/its/core-it-suite/src/test/resources/mng-8648/project/pom.xml
@@ -0,0 +1,15 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<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";>
+
+  <modelVersion>4.0.0</modelVersion>
+
+  <groupId>org.apache.maven.its.mng8648</groupId>
+  <artifactId>root</artifactId>
+  <version>1-SNAPSHOT</version>
+  <packaging>pom</packaging>
+
+  <modules>
+    <module>subproject-a</module>
+    <module>subproject-b</module>
+  </modules>
+</project>
diff --git 
a/its/core-it-suite/src/test/resources/mng-8648/project/subproject-a/pom.xml 
b/its/core-it-suite/src/test/resources/mng-8648/project/subproject-a/pom.xml
new file mode 100644
index 0000000000..fe4d6a635a
--- /dev/null
+++ b/its/core-it-suite/src/test/resources/mng-8648/project/subproject-a/pom.xml
@@ -0,0 +1,12 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<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";>
+
+  <modelVersion>4.0.0</modelVersion>
+
+  <parent>
+    <groupId>org.apache.maven.its.mng8648</groupId>
+    <artifactId>root</artifactId>
+    <version>1-SNAPSHOT</version>
+  </parent>
+  <artifactId>subproject-a</artifactId>
+</project>
diff --git 
a/its/core-it-suite/src/test/resources/mng-8648/project/subproject-b/pom.xml 
b/its/core-it-suite/src/test/resources/mng-8648/project/subproject-b/pom.xml
new file mode 100644
index 0000000000..585871705c
--- /dev/null
+++ b/its/core-it-suite/src/test/resources/mng-8648/project/subproject-b/pom.xml
@@ -0,0 +1,11 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<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";>
+
+  <modelVersion>4.0.0</modelVersion>
+  <parent>
+    <groupId>org.apache.maven.its.mng8648</groupId>
+    <artifactId>root</artifactId>
+    <version>1-SNAPSHOT</version>
+  </parent>
+  <artifactId>subproject-b</artifactId>
+</project>

Reply via email to