You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@maven.apache.org by GitBox <gi...@apache.org> on 2022/04/28 20:14:37 UTC

[GitHub] [maven] slawekjaranowski opened a new pull request, #731: [MNG-7464] Warn about using read-only parameters for Mojo in configuration

slawekjaranowski opened a new pull request, #731:
URL: https://github.com/apache/maven/pull/731

   
   Following this checklist to help us incorporate your
   contribution quickly and easily:
   
    - [x] Make sure there is a [JIRA issue](https://issues.apache.org/jira/browse/MNG) filed
          for the change (usually before you start working on it).  Trivial changes like typos do not
          require a JIRA issue. Your pull request should address just this issue, without
          pulling in other changes.
    - [x] Each commit in the pull request should have a meaningful subject line and body.
    - [x] Format the pull request title like `[MNG-XXX] SUMMARY`, where you replace `MNG-XXX`
          and `SUMMARY` with the appropriate JIRA issue. Best practice is to use the JIRA issue
          title in the pull request title and in the first line of the commit message.
    - [ ] Write a pull request description that is detailed enough to understand what the pull request does, how, and why.
    - [x] Run `mvn clean verify` to make sure basic checks pass. A more thorough check will
          be performed on your pull request automatically.
    - [x] You have run the [Core IT][core-its] successfully.
   
   If your pull request is about ~20 lines of code you don't need to sign an
   [Individual Contributor License Agreement](https://www.apache.org/licenses/icla.pdf) if you are unsure
   please ask on the developers list.
   
   To make clear that you license your contribution under
   the [Apache License Version 2.0, January 2004](http://www.apache.org/licenses/LICENSE-2.0)
   you have to acknowledge this by using the following check-box.
   
    - [x] I hereby declare this contribution to be licenced under the [Apache License Version 2.0, January 2004](http://www.apache.org/licenses/LICENSE-2.0)
   
    - [x] In any other case, please file an [Apache Individual Contributor License Agreement](https://www.apache.org/licenses/icla.pdf).
   
   [core-its]: https://maven.apache.org/core-its/core-it-suite/
   


-- 
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: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [maven] michael-o commented on a diff in pull request #731: [MNG-7464] Warn about using read-only parameters for Mojo in configuration

Posted by GitBox <gi...@apache.org>.
michael-o commented on code in PR #731:
URL: https://github.com/apache/maven/pull/731#discussion_r862331813


##########
maven-core/src/main/java/org/apache/maven/plugin/internal/AbstractMavenPluginParametersValidator.java:
##########
@@ -0,0 +1,153 @@
+package org.apache.maven.plugin.internal;
+
+/*
+ * 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.
+ */
+
+import java.util.Arrays;
+import java.util.List;
+
+import org.apache.maven.plugin.descriptor.Parameter;
+import org.apache.maven.shared.utils.logging.MessageBuilder;
+import org.apache.maven.shared.utils.logging.MessageUtils;
+import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
+import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
+import org.codehaus.plexus.configuration.PlexusConfiguration;
+import org.slf4j.Logger;
+
+/**
+ * Common implementations for plugin parameters configuration validation.
+ *
+ * @author Slawomir Jaranowski
+ */
+abstract class AbstractMavenPluginParametersValidator implements MavenPluginConfigurationValidator
+{
+
+    // plugin author can provide @Parameter( property = "session" ) in this case property will always evaluate
+    // so, we need ignore those
+
+    // source org.apache.maven.plugin.PluginParameterExpressionEvaluator
+    private static final List<String> IGNORED_PROPERTY_VALUES = Arrays.asList(
+        "basedir",
+        "executedProject",
+        "localRepository",
+        "mojo",
+        "mojoExecution",
+        "plugin",
+        "project",
+        "reactorProjects",
+        "session",
+        "settings"
+    );
+
+    private static final List<String> IGNORED_PROPERTY_PREFIX = Arrays.asList(
+        "mojo.",
+        "plugin.",
+        "pom.",

Review Comment:
   https://issues.apache.org/jira/browse/MNG-7244
   



-- 
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: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [maven] michael-o commented on a diff in pull request #731: [MNG-7464] Warn about using read-only parameters for Mojo in configuration

Posted by GitBox <gi...@apache.org>.
michael-o commented on code in PR #731:
URL: https://github.com/apache/maven/pull/731#discussion_r862331679


##########
maven-core/src/main/java/org/apache/maven/plugin/internal/AbstractMavenPluginParametersValidator.java:
##########
@@ -0,0 +1,153 @@
+package org.apache.maven.plugin.internal;
+
+/*
+ * 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.
+ */
+
+import java.util.Arrays;
+import java.util.List;
+
+import org.apache.maven.plugin.descriptor.Parameter;
+import org.apache.maven.shared.utils.logging.MessageBuilder;
+import org.apache.maven.shared.utils.logging.MessageUtils;
+import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
+import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
+import org.codehaus.plexus.configuration.PlexusConfiguration;
+import org.slf4j.Logger;
+
+/**
+ * Common implementations for plugin parameters configuration validation.
+ *
+ * @author Slawomir Jaranowski
+ */
+abstract class AbstractMavenPluginParametersValidator implements MavenPluginConfigurationValidator
+{
+
+    // plugin author can provide @Parameter( property = "session" ) in this case property will always evaluate
+    // so, we need ignore those
+
+    // source org.apache.maven.plugin.PluginParameterExpressionEvaluator
+    private static final List<String> IGNORED_PROPERTY_VALUES = Arrays.asList(
+        "basedir",
+        "executedProject",
+        "localRepository",
+        "mojo",
+        "mojoExecution",
+        "plugin",
+        "project",
+        "reactorProjects",
+        "session",
+        "settings"
+    );
+
+    private static final List<String> IGNORED_PROPERTY_PREFIX = Arrays.asList(
+        "mojo.",
+        "plugin.",
+        "pom.",

Review Comment:
   Beat me, but I am certain that we have worked on it because `pom.` is deprecated.



-- 
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: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [maven] michael-o commented on a diff in pull request #731: [MNG-7464] Warn about using read-only parameters for Mojo in configuration

Posted by GitBox <gi...@apache.org>.
michael-o commented on code in PR #731:
URL: https://github.com/apache/maven/pull/731#discussion_r863687256


##########
maven-core/src/main/java/org/apache/maven/plugin/internal/AbstractMavenPluginParametersValidator.java:
##########
@@ -0,0 +1,152 @@
+package org.apache.maven.plugin.internal;
+
+/*
+ * 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.
+ */
+
+import java.util.Arrays;
+import java.util.List;
+
+import org.apache.maven.plugin.descriptor.Parameter;
+import org.apache.maven.shared.utils.logging.MessageBuilder;
+import org.apache.maven.shared.utils.logging.MessageUtils;
+import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
+import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
+import org.codehaus.plexus.configuration.PlexusConfiguration;
+import org.slf4j.Logger;
+
+/**
+ * Common implementations for plugin parameters configuration validation.
+ *
+ * @author Slawomir Jaranowski
+ */
+abstract class AbstractMavenPluginParametersValidator implements MavenPluginConfigurationValidator
+{
+
+    // plugin author can provide @Parameter( property = "session" ) in this case property will always evaluate
+    // so, we need ignore those
+
+    // source org.apache.maven.plugin.PluginParameterExpressionEvaluator
+    private static final List<String> IGNORED_PROPERTY_VALUES = Arrays.asList(
+        "basedir",
+        "executedProject",
+        "localRepository",
+        "mojo",
+        "mojoExecution",
+        "plugin",
+        "project",
+        "reactorProjects",
+        "session",
+        "settings"
+    );
+
+    private static final List<String> IGNORED_PROPERTY_PREFIX = Arrays.asList(
+        "mojo.",
+        "plugin.",
+        "project.",
+        "session.",
+        "settings."
+    );
+
+    protected abstract Logger getLogger();
+
+    protected static boolean isValueSet( PlexusConfiguration config,
+                                         ExpressionEvaluator expressionEvaluator )
+    {
+        if ( config == null )
+        {
+            return false;
+        }
+
+        // there are sub items ... so configuration is declared
+        if ( config.getChildCount() > 0 )
+        {
+            return true;
+        }
+
+        String strValue = config.getValue();
+
+        if ( strValue == null || strValue.isEmpty() )
+        {
+            return false;
+        }
+
+        if ( isIgnoredProperty( strValue ) )
+        {
+            return false;
+        }
+
+        // for declaration like @Parameter( property = "config.property" )
+        // the value will contains ${config.property}

Review Comment:
   will contain



##########
maven-core/src/main/java/org/apache/maven/plugin/internal/ReadOnlyPluginParametersValidator.java:
##########
@@ -0,0 +1,80 @@
+package org.apache.maven.plugin.internal;
+
+/*
+ * 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.
+ */
+
+import javax.inject.Named;
+import javax.inject.Singleton;
+
+import org.apache.maven.plugin.descriptor.MojoDescriptor;
+import org.apache.maven.plugin.descriptor.Parameter;
+import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
+import org.codehaus.plexus.configuration.PlexusConfiguration;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Print warnings if read-only parameters of plugin are used in configuration.
+ *
+ * @author Slawomir Jaranowski
+ */
+@Named
+@Singleton
+public class ReadOnlyPluginParametersValidator extends AbstractMavenPluginParametersValidator
+{
+    private static final Logger LOGGER = LoggerFactory.getLogger( ReadOnlyPluginParametersValidator.class );
+
+    @Override
+    protected Logger getLogger()
+    {
+        return LOGGER;
+    }
+
+    @Override
+    protected String getParameterLogReason( Parameter parameter )
+    {
+        return "is read-only, should not be used in configuration";

Review Comment:
   Are you going the change the message?



##########
maven-core/src/main/java/org/apache/maven/plugin/internal/ReadOnlyPluginParametersValidator.java:
##########
@@ -0,0 +1,80 @@
+package org.apache.maven.plugin.internal;
+
+/*
+ * 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.
+ */
+
+import javax.inject.Named;
+import javax.inject.Singleton;
+
+import org.apache.maven.plugin.descriptor.MojoDescriptor;
+import org.apache.maven.plugin.descriptor.Parameter;
+import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
+import org.codehaus.plexus.configuration.PlexusConfiguration;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Print warnings if read-only parameters of plugin are used in configuration.

Review Comment:
   "of a plugin"



-- 
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: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [maven] slawekjaranowski merged pull request #731: [MNG-7464] Warn about using read-only parameters for Mojo in configuration

Posted by GitBox <gi...@apache.org>.
slawekjaranowski merged PR #731:
URL: https://github.com/apache/maven/pull/731


-- 
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: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [maven] michael-o commented on a diff in pull request #731: [MNG-7464] Warn about using read-only parameters for Mojo in configuration

Posted by GitBox <gi...@apache.org>.
michael-o commented on code in PR #731:
URL: https://github.com/apache/maven/pull/731#discussion_r862331778


##########
maven-core/src/main/java/org/apache/maven/plugin/internal/AbstractMavenPluginParametersValidator.java:
##########
@@ -0,0 +1,153 @@
+package org.apache.maven.plugin.internal;
+
+/*
+ * 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.
+ */
+
+import java.util.Arrays;
+import java.util.List;
+
+import org.apache.maven.plugin.descriptor.Parameter;
+import org.apache.maven.shared.utils.logging.MessageBuilder;
+import org.apache.maven.shared.utils.logging.MessageUtils;
+import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
+import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
+import org.codehaus.plexus.configuration.PlexusConfiguration;
+import org.slf4j.Logger;
+
+/**
+ * Common implementations for plugin parameters configuration validation.
+ *
+ * @author Slawomir Jaranowski
+ */
+abstract class AbstractMavenPluginParametersValidator implements MavenPluginConfigurationValidator
+{
+
+    // plugin author can provide @Parameter( property = "session" ) in this case property will always evaluate
+    // so, we need ignore those
+
+    // source org.apache.maven.plugin.PluginParameterExpressionEvaluator
+    private static final List<String> IGNORED_PROPERTY_VALUES = Arrays.asList(
+        "basedir",
+        "executedProject",
+        "localRepository",
+        "mojo",
+        "mojoExecution",
+        "plugin",
+        "project",
+        "reactorProjects",
+        "session",
+        "settings"
+    );
+
+    private static final List<String> IGNORED_PROPERTY_PREFIX = Arrays.asList(
+        "mojo.",
+        "plugin.",
+        "pom.",

Review Comment:
   b2a21f12f8a0598dd4a286177e340bda0148e9ba



-- 
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: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [maven] michael-o commented on a diff in pull request #731: [MNG-7464] Warn about using read-only parameters for Mojo in configuration

Posted by GitBox <gi...@apache.org>.
michael-o commented on code in PR #731:
URL: https://github.com/apache/maven/pull/731#discussion_r863688136


##########
maven-core/src/main/java/org/apache/maven/plugin/internal/ReadOnlyPluginParametersValidator.java:
##########
@@ -0,0 +1,80 @@
+package org.apache.maven.plugin.internal;
+
+/*
+ * 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.
+ */
+
+import javax.inject.Named;
+import javax.inject.Singleton;
+
+import org.apache.maven.plugin.descriptor.MojoDescriptor;
+import org.apache.maven.plugin.descriptor.Parameter;
+import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
+import org.codehaus.plexus.configuration.PlexusConfiguration;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Print warnings if read-only parameters of plugin are used in configuration.
+ *
+ * @author Slawomir Jaranowski
+ */
+@Named
+@Singleton
+public class ReadOnlyPluginParametersValidator extends AbstractMavenPluginParametersValidator
+{
+    private static final Logger LOGGER = LoggerFactory.getLogger( ReadOnlyPluginParametersValidator.class );
+
+    @Override
+    protected Logger getLogger()
+    {
+        return LOGGER;
+    }
+
+    @Override
+    protected String getParameterLogReason( Parameter parameter )
+    {
+        return "is read-only, should not be used in configuration";

Review Comment:
   Are you going to change this message?



-- 
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: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [maven] slawekjaranowski commented on a diff in pull request #731: [MNG-7464] Warn about using read-only parameters for Mojo in configuration

Posted by GitBox <gi...@apache.org>.
slawekjaranowski commented on code in PR #731:
URL: https://github.com/apache/maven/pull/731#discussion_r862735488


##########
maven-core/src/main/java/org/apache/maven/plugin/internal/ReadOnlyPluginParametersValidator.java:
##########
@@ -0,0 +1,80 @@
+package org.apache.maven.plugin.internal;
+
+/*
+ * 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.
+ */
+
+import javax.inject.Named;
+import javax.inject.Singleton;
+
+import org.apache.maven.plugin.descriptor.MojoDescriptor;
+import org.apache.maven.plugin.descriptor.Parameter;
+import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
+import org.codehaus.plexus.configuration.PlexusConfiguration;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Print warnings if read-only parameters of plugin are used in configuration.
+ *
+ * @author Slawomir Jaranowski
+ */
+@Named
+@Singleton
+public class ReadOnlyPluginParametersValidator extends AbstractMavenPluginParametersValidator
+{
+    private static final Logger LOGGER = LoggerFactory.getLogger( ReadOnlyPluginParametersValidator.class );
+
+    @Override
+    protected Logger getLogger()
+    {
+        return LOGGER;
+    }
+
+    @Override
+    protected String getParameterLogReason( Parameter parameter )
+    {
+        return "is read-only, should not be used in configuration";

Review Comment:
   unfortunately such settings have effect and parameters are changed according to user settings in configuration.
   
   so `SHALL NOT` or even `MUST NOT` probably will be better.



-- 
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: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [maven] slawekjaranowski commented on a diff in pull request #731: [MNG-7464] Warn about using read-only parameters for Mojo in configuration

Posted by GitBox <gi...@apache.org>.
slawekjaranowski commented on code in PR #731:
URL: https://github.com/apache/maven/pull/731#discussion_r862331108


##########
maven-core/src/main/java/org/apache/maven/plugin/internal/AbstractMavenPluginParametersValidator.java:
##########
@@ -0,0 +1,153 @@
+package org.apache.maven.plugin.internal;
+
+/*
+ * 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.
+ */
+
+import java.util.Arrays;
+import java.util.List;
+
+import org.apache.maven.plugin.descriptor.Parameter;
+import org.apache.maven.shared.utils.logging.MessageBuilder;
+import org.apache.maven.shared.utils.logging.MessageUtils;
+import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
+import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
+import org.codehaus.plexus.configuration.PlexusConfiguration;
+import org.slf4j.Logger;
+
+/**
+ * Common implementations for plugin parameters configuration validation.
+ *
+ * @author Slawomir Jaranowski
+ */
+abstract class AbstractMavenPluginParametersValidator implements MavenPluginConfigurationValidator
+{
+
+    // plugin author can provide @Parameter( property = "session" ) in this case property will always evaluate
+    // so, we need ignore those
+
+    // source org.apache.maven.plugin.PluginParameterExpressionEvaluator
+    private static final List<String> IGNORED_PROPERTY_VALUES = Arrays.asList(
+        "basedir",
+        "executedProject",
+        "localRepository",
+        "mojo",
+        "mojoExecution",
+        "plugin",
+        "project",
+        "reactorProjects",
+        "session",
+        "settings"
+    );
+
+    private static final List<String> IGNORED_PROPERTY_PREFIX = Arrays.asList(
+        "mojo.",
+        "plugin.",
+        "pom.",

Review Comment:
   I see one about prefixless https://issues.apache.org/jira/browse/MNG-7404



-- 
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: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [maven] slawekjaranowski commented on a diff in pull request #731: [MNG-7464] Warn about using read-only parameters for Mojo in configuration

Posted by GitBox <gi...@apache.org>.
slawekjaranowski commented on code in PR #731:
URL: https://github.com/apache/maven/pull/731#discussion_r862333656


##########
maven-core/src/main/java/org/apache/maven/plugin/internal/AbstractMavenPluginParametersValidator.java:
##########
@@ -0,0 +1,153 @@
+package org.apache.maven.plugin.internal;
+
+/*
+ * 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.
+ */
+
+import java.util.Arrays;
+import java.util.List;
+
+import org.apache.maven.plugin.descriptor.Parameter;
+import org.apache.maven.shared.utils.logging.MessageBuilder;
+import org.apache.maven.shared.utils.logging.MessageUtils;
+import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
+import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
+import org.codehaus.plexus.configuration.PlexusConfiguration;
+import org.slf4j.Logger;
+
+/**
+ * Common implementations for plugin parameters configuration validation.
+ *
+ * @author Slawomir Jaranowski
+ */
+abstract class AbstractMavenPluginParametersValidator implements MavenPluginConfigurationValidator
+{
+
+    // plugin author can provide @Parameter( property = "session" ) in this case property will always evaluate
+    // so, we need ignore those
+
+    // source org.apache.maven.plugin.PluginParameterExpressionEvaluator
+    private static final List<String> IGNORED_PROPERTY_VALUES = Arrays.asList(
+        "basedir",
+        "executedProject",
+        "localRepository",
+        "mojo",
+        "mojoExecution",
+        "plugin",
+        "project",
+        "reactorProjects",
+        "session",
+        "settings"
+    );
+
+    private static final List<String> IGNORED_PROPERTY_PREFIX = Arrays.asList(
+        "mojo.",
+        "plugin.",
+        "pom.",

Review Comment:
   ok, I will remove `pom` for 4.0.x ... but restore it in 3.9.x 



-- 
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: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [maven] michael-o commented on a diff in pull request #731: [MNG-7464] Warn about using read-only parameters for Mojo in configuration

Posted by GitBox <gi...@apache.org>.
michael-o commented on code in PR #731:
URL: https://github.com/apache/maven/pull/731#discussion_r862515080


##########
maven-core/src/main/java/org/apache/maven/plugin/internal/ReadOnlyPluginParametersValidator.java:
##########
@@ -0,0 +1,80 @@
+package org.apache.maven.plugin.internal;
+
+/*
+ * 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.
+ */
+
+import javax.inject.Named;
+import javax.inject.Singleton;
+
+import org.apache.maven.plugin.descriptor.MojoDescriptor;
+import org.apache.maven.plugin.descriptor.Parameter;
+import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
+import org.codehaus.plexus.configuration.PlexusConfiguration;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Print warnings if read-only parameters of plugin are used in configuration.
+ *
+ * @author Slawomir Jaranowski
+ */
+@Named
+@Singleton
+public class ReadOnlyPluginParametersValidator extends AbstractMavenPluginParametersValidator
+{
+    private static final Logger LOGGER = LoggerFactory.getLogger( ReadOnlyPluginParametersValidator.class );
+
+    @Override
+    protected Logger getLogger()
+    {
+        return LOGGER;
+    }
+
+    @Override
+    protected String getParameterLogReason( Parameter parameter )
+    {
+        return "is read-only, should not be used in configuration";

Review Comment:
   Look at https://datatracker.ietf.org/doc/html/rfc2119 and should not, it gives me the feeling that the log message is wrong. As far as I understand: Setting those has no effect, but should not does not tell, no? If so, I would expect "shall not".



-- 
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: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [maven] michael-o commented on a diff in pull request #731: [MNG-7464] Warn about using read-only parameters for Mojo in configuration

Posted by GitBox <gi...@apache.org>.
michael-o commented on code in PR #731:
URL: https://github.com/apache/maven/pull/731#discussion_r862330706


##########
maven-core/src/main/java/org/apache/maven/plugin/internal/AbstractMavenPluginParametersValidator.java:
##########
@@ -0,0 +1,153 @@
+package org.apache.maven.plugin.internal;
+
+/*
+ * 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.
+ */
+
+import java.util.Arrays;
+import java.util.List;
+
+import org.apache.maven.plugin.descriptor.Parameter;
+import org.apache.maven.shared.utils.logging.MessageBuilder;
+import org.apache.maven.shared.utils.logging.MessageUtils;
+import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
+import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
+import org.codehaus.plexus.configuration.PlexusConfiguration;
+import org.slf4j.Logger;
+
+/**
+ * Common implementations for plugin parameters configuration validation.
+ *
+ * @author Slawomir Jaranowski
+ */
+abstract class AbstractMavenPluginParametersValidator implements MavenPluginConfigurationValidator
+{
+
+    // plugin author can provide @Parameter( property = "session" ) in this case property will always evaluate
+    // so, we need ignore those
+
+    // source org.apache.maven.plugin.PluginParameterExpressionEvaluator
+    private static final List<String> IGNORED_PROPERTY_VALUES = Arrays.asList(
+        "basedir",
+        "executedProject",
+        "localRepository",
+        "mojo",
+        "mojoExecution",
+        "plugin",
+        "project",
+        "reactorProjects",
+        "session",
+        "settings"
+    );
+
+    private static final List<String> IGNORED_PROPERTY_PREFIX = Arrays.asList(
+        "mojo.",
+        "plugin.",
+        "pom.",
+        "project.",
+        "session.",
+        "settings."
+    );
+
+    protected abstract Logger getLogger();
+
+    protected static boolean isValueSet( PlexusConfiguration config,
+                                         ExpressionEvaluator expressionEvaluator )
+    {
+        if ( config == null )
+        {
+            return false;
+        }
+
+        // there are sub items ... so configuration is declared
+        if ( config.getChildCount() > 0 )
+        {
+            return true;
+        }
+
+        String strValue = config.getValue();
+
+        if ( strValue == null || strValue.isEmpty() )
+        {
+            return false;
+        }
+
+        if ( isIgnoredProperty( strValue ) )
+        {
+            return false;
+        }
+
+        // for declaration like @Parameter( property = "config.property" )
+        // the value will contains ${config.property}
+
+        try
+        {
+            return expressionEvaluator.evaluate( strValue ) != null;
+        }
+        catch ( ExpressionEvaluationException e )
+        {
+            // not important
+            // will be reported during Mojo fields populate
+        }
+
+        // fallback - in case of error in expressionEvaluator
+        return false;
+    }
+
+    private static boolean isIgnoredProperty( String strValue )
+    {
+        if ( !strValue.startsWith( "${" ) )
+        {
+            return false;
+        }
+
+        String propertyName = strValue.replace( "${", "" ).replace( "}", "" );
+
+        if ( IGNORED_PROPERTY_VALUES.contains( propertyName ) )
+        {
+            return true;
+        }
+
+        return IGNORED_PROPERTY_PREFIX.stream().anyMatch( propertyName::startsWith );

Review Comment:
   No, nope. I just find the expression extremely cool.



-- 
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: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [maven] slawekjaranowski commented on a diff in pull request #731: [MNG-7464] Warn about using read-only parameters for Mojo in configuration

Posted by GitBox <gi...@apache.org>.
slawekjaranowski commented on code in PR #731:
URL: https://github.com/apache/maven/pull/731#discussion_r864561100


##########
maven-core/src/main/java/org/apache/maven/plugin/internal/ReadOnlyPluginParametersValidator.java:
##########
@@ -0,0 +1,80 @@
+package org.apache.maven.plugin.internal;
+
+/*
+ * 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.
+ */
+
+import javax.inject.Named;
+import javax.inject.Singleton;
+
+import org.apache.maven.plugin.descriptor.MojoDescriptor;
+import org.apache.maven.plugin.descriptor.Parameter;
+import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
+import org.codehaus.plexus.configuration.PlexusConfiguration;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Print warnings if read-only parameters of plugin are used in configuration.
+ *
+ * @author Slawomir Jaranowski
+ */
+@Named
+@Singleton
+public class ReadOnlyPluginParametersValidator extends AbstractMavenPluginParametersValidator
+{
+    private static final Logger LOGGER = LoggerFactory.getLogger( ReadOnlyPluginParametersValidator.class );
+
+    @Override
+    protected Logger getLogger()
+    {
+        return LOGGER;
+    }
+
+    @Override
+    protected String getParameterLogReason( Parameter parameter )
+    {
+        return "is read-only, should not be used in configuration";

Review Comment:
   Yes.
   according to RFC:
   
   > MUST NOT   This phrase, or the phrase "SHALL NOT", mean that the
      definition is an absolute prohibition of the specification.
   
   I will choose  `MUST NOT` - I hope it will be the most accurate



-- 
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: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [maven] michael-o commented on a diff in pull request #731: [MNG-7464] Warn about using read-only parameters for Mojo in configuration

Posted by GitBox <gi...@apache.org>.
michael-o commented on code in PR #731:
URL: https://github.com/apache/maven/pull/731#discussion_r862325468


##########
maven-core/src/main/java/org/apache/maven/plugin/internal/AbstractMavenPluginParametersValidator.java:
##########
@@ -0,0 +1,153 @@
+package org.apache.maven.plugin.internal;
+
+/*
+ * 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.
+ */
+
+import java.util.Arrays;
+import java.util.List;
+
+import org.apache.maven.plugin.descriptor.Parameter;
+import org.apache.maven.shared.utils.logging.MessageBuilder;
+import org.apache.maven.shared.utils.logging.MessageUtils;
+import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
+import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
+import org.codehaus.plexus.configuration.PlexusConfiguration;
+import org.slf4j.Logger;
+
+/**
+ * Common implementations for plugin parameters configuration validation.
+ *
+ * @author Slawomir Jaranowski
+ */
+abstract class AbstractMavenPluginParametersValidator implements MavenPluginConfigurationValidator
+{
+
+    // plugin author can provide @Parameter( property = "session" ) in this case property will always evaluate
+    // so, we need ignore those
+
+    // source org.apache.maven.plugin.PluginParameterExpressionEvaluator
+    private static final List<String> IGNORED_PROPERTY_VALUES = Arrays.asList(
+        "basedir",
+        "executedProject",
+        "localRepository",
+        "mojo",
+        "mojoExecution",
+        "plugin",
+        "project",
+        "reactorProjects",
+        "session",
+        "settings"
+    );
+
+    private static final List<String> IGNORED_PROPERTY_PREFIX = Arrays.asList(
+        "mojo.",
+        "plugin.",
+        "pom.",

Review Comment:
   The `pom` prefix has already been removed, no? Why ignore?



##########
maven-core/src/main/java/org/apache/maven/plugin/internal/DefaultMavenPluginManager.java:
##########
@@ -606,7 +606,10 @@ else if ( cause instanceof LinkageError )
 
             ExpressionEvaluator expressionEvaluator = new PluginParameterExpressionEvaluator( session, mojoExecution );
 
-            configurationValidator.validate( mojoDescriptor, pomConfiguration, expressionEvaluator );
+            for ( MavenPluginConfigurationValidator validator: configurationValidator )

Review Comment:
   configurationValidators



##########
maven-core/src/main/java/org/apache/maven/plugin/internal/DefaultMavenPluginManager.java:
##########
@@ -143,7 +143,7 @@
     private PluginVersionResolver pluginVersionResolver;
     private PluginArtifactsCache pluginArtifactsCache;
     private MavenPluginValidator pluginValidator;
-    private MavenPluginConfigurationValidator configurationValidator;
+    private List<MavenPluginConfigurationValidator> configurationValidator;

Review Comment:
   configurationValidators



##########
maven-core/src/main/java/org/apache/maven/plugin/internal/AbstractMavenPluginParametersValidator.java:
##########
@@ -0,0 +1,153 @@
+package org.apache.maven.plugin.internal;
+
+/*
+ * 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.
+ */
+
+import java.util.Arrays;
+import java.util.List;
+
+import org.apache.maven.plugin.descriptor.Parameter;
+import org.apache.maven.shared.utils.logging.MessageBuilder;
+import org.apache.maven.shared.utils.logging.MessageUtils;
+import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
+import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
+import org.codehaus.plexus.configuration.PlexusConfiguration;
+import org.slf4j.Logger;
+
+/**
+ * Common implementations for plugin parameters configuration validation.
+ *
+ * @author Slawomir Jaranowski
+ */
+abstract class AbstractMavenPluginParametersValidator implements MavenPluginConfigurationValidator
+{
+
+    // plugin author can provide @Parameter( property = "session" ) in this case property will always evaluate
+    // so, we need ignore those
+
+    // source org.apache.maven.plugin.PluginParameterExpressionEvaluator
+    private static final List<String> IGNORED_PROPERTY_VALUES = Arrays.asList(
+        "basedir",
+        "executedProject",
+        "localRepository",
+        "mojo",
+        "mojoExecution",
+        "plugin",
+        "project",
+        "reactorProjects",
+        "session",
+        "settings"
+    );
+
+    private static final List<String> IGNORED_PROPERTY_PREFIX = Arrays.asList(
+        "mojo.",
+        "plugin.",
+        "pom.",
+        "project.",
+        "session.",
+        "settings."
+    );
+
+    protected abstract Logger getLogger();
+
+    protected static boolean isValueSet( PlexusConfiguration config,
+                                         ExpressionEvaluator expressionEvaluator )
+    {
+        if ( config == null )
+        {
+            return false;
+        }
+
+        // there are sub items ... so configuration is declared
+        if ( config.getChildCount() > 0 )
+        {
+            return true;
+        }
+
+        String strValue = config.getValue();
+
+        if ( strValue == null || strValue.isEmpty() )
+        {
+            return false;
+        }
+
+        if ( isIgnoredProperty( strValue ) )
+        {
+            return false;
+        }
+
+        // for declaration like @Parameter( property = "config.property" )
+        // the value will contains ${config.property}
+
+        try
+        {
+            return expressionEvaluator.evaluate( strValue ) != null;
+        }
+        catch ( ExpressionEvaluationException e )
+        {
+            // not important
+            // will be reported during Mojo fields populate
+        }
+
+        // fallback - in case of error in expressionEvaluator
+        return false;
+    }
+
+    private static boolean isIgnoredProperty( String strValue )
+    {
+        if ( !strValue.startsWith( "${" ) )
+        {
+            return false;
+        }
+
+        String propertyName = strValue.replace( "${", "" ).replace( "}", "" );
+
+        if ( IGNORED_PROPERTY_VALUES.contains( propertyName ) )
+        {
+            return true;
+        }
+
+        return IGNORED_PROPERTY_PREFIX.stream().anyMatch( propertyName::startsWith );

Review Comment:
   Wow, this is sick :-D



##########
maven-core/src/main/java/org/apache/maven/plugin/internal/DefaultMavenPluginManager.java:
##########
@@ -160,7 +160,7 @@ public DefaultMavenPluginManager(
             PluginVersionResolver pluginVersionResolver,
             PluginArtifactsCache pluginArtifactsCache,
             MavenPluginValidator pluginValidator,
-            MavenPluginConfigurationValidator configurationValidator )
+            List<MavenPluginConfigurationValidator> configurationValidator )

Review Comment:
   configurationValidators



-- 
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: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [maven] slawekjaranowski commented on a diff in pull request #731: [MNG-7464] Warn about using read-only parameters for Mojo in configuration

Posted by GitBox <gi...@apache.org>.
slawekjaranowski commented on code in PR #731:
URL: https://github.com/apache/maven/pull/731#discussion_r862330410


##########
maven-core/src/main/java/org/apache/maven/plugin/internal/AbstractMavenPluginParametersValidator.java:
##########
@@ -0,0 +1,153 @@
+package org.apache.maven.plugin.internal;
+
+/*
+ * 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.
+ */
+
+import java.util.Arrays;
+import java.util.List;
+
+import org.apache.maven.plugin.descriptor.Parameter;
+import org.apache.maven.shared.utils.logging.MessageBuilder;
+import org.apache.maven.shared.utils.logging.MessageUtils;
+import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
+import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
+import org.codehaus.plexus.configuration.PlexusConfiguration;
+import org.slf4j.Logger;
+
+/**
+ * Common implementations for plugin parameters configuration validation.
+ *
+ * @author Slawomir Jaranowski
+ */
+abstract class AbstractMavenPluginParametersValidator implements MavenPluginConfigurationValidator
+{
+
+    // plugin author can provide @Parameter( property = "session" ) in this case property will always evaluate
+    // so, we need ignore those
+
+    // source org.apache.maven.plugin.PluginParameterExpressionEvaluator
+    private static final List<String> IGNORED_PROPERTY_VALUES = Arrays.asList(
+        "basedir",
+        "executedProject",
+        "localRepository",
+        "mojo",
+        "mojoExecution",
+        "plugin",
+        "project",
+        "reactorProjects",
+        "session",
+        "settings"
+    );
+
+    private static final List<String> IGNORED_PROPERTY_PREFIX = Arrays.asList(
+        "mojo.",
+        "plugin.",
+        "pom.",

Review Comment:
   still exist in `PluginParameterExpressionEvaluator`
   https://github.com/apache/maven/blob/master/maven-core/src/main/java/org/apache/maven/plugin/PluginParameterExpressionEvaluator.java#L245
   
   I can remove it here if there is plan to remove such prefix from maven ... IHMO should be done together with change in `PluginParameterExpressionEvaluator`



##########
maven-core/src/main/java/org/apache/maven/plugin/internal/AbstractMavenPluginParametersValidator.java:
##########
@@ -0,0 +1,153 @@
+package org.apache.maven.plugin.internal;
+
+/*
+ * 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.
+ */
+
+import java.util.Arrays;
+import java.util.List;
+
+import org.apache.maven.plugin.descriptor.Parameter;
+import org.apache.maven.shared.utils.logging.MessageBuilder;
+import org.apache.maven.shared.utils.logging.MessageUtils;
+import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
+import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
+import org.codehaus.plexus.configuration.PlexusConfiguration;
+import org.slf4j.Logger;
+
+/**
+ * Common implementations for plugin parameters configuration validation.
+ *
+ * @author Slawomir Jaranowski
+ */
+abstract class AbstractMavenPluginParametersValidator implements MavenPluginConfigurationValidator
+{
+
+    // plugin author can provide @Parameter( property = "session" ) in this case property will always evaluate
+    // so, we need ignore those
+
+    // source org.apache.maven.plugin.PluginParameterExpressionEvaluator
+    private static final List<String> IGNORED_PROPERTY_VALUES = Arrays.asList(
+        "basedir",
+        "executedProject",
+        "localRepository",
+        "mojo",
+        "mojoExecution",
+        "plugin",
+        "project",
+        "reactorProjects",
+        "session",
+        "settings"
+    );
+
+    private static final List<String> IGNORED_PROPERTY_PREFIX = Arrays.asList(
+        "mojo.",
+        "plugin.",
+        "pom.",
+        "project.",
+        "session.",
+        "settings."
+    );
+
+    protected abstract Logger getLogger();
+
+    protected static boolean isValueSet( PlexusConfiguration config,
+                                         ExpressionEvaluator expressionEvaluator )
+    {
+        if ( config == null )
+        {
+            return false;
+        }
+
+        // there are sub items ... so configuration is declared
+        if ( config.getChildCount() > 0 )
+        {
+            return true;
+        }
+
+        String strValue = config.getValue();
+
+        if ( strValue == null || strValue.isEmpty() )
+        {
+            return false;
+        }
+
+        if ( isIgnoredProperty( strValue ) )
+        {
+            return false;
+        }
+
+        // for declaration like @Parameter( property = "config.property" )
+        // the value will contains ${config.property}
+
+        try
+        {
+            return expressionEvaluator.evaluate( strValue ) != null;
+        }
+        catch ( ExpressionEvaluationException e )
+        {
+            // not important
+            // will be reported during Mojo fields populate
+        }
+
+        // fallback - in case of error in expressionEvaluator
+        return false;
+    }
+
+    private static boolean isIgnoredProperty( String strValue )
+    {
+        if ( !strValue.startsWith( "${" ) )
+        {
+            return false;
+        }
+
+        String propertyName = strValue.replace( "${", "" ).replace( "}", "" );
+
+        if ( IGNORED_PROPERTY_VALUES.contains( propertyName ) )
+        {
+            return true;
+        }
+
+        return IGNORED_PROPERTY_PREFIX.stream().anyMatch( propertyName::startsWith );

Review Comment:
   lambda way ... what is your proposition instead of ...



-- 
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: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [maven] michael-o commented on a diff in pull request #731: [MNG-7464] Warn about using read-only parameters for Mojo in configuration

Posted by GitBox <gi...@apache.org>.
michael-o commented on code in PR #731:
URL: https://github.com/apache/maven/pull/731#discussion_r862330673


##########
maven-core/src/main/java/org/apache/maven/plugin/internal/AbstractMavenPluginParametersValidator.java:
##########
@@ -0,0 +1,153 @@
+package org.apache.maven.plugin.internal;
+
+/*
+ * 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.
+ */
+
+import java.util.Arrays;
+import java.util.List;
+
+import org.apache.maven.plugin.descriptor.Parameter;
+import org.apache.maven.shared.utils.logging.MessageBuilder;
+import org.apache.maven.shared.utils.logging.MessageUtils;
+import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
+import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
+import org.codehaus.plexus.configuration.PlexusConfiguration;
+import org.slf4j.Logger;
+
+/**
+ * Common implementations for plugin parameters configuration validation.
+ *
+ * @author Slawomir Jaranowski
+ */
+abstract class AbstractMavenPluginParametersValidator implements MavenPluginConfigurationValidator
+{
+
+    // plugin author can provide @Parameter( property = "session" ) in this case property will always evaluate
+    // so, we need ignore those
+
+    // source org.apache.maven.plugin.PluginParameterExpressionEvaluator
+    private static final List<String> IGNORED_PROPERTY_VALUES = Arrays.asList(
+        "basedir",
+        "executedProject",
+        "localRepository",
+        "mojo",
+        "mojoExecution",
+        "plugin",
+        "project",
+        "reactorProjects",
+        "session",
+        "settings"
+    );
+
+    private static final List<String> IGNORED_PROPERTY_PREFIX = Arrays.asList(
+        "mojo.",
+        "plugin.",
+        "pom.",

Review Comment:
   Add a note with the JIRA issue where it has been removed in Maven 4. @mthmulders Did remove it.



-- 
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: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org