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


##########
impl/maven-core/src/main/java/org/apache/maven/configuration/internal/DefaultMojoConfigurationService.java:
##########
@@ -0,0 +1,186 @@
+/*
+ * 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.configuration.internal;
+
+import javax.inject.Inject;
+import javax.inject.Named;
+
+import java.util.HashSet;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.maven.api.MojoExecution;
+import org.apache.maven.api.Project;
+import org.apache.maven.api.Service;
+import org.apache.maven.api.di.SessionScoped;
+import org.apache.maven.api.plugin.descriptor.Parameter;
+import org.apache.maven.api.services.MojoConfigurationService;
+import org.apache.maven.internal.impl.DefaultMojoExecution;
+import org.apache.maven.internal.impl.DefaultProject;
+import org.apache.maven.internal.impl.InternalMavenSession;
+import org.apache.maven.internal.xml.XmlPlexusConfiguration;
+import org.apache.maven.plugin.MavenPluginManager;
+import org.apache.maven.plugin.PluginContainerException;
+import org.apache.maven.plugin.PluginParameterExpressionEvaluatorV4;
+import org.apache.maven.plugin.PluginResolutionException;
+import org.apache.maven.plugin.descriptor.PluginDescriptor;
+import org.apache.maven.plugin.internal.PluginConfigurationInterpolator;
+import org.codehaus.plexus.classworlds.realm.ClassRealm;

Review Comment:
   🟡 Cross-package coupling: `DefaultMojoConfigurationService` lives in 
`configuration.internal` but imports `PluginConfigurationInterpolator` from 
`plugin.internal`. Consider moving `PluginConfigurationInterpolator` to 
`configuration.internal` or exposing it through a non-internal abstraction.



##########
api/maven-api-core/src/main/java/org/apache/maven/api/services/MojoConfigurationService.java:
##########
@@ -0,0 +1,63 @@
+/*
+ * 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.services;
+
+import java.util.Map;
+
+import org.apache.maven.api.MojoExecution;
+import org.apache.maven.api.Project;
+import org.apache.maven.api.Service;
+import org.apache.maven.api.annotations.Experimental;
+import org.apache.maven.api.annotations.Nonnull;
+
+/**
+ * Provides access to the configured parameters of a Mojo without 
instantiating the root Mojo, invoking its setters,
+ * or running its {@code execute} method. Resolving parameters may require 
resolving the plugin and setting up its
+ * class realm. Converting nested configuration may instantiate nested 
parameter objects and invoke their setters.
+ *
+ * @since 4.1.0
+ */

Review Comment:
   🔴 Wrong `@since` tag — `master` is currently `4.2.0-SNAPSHOT`, so this must 
be `@since 4.2.0`.



##########
impl/maven-core/src/main/java/org/apache/maven/configuration/internal/DefaultMojoConfigurationService.java:
##########
@@ -0,0 +1,186 @@
+/*
+ * 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.configuration.internal;
+
+import javax.inject.Inject;
+import javax.inject.Named;
+
+import java.util.HashSet;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.maven.api.MojoExecution;
+import org.apache.maven.api.Project;
+import org.apache.maven.api.Service;
+import org.apache.maven.api.di.SessionScoped;
+import org.apache.maven.api.plugin.descriptor.Parameter;
+import org.apache.maven.api.services.MojoConfigurationService;
+import org.apache.maven.internal.impl.DefaultMojoExecution;
+import org.apache.maven.internal.impl.DefaultProject;
+import org.apache.maven.internal.impl.InternalMavenSession;
+import org.apache.maven.internal.xml.XmlPlexusConfiguration;
+import org.apache.maven.plugin.MavenPluginManager;
+import org.apache.maven.plugin.PluginContainerException;
+import org.apache.maven.plugin.PluginParameterExpressionEvaluatorV4;
+import org.apache.maven.plugin.PluginResolutionException;
+import org.apache.maven.plugin.descriptor.PluginDescriptor;
+import org.apache.maven.plugin.internal.PluginConfigurationInterpolator;
+import org.codehaus.plexus.classworlds.realm.ClassRealm;
+import 
org.codehaus.plexus.component.configurator.ComponentConfigurationException;
+import 
org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
+import org.codehaus.plexus.configuration.DefaultPlexusConfiguration;
+import org.codehaus.plexus.configuration.PlexusConfiguration;
+import org.eclipse.sisu.Typed;
+
+@Named
+@Typed({MojoConfigurationService.class, Service.class})
+@SessionScoped
+public class DefaultMojoConfigurationService implements 
MojoConfigurationService {
+
+    private final InternalMavenSession session;
+    private final MavenPluginManager pluginManager;
+
+    @Inject
+    public DefaultMojoConfigurationService(InternalMavenSession session, 
MavenPluginManager pluginManager) {
+        this.session = session;
+        this.pluginManager = pluginManager;
+    }
+
+    @Override
+    public Map<String, Object> getParameters(Project project, MojoExecution 
mojoExecution) {
+        if (!(mojoExecution instanceof DefaultMojoExecution execution)) {
+            throw new IllegalArgumentException("Unsupported MojoExecution 
implementation");
+        }

Review Comment:
   🟠 The exception message omits the actual class name, making runtime failures 
hard to diagnose. Prefer:
   ```java
   throw new IllegalArgumentException("Unsupported MojoExecution 
implementation: " + mojoExecution.getClass().getName());
   ```



##########
impl/maven-core/src/main/java/org/apache/maven/configuration/internal/DefaultMojoConfigurationService.java:
##########
@@ -0,0 +1,186 @@
+/*
+ * 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.configuration.internal;
+
+import javax.inject.Inject;
+import javax.inject.Named;
+
+import java.util.HashSet;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.maven.api.MojoExecution;
+import org.apache.maven.api.Project;
+import org.apache.maven.api.Service;
+import org.apache.maven.api.di.SessionScoped;
+import org.apache.maven.api.plugin.descriptor.Parameter;
+import org.apache.maven.api.services.MojoConfigurationService;
+import org.apache.maven.internal.impl.DefaultMojoExecution;
+import org.apache.maven.internal.impl.DefaultProject;
+import org.apache.maven.internal.impl.InternalMavenSession;
+import org.apache.maven.internal.xml.XmlPlexusConfiguration;
+import org.apache.maven.plugin.MavenPluginManager;
+import org.apache.maven.plugin.PluginContainerException;
+import org.apache.maven.plugin.PluginParameterExpressionEvaluatorV4;
+import org.apache.maven.plugin.PluginResolutionException;
+import org.apache.maven.plugin.descriptor.PluginDescriptor;
+import org.apache.maven.plugin.internal.PluginConfigurationInterpolator;
+import org.codehaus.plexus.classworlds.realm.ClassRealm;
+import 
org.codehaus.plexus.component.configurator.ComponentConfigurationException;
+import 
org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
+import org.codehaus.plexus.configuration.DefaultPlexusConfiguration;
+import org.codehaus.plexus.configuration.PlexusConfiguration;
+import org.eclipse.sisu.Typed;
+
+@Named
+@Typed({MojoConfigurationService.class, Service.class})
+@SessionScoped
+public class DefaultMojoConfigurationService implements 
MojoConfigurationService {
+
+    private final InternalMavenSession session;
+    private final MavenPluginManager pluginManager;
+
+    @Inject
+    public DefaultMojoConfigurationService(InternalMavenSession session, 
MavenPluginManager pluginManager) {
+        this.session = session;
+        this.pluginManager = pluginManager;
+    }
+
+    @Override
+    public Map<String, Object> getParameters(Project project, MojoExecution 
mojoExecution) {
+        if (!(mojoExecution instanceof DefaultMojoExecution execution)) {
+            throw new IllegalArgumentException("Unsupported MojoExecution 
implementation");
+        }
+
+        if (!(project instanceof DefaultProject defaultProject)) {
+            throw new IllegalArgumentException("Unsupported Project 
implementation");
+        }

Review Comment:
   🟠 Same here — include `project.getClass().getName()` in the exception 
message.



##########
impl/maven-core/src/test/java/org/apache/maven/configuration/internal/DefaultMojoConfigurationServiceTest.java:
##########
@@ -0,0 +1,787 @@
+/*
+ * 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.configuration.internal;
+
+import javax.inject.Inject;
+
+import java.io.File;
+import java.net.URI;
+import java.nio.file.Path;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.maven.AbstractCoreMavenComponentTestCase;
+import org.apache.maven.api.MojoExecution;
+import org.apache.maven.api.Project;
+import org.apache.maven.api.plugin.descriptor.Parameter;
+import org.apache.maven.api.services.MojoConfigurationService;
+import org.apache.maven.internal.impl.DefaultLookup;
+import org.apache.maven.internal.impl.DefaultMojoExecution;
+import org.apache.maven.internal.impl.DefaultProject;
+import org.apache.maven.internal.impl.DefaultSession;
+import org.apache.maven.internal.impl.InternalMavenSession;
+import org.apache.maven.internal.xml.XmlPlexusConfiguration;
+import org.apache.maven.plugin.MavenPluginManager;
+import org.apache.maven.plugin.PluginParameterExpressionEvaluatorV4;
+import org.apache.maven.plugin.descriptor.PluginDescriptor;
+import org.apache.maven.plugin.internal.PluginConfigurationInterpolator;
+import org.codehaus.plexus.PlexusContainer;
+import org.codehaus.plexus.classworlds.ClassWorld;
+import org.codehaus.plexus.classworlds.realm.ClassRealm;
+import 
org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
+import org.codehaus.plexus.configuration.PlexusConfiguration;
+import org.codehaus.plexus.util.xml.Xpp3Dom;
+import org.eclipse.aether.RepositorySystem;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.ArgumentMatchers.isNull;
+import static org.mockito.Mockito.doAnswer;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+
+class DefaultMojoConfigurationServiceTest extends 
AbstractCoreMavenComponentTestCase {
+
+    @Inject
+    PlexusContainer container;
+
+    @Test
+    void readsConfiguredParameterWithoutInstantiatingMojo() throws Exception {
+        TestMojo.setterCalls = 0;
+
+        org.apache.maven.execution.MavenSession mavenSession = 
createMavenSession(null);
+        DefaultSession session = new DefaultSession(

Review Comment:
   🔵 `createMavenSession(null)` and `new ClassWorld(...)` are repeated in all 
11 test methods. Extract to a `@BeforeEach` field initializer to remove the 
duplication.



##########
impl/maven-core/src/test/java/org/apache/maven/configuration/internal/DefaultMojoConfigurationServiceTest.java:
##########
@@ -0,0 +1,787 @@
+/*
+ * 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.configuration.internal;
+
+import javax.inject.Inject;
+
+import java.io.File;
+import java.net.URI;
+import java.nio.file.Path;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.maven.AbstractCoreMavenComponentTestCase;
+import org.apache.maven.api.MojoExecution;
+import org.apache.maven.api.Project;
+import org.apache.maven.api.plugin.descriptor.Parameter;
+import org.apache.maven.api.services.MojoConfigurationService;
+import org.apache.maven.internal.impl.DefaultLookup;
+import org.apache.maven.internal.impl.DefaultMojoExecution;
+import org.apache.maven.internal.impl.DefaultProject;
+import org.apache.maven.internal.impl.DefaultSession;
+import org.apache.maven.internal.impl.InternalMavenSession;
+import org.apache.maven.internal.xml.XmlPlexusConfiguration;
+import org.apache.maven.plugin.MavenPluginManager;
+import org.apache.maven.plugin.PluginParameterExpressionEvaluatorV4;
+import org.apache.maven.plugin.descriptor.PluginDescriptor;
+import org.apache.maven.plugin.internal.PluginConfigurationInterpolator;
+import org.codehaus.plexus.PlexusContainer;
+import org.codehaus.plexus.classworlds.ClassWorld;
+import org.codehaus.plexus.classworlds.realm.ClassRealm;
+import 
org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
+import org.codehaus.plexus.configuration.PlexusConfiguration;
+import org.codehaus.plexus.util.xml.Xpp3Dom;
+import org.eclipse.aether.RepositorySystem;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.ArgumentMatchers.isNull;
+import static org.mockito.Mockito.doAnswer;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+
+class DefaultMojoConfigurationServiceTest extends 
AbstractCoreMavenComponentTestCase {
+

Review Comment:
   🔵 No negative-path tests: the implementation has three guard clauses 
(non-`DefaultMojoExecution`, non-`DefaultProject`, non-V4 Mojo) that throw 
`IllegalArgumentException`, but none are covered by `assertThrows`. Please add 
a test for each.



##########
impl/maven-core/src/main/java/org/apache/maven/configuration/internal/DefaultMojoConfigurationService.java:
##########
@@ -0,0 +1,186 @@
+/*
+ * 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.configuration.internal;
+
+import javax.inject.Inject;
+import javax.inject.Named;
+
+import java.util.HashSet;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.maven.api.MojoExecution;
+import org.apache.maven.api.Project;
+import org.apache.maven.api.Service;
+import org.apache.maven.api.di.SessionScoped;
+import org.apache.maven.api.plugin.descriptor.Parameter;
+import org.apache.maven.api.services.MojoConfigurationService;
+import org.apache.maven.internal.impl.DefaultMojoExecution;
+import org.apache.maven.internal.impl.DefaultProject;
+import org.apache.maven.internal.impl.InternalMavenSession;
+import org.apache.maven.internal.xml.XmlPlexusConfiguration;
+import org.apache.maven.plugin.MavenPluginManager;
+import org.apache.maven.plugin.PluginContainerException;
+import org.apache.maven.plugin.PluginParameterExpressionEvaluatorV4;
+import org.apache.maven.plugin.PluginResolutionException;
+import org.apache.maven.plugin.descriptor.PluginDescriptor;
+import org.apache.maven.plugin.internal.PluginConfigurationInterpolator;
+import org.codehaus.plexus.classworlds.realm.ClassRealm;
+import 
org.codehaus.plexus.component.configurator.ComponentConfigurationException;
+import 
org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
+import org.codehaus.plexus.configuration.DefaultPlexusConfiguration;
+import org.codehaus.plexus.configuration.PlexusConfiguration;
+import org.eclipse.sisu.Typed;
+
+@Named
+@Typed({MojoConfigurationService.class, Service.class})
+@SessionScoped
+public class DefaultMojoConfigurationService implements 
MojoConfigurationService {
+
+    private final InternalMavenSession session;
+    private final MavenPluginManager pluginManager;
+
+    @Inject
+    public DefaultMojoConfigurationService(InternalMavenSession session, 
MavenPluginManager pluginManager) {
+        this.session = session;
+        this.pluginManager = pluginManager;
+    }
+
+    @Override
+    public Map<String, Object> getParameters(Project project, MojoExecution 
mojoExecution) {
+        if (!(mojoExecution instanceof DefaultMojoExecution execution)) {
+            throw new IllegalArgumentException("Unsupported MojoExecution 
implementation");
+        }
+
+        if (!(project instanceof DefaultProject defaultProject)) {
+            throw new IllegalArgumentException("Unsupported Project 
implementation");
+        }
+
+        org.apache.maven.project.MavenProject mavenProject = 
defaultProject.getProject();
+
+        org.apache.maven.plugin.MojoExecution delegate = 
execution.getDelegate();
+        org.apache.maven.plugin.descriptor.MojoDescriptor mojoDescriptor = 
delegate.getMojoDescriptor();
+
+        if (!mojoDescriptor.isV4Api()) {
+            throw new IllegalArgumentException("Only V4 Mojos are currently 
supported");
+        }
+
+        PluginDescriptor pluginDescriptor = 
mojoDescriptor.getPluginDescriptor();
+        org.apache.maven.execution.MavenSession interpolationSession =
+                session.getMavenSession().clone();
+        interpolationSession.setCurrentProject(mavenProject);
+        ClassRealm pluginRealm = pluginDescriptor.getClassRealm();
+
+        if (pluginRealm == null) {
+            try {
+                pluginManager.setupPluginRealm(pluginDescriptor, 
interpolationSession, null, null, null);
+                pluginRealm = pluginDescriptor.getClassRealm();
+            } catch (PluginResolutionException | PluginContainerException e) {
+                throw new IllegalStateException("Could not resolve plugin 
realm", e);
+            }
+        }
+
+        ClassLoader oldClassLoader = 
Thread.currentThread().getContextClassLoader();
+

Review Comment:
   🟡 The TCCL swap works, but it carries an implicit threading restriction. 
Please document the threading contract in Javadoc on `getParameters()` — e.g., 
why the TCCL swap is needed and whether callers must hold any particular thread 
context.



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