You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by sl...@apache.org on 2020/03/29 00:46:29 UTC

[maven] branch MNG-5001-t updated: [MNG-5001] Block the option to set Mojo parameters marked as @readonly

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

slachiewicz pushed a commit to branch MNG-5001-t
in repository https://gitbox.apache.org/repos/asf/maven.git


The following commit(s) were added to refs/heads/MNG-5001-t by this push:
     new c0d72a0  [MNG-5001] Block the option to set Mojo parameters marked as @readonly
c0d72a0 is described below

commit c0d72a01e4bf11eed237cd484405cb277b57cc0e
Author: Sylwester Lachiewicz <sl...@apache.org>
AuthorDate: Fri Apr 5 16:46:16 2019 +0200

    [MNG-5001] Block the option to set Mojo parameters marked as @readonly
---
 .../maven/plugin/PluginParameterException.java     |  2 +-
 .../plugin/internal/DefaultMavenPluginManager.java | 35 +++++++++++++++++++++-
 2 files changed, 35 insertions(+), 2 deletions(-)

diff --git a/maven-core/src/main/java/org/apache/maven/plugin/PluginParameterException.java b/maven-core/src/main/java/org/apache/maven/plugin/PluginParameterException.java
index eaf02ec..e69f555 100644
--- a/maven-core/src/main/java/org/apache/maven/plugin/PluginParameterException.java
+++ b/maven-core/src/main/java/org/apache/maven/plugin/PluginParameterException.java
@@ -43,7 +43,7 @@ public class PluginParameterException
     public PluginParameterException( MojoDescriptor mojo, List<Parameter> parameters )
     {
         super( mojo.getPluginDescriptor(), "The parameters " + format( parameters ) + " for goal "
-            + mojo.getRoleHint() + " are missing or invalid" );
+            + mojo.getRoleHint() + " are missing, read-only or invalid" );
 
         this.mojo = mojo;
 
diff --git a/maven-core/src/main/java/org/apache/maven/plugin/internal/DefaultMavenPluginManager.java b/maven-core/src/main/java/org/apache/maven/plugin/internal/DefaultMavenPluginManager.java
index 9febd4a..b427622 100644
--- a/maven-core/src/main/java/org/apache/maven/plugin/internal/DefaultMavenPluginManager.java
+++ b/maven-core/src/main/java/org/apache/maven/plugin/internal/DefaultMavenPluginManager.java
@@ -624,6 +624,8 @@ public class DefaultMavenPluginManager
             logger.debug(
                 "Configuring mojo '" + mojoDescriptor.getId() + "' with " + configuratorId + " configurator -->" );
 
+            validateReadOnlyParameters( mojoDescriptor, configuration );
+
             configurator.configureComponent( mojo, configuration, expressionEvaluator, pluginRealm, validator );
 
             logger.debug( "-- end configuration --" );
@@ -716,7 +718,6 @@ public class DefaultMavenPluginManager
             {
                 continue;
             }
-
             Object value = null;
 
             PlexusConfiguration config = configuration.getChild( parameter.getName(), false );
@@ -739,6 +740,7 @@ public class DefaultMavenPluginManager
                         + configuration.getName() + "'";
                     throw new ComponentConfigurationException( configuration, msg, e );
                 }
+
             }
 
             if ( value == null && ( config == null || config.getChildCount() <= 0 ) )
@@ -753,6 +755,37 @@ public class DefaultMavenPluginManager
         }
     }
 
+    private void validateReadOnlyParameters( MojoDescriptor mojoDescriptor, PlexusConfiguration configuration )
+            throws PluginParameterException
+    {
+        if ( mojoDescriptor.getParameters() == null )
+        {
+            return;
+        }
+
+        List<Parameter> invalidParameters = new ArrayList<>();
+
+        for ( Parameter parameter : mojoDescriptor.getParameters() )
+        {
+            PlexusConfiguration config = configuration.getChild( parameter.getName(), false );
+            if ( config != null && !parameter.isEditable() )
+            {
+                String value = config.getValue( null );
+                String defaultValue = config.getAttribute( "default-value", null );
+                if ( value != null && defaultValue != null && !Objects.equals( defaultValue, value ) )
+                {
+                    invalidParameters.add( parameter );
+                    logger.error( "Setting read-only parameter '" + parameter.getName() + "' to " + value );
+                }
+            }
+        }
+
+        if ( !invalidParameters.isEmpty() )
+        {
+            throw new PluginParameterException( mojoDescriptor, invalidParameters );
+        }
+    }
+
     public void releaseMojo( Object mojo, MojoExecution mojoExecution )
     {
         if ( mojo != null )