gnodet-bot commented on code in PR #13647:
URL: https://github.com/apache/maven/pull/13647#discussion_r4550262133


##########
api/maven-api-spi/src/main/java/org/apache/maven/api/spi/ProjectTransformer.java:
##########
@@ -0,0 +1,79 @@
+/*
+ * 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.spi;
+
+import org.apache.maven.api.Project;
+import org.apache.maven.api.Session;
+import org.apache.maven.api.annotations.Consumer;
+import org.apache.maven.api.annotations.Experimental;
+import org.apache.maven.api.annotations.Nonnull;
+import org.apache.maven.api.model.Model;
+import org.apache.maven.api.services.MavenException;
+
+/**
+ * Allows build extensions to transform project models after all reactor 
projects have been read
+ * and before the reactor is sorted for execution. This is the v4 API 
equivalent of
+ * {@code AbstractMavenLifecycleParticipant.afterProjectsRead()}.
+ * <p>
+ * Transformers are called in sequence for each reactor project; the model 
returned by one transformer
+ * is passed as input to the next. The final model is written back into the 
project by the runtime.
+ * <p>
+ * Implementations may return a modified model built via {@code 
Model.newBuilder(model)...build()},
+ * or return the input model unmodified for a no-op. Throwing aborts the build.
+ *
+ * <h2>Allowed modifications</h2>
+ * At this point in the build, no artifacts have been resolved and no plugin 
classloaders have been
+ * created. The following model sections are safe to modify:
+ * <ul>
+ *   <li>{@code <dependencies>} — e.g. injecting resolved platform 
dependencies</li>
+ *   <li>{@code <dependencyManagement>}</li>
+ *   <li>{@code <build>}, {@code <plugins>}, {@code <pluginManagement>} — e.g. 
injecting JVM args
+ *       into Surefire/Failsafe configuration</li>
+ *   <li>{@code <properties>}</li>
+ * </ul>
+ *
+ * <h2>Frozen fields</h2>
+ * The runtime rejects changes to the following fields after all transformers 
have run — modifying
+ * them would break reactor identity, inheritance, or module structure that 
has already been assembled:
+ * <ul>
+ *   <li>{@code groupId}, {@code artifactId} — reactor identity</li>
+ *   <li>{@code <parent>}, {@code <mixins>} — inheritance already resolved</li>
+ *   <li>{@code <modules>} — project set is fixed at this point</li>

Review Comment:
   ⚠️ **Missing `<subprojects>` in frozen-field list.**
   
   `<subprojects>` (the Maven 4 equivalent of `<modules>` for multi-module 
builds) is not listed here. It should be listed alongside `<modules>` since it 
also affects module structure that has already been assembled. The 
`validateFrozenFields` implementation in `ProjectTransformerDispatcher` also 
needs a corresponding check for `getSubprojects()`.



##########
impl/maven-core/src/test/java/org/apache/maven/internal/impl/ProjectTransformerDispatcherTest.java:
##########
@@ -0,0 +1,102 @@
+/*
+ * 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.ArrayList;
+import java.util.List;
+
+import org.apache.maven.MavenExecutionException;
+import org.apache.maven.api.Project;
+import org.apache.maven.api.model.Model;
+import org.apache.maven.api.spi.ProjectTransformer;
+import org.apache.maven.execution.MavenSession;
+import org.apache.maven.project.MavenProject;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+class ProjectTransformerDispatcherTest {
+
+    @Test
+    void chainsTransformersAndWritesModelBack() throws Exception {
+        MavenProject mavenProject = new MavenProject();
+        mavenProject.setGroupId("org.test");
+        mavenProject.setArtifactId("test");
+        mavenProject.setVersion("1.0");
+
+        MavenSession mavenSession = mock(MavenSession.class);
+        InternalMavenSession apiSession = mock(InternalMavenSession.class);
+        when(mavenSession.getSession()).thenReturn(apiSession);
+        when(mavenSession.getProjects()).thenReturn(List.of(mavenProject));
+
+        Project apiProject = mock(Project.class);
+        Model originalModel = mavenProject.getModel().getDelegate();
+        when(apiSession.getProject(mavenProject)).thenReturn(apiProject);
+        when(apiProject.model()).thenReturn(originalModel);
+
+        List<String> calls = new ArrayList<>();
+        ProjectTransformer addProperty = (session, project, model) -> {
+            calls.add("addProperty");
+            return model.withProperties(java.util.Map.of("injected", "true"));
+        };
+        ProjectTransformer addProperty2 = (session, project, model) -> {
+            calls.add("addProperty2");
+            assertEquals("true", model.getProperties().get("injected"));
+            return model.withProperties(java.util.Map.of("injected", "true", 
"second", "yes"));
+        };
+
+        ProjectTransformerDispatcher.transformProjects(mavenSession, 
List.of(addProperty, addProperty2));
+
+        assertEquals(List.of("addProperty", "addProperty2"), calls);
+        assertEquals("yes", 
mavenProject.getModel().getDelegate().getProperties().get("second"));
+    }
+
+    @Test
+    void runtimeExceptionWrappedInMavenExecutionException() {
+        MavenProject mavenProject = new MavenProject();
+        mavenProject.setGroupId("org.test");
+        mavenProject.setArtifactId("test");
+        mavenProject.setVersion("1.0");
+
+        MavenSession mavenSession = mock(MavenSession.class);
+        InternalMavenSession apiSession = mock(InternalMavenSession.class);
+        when(mavenSession.getSession()).thenReturn(apiSession);
+        when(mavenSession.getProjects()).thenReturn(List.of(mavenProject));
+
+        Project apiProject = mock(Project.class);
+        when(apiSession.getProject(mavenProject)).thenReturn(apiProject);
+        
when(apiProject.model()).thenReturn(mavenProject.getModel().getDelegate());
+
+        RuntimeException cause = new RuntimeException("buggy extension");
+        ProjectTransformer failing = (session, project, model) -> {
+            throw cause;
+        };
+
+        MavenExecutionException ex =
+                assertThrows(MavenExecutionException.class, () -> 
ProjectTransformerDispatcher.transformProjects(
+                        mavenSession, List.of(failing)));
+
+        assertInstanceOf(RuntimeException.class, ex.getCause());
+        assertEquals("buggy extension", ex.getCause().getMessage());
+    }

Review Comment:
   ⚠️ **Missing test for the frozen-field rejection path.**
   
   Neither `chainsTransformersAndWritesModelBack` nor 
`runtimeExceptionWrappedInMavenExecutionException` exercises 
`validateFrozenFields` with a transformer that actually mutates a frozen field. 
Add a test like:
   
   ```java
   @Test
   void frozenFieldViolationThrowsMavenExecutionException() {
       // set up MavenProject/MavenSession/apiSession mocks as above...
       ProjectTransformer mutatesGroupId = (session, project, model) ->
           model.withGroupId("com.evil");
       MavenExecutionException ex = assertThrows(MavenExecutionException.class,
           () -> ProjectTransformerDispatcher.transformProjects(mavenSession, 
List.of(mutatesGroupId)));
       assertTrue(ex.getMessage().contains("groupId/artifactId"));
   }
   ```



##########
impl/maven-core/src/main/java/org/apache/maven/lifecycle/internal/LifecycleModuleBuilder.java:
##########
@@ -113,6 +114,7 @@ public void buildProject(
 
             projectExecutionListener.afterProjectExecutionSuccess(
                     new ProjectExecutionEvent(session, currentProject, 
mojoExecutions));
+            successReported = true;

Review Comment:
   ⚠️ **`successReported = true` should be set before the listener call.**
   
   If `afterProjectExecutionSuccess(...)` throws, `successReported` is still 
`false` when the catch block runs — triggering `afterProjectExecutionFailure` 
for a build that actually succeeded. This is a listener-exception handling 
inconsistency. Set `successReported = true` before calling the listener, or 
restructure so listener exceptions don't route to the failure path:
   
   ```suggestion
               successReported = true;
               projectExecutionListener.afterProjectExecutionSuccess(
                       new ProjectExecutionEvent(session, currentProject, 
mojoExecutions));
   ```



##########
api/maven-api-spi/src/main/java/org/apache/maven/api/spi/ProjectTransformer.java:
##########
@@ -0,0 +1,79 @@
+/*
+ * 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.spi;
+
+import org.apache.maven.api.Project;
+import org.apache.maven.api.Session;
+import org.apache.maven.api.annotations.Consumer;
+import org.apache.maven.api.annotations.Experimental;
+import org.apache.maven.api.annotations.Nonnull;
+import org.apache.maven.api.model.Model;
+import org.apache.maven.api.services.MavenException;
+
+/**
+ * Allows build extensions to transform project models after all reactor 
projects have been read
+ * and before the reactor is sorted for execution. This is the v4 API 
equivalent of
+ * {@code AbstractMavenLifecycleParticipant.afterProjectsRead()}.
+ * <p>
+ * Transformers are called in sequence for each reactor project; the model 
returned by one transformer
+ * is passed as input to the next. The final model is written back into the 
project by the runtime.
+ * <p>
+ * Implementations may return a modified model built via {@code 
Model.newBuilder(model)...build()},
+ * or return the input model unmodified for a no-op. Throwing aborts the build.
+ *
+ * <h2>Allowed modifications</h2>
+ * At this point in the build, no artifacts have been resolved and no plugin 
classloaders have been
+ * created. The following model sections are safe to modify:
+ * <ul>
+ *   <li>{@code <dependencies>} — e.g. injecting resolved platform 
dependencies</li>
+ *   <li>{@code <dependencyManagement>}</li>
+ *   <li>{@code <build>}, {@code <plugins>}, {@code <pluginManagement>} — e.g. 
injecting JVM args
+ *       into Surefire/Failsafe configuration</li>
+ *   <li>{@code <properties>}</li>
+ * </ul>
+ *
+ * <h2>Frozen fields</h2>
+ * The runtime rejects changes to the following fields after all transformers 
have run — modifying

Review Comment:
   ⚠️ **Javadoc timing claim doesn't match the implementation.**
   
   *"after all transformers have run"* implies a final-state check at the end 
of the chain. The actual code in 
`ProjectTransformerDispatcher.transformProjects()` calls `validateFrozenFields` 
inside the per-transformer loop — i.e., it validates after each transformer, 
not after all of them. Update to: *"The runtime rejects changes to the 
following fields — the check runs after each transformer in the chain"* or 
similar.



-- 
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]

Reply via email to