gnodet-bot commented on code in PR #13647: URL: https://github.com/apache/maven/pull/13647#discussion_r4548939374
########## impl/maven-core/src/main/java/org/apache/maven/internal/impl/ProjectTransformerDispatcher.java: ########## @@ -0,0 +1,107 @@ +/* + * 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.Collection; +import java.util.List; +import java.util.Objects; + +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; + +/** + * Dispatches {@link ProjectTransformer} instances over all reactor projects, + * chaining each transformer's output model as the next transformer's input. + */ +public final class ProjectTransformerDispatcher { + + private ProjectTransformerDispatcher() {} + + public static void transformProjects(MavenSession session, Collection<ProjectTransformer> transformers) + throws MavenExecutionException { + if (transformers.isEmpty()) { + return; + } + List<ProjectTransformer> chain = List.copyOf(transformers); + InternalMavenSession apiSession = InternalMavenSession.from(session.getSession()); + Thread thread = Thread.currentThread(); + ClassLoader originalClassLoader = thread.getContextClassLoader(); + try { + for (MavenProject mavenProject : session.getProjects()) { + Project project = apiSession.getProject(mavenProject); + Model model = project.model(); + + for (ProjectTransformer transformer : chain) { + thread.setContextClassLoader(transformer.getClass().getClassLoader()); + try { + model = transformer.transformProject(apiSession, project, model); + } catch (RuntimeException e) { + throw new MavenExecutionException( + "Project transformer failed for " + mavenProject.getId(), e); + } + if (model == null) { + throw new MavenExecutionException( + "Project transformer " + + transformer.getClass().getName() + " returned null model for " + + mavenProject.getId(), + (Throwable) null); + } + validateFrozenFields( + project.model(), model, transformer.getClass().getName(), mavenProject.getId()); + } + + if (model != project.model()) { + mavenProject.setModel(new org.apache.maven.model.Model(model)); + } + } + } finally { + thread.setContextClassLoader(originalClassLoader); + } + } + + private static void validateFrozenFields( + Model original, Model transformed, String transformerName, String projectId) + throws MavenExecutionException { + if (!Objects.equals(original.getGroupId(), transformed.getGroupId()) + || !Objects.equals(original.getArtifactId(), transformed.getArtifactId())) { + throw new MavenExecutionException( + transformerName + " must not change groupId or artifactId of " + projectId, (Throwable) null); + } + if (!Objects.equals(original.getParent(), transformed.getParent())) { + throw new MavenExecutionException( + transformerName + " must not change <parent> of " + projectId, (Throwable) null); + } + if (!Objects.equals(original.getMixins(), transformed.getMixins())) { + throw new MavenExecutionException( + transformerName + " must not change <mixins> of " + projectId, (Throwable) null); + } + if (!Objects.equals(original.getModules(), transformed.getModules())) { Review Comment: ⚠️ **`validateFrozenFields` omits `<subprojects>`** — `ModelBase` has two separate fields: `getModules()` (`@Deprecated(since = "4.0.0")`) and `getSubprojects()` (the Maven 4.1.0 replacement). A transformer using `model.withSubprojects(...)` bypasses this check silently. Add the same guard: ```suggestion private static void validateFrozenFields( Model original, Model transformed, String transformerName, String projectId) throws MavenExecutionException { if (!Objects.equals(original.getGroupId(), transformed.getGroupId()) || !Objects.equals(original.getArtifactId(), transformed.getArtifactId())) { throw new MavenExecutionException( transformerName + " must not change groupId or artifactId of " + projectId, (Throwable) null); } if (!Objects.equals(original.getParent(), transformed.getParent())) { throw new MavenExecutionException( transformerName + " must not change <parent> of " + projectId, (Throwable) null); } if (!Objects.equals(original.getMixins(), transformed.getMixins())) { throw new MavenExecutionException( transformerName + " must not change <mixins> of " + projectId, (Throwable) null); } if (!Objects.equals(original.getModules(), transformed.getModules())) { throw new MavenExecutionException( transformerName + " must not change <modules> of " + projectId, (Throwable) null); } if (!Objects.equals(original.getSubprojects(), transformed.getSubprojects())) { throw new MavenExecutionException( transformerName + " must not change <subprojects> of " + projectId, (Throwable) null); } if (!Objects.equals(original.getProfiles(), transformed.getProfiles())) { throw new MavenExecutionException( transformerName + " must not change <profiles> of " + projectId, (Throwable) null); } } ``` ########## 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 is wrong** — the runtime validates after *each* transformer (inside the loop), not "after all transformers have run". This is the right design — it names the exact offending transformer in the error message. Update the description: ```suggestion * The runtime rejects changes to the following fields after each transformer runs — modifying ``` ########## 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: 💡 **`<subprojects>` missing from frozen-field list** — `<modules>` is deprecated since 4.0.0; `<subprojects>` is the Maven 4.1.0 replacement and a separate field. Both should be listed: ```suggestion * <li>{@code <modules>}, {@code <subprojects>} — project set is fixed at this point</li> ``` -- 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]
