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 2018/06/17 16:46:21 UTC

[GitHub] khmarbaise closed pull request #31: MENFORCER-142 Initial poc implementation of adding rules from cmdline (WIP)

khmarbaise closed pull request #31: MENFORCER-142 Initial poc implementation of adding rules from cmdline (WIP)
URL: https://github.com/apache/maven-enforcer/pull/31
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/maven-enforcer-plugin/src/main/java/org/apache/maven/plugins/enforcer/EnforceMojo.java b/maven-enforcer-plugin/src/main/java/org/apache/maven/plugins/enforcer/EnforceMojo.java
index 2048b8d..3d623fc 100644
--- a/maven-enforcer-plugin/src/main/java/org/apache/maven/plugins/enforcer/EnforceMojo.java
+++ b/maven-enforcer-plugin/src/main/java/org/apache/maven/plugins/enforcer/EnforceMojo.java
@@ -101,9 +101,15 @@
     /**
      * Array of objects that implement the EnforcerRule interface to execute.
      */
-    @Parameter( required = true )
+    @Parameter( required = false )
     private EnforcerRule[] rules;
 
+    /**
+     * Array of Strings that matches the EnforcerRules to execute.
+     */
+    @Parameter( required = false, property = "rules" )
+    private String[] commandLineRules;
+
     /**
      * Use this flag to disable rule result caching. This will cause all rules to execute on each project even if the
      * rule indicates it can safely be cached.
@@ -115,6 +121,7 @@
     // plugin's container in 2.0.x
     protected PlexusContainer container;
 
+    @Override
     public void contextualize( Context context )
         throws ContextException
     {
@@ -131,6 +138,7 @@ private boolean havingRules()
      * 
      * @throws MojoExecutionException
      */
+    @Override
     public void execute()
         throws MojoExecutionException
     {
@@ -138,7 +146,11 @@ public void execute()
 
         EnforcerExpressionEvaluator evaluator =
             new EnforcerExpressionEvaluator( session, mojoExecution );
-
+        if ( commandLineRules != null && commandLineRules.length > 0 )
+        {
+            this.rules = createRulesFromCommandLineOptions();
+        }
+        
         if ( isSkip() )
         {
             log.info( "Skipping Rule Enforcement." );
@@ -238,6 +250,30 @@ public void execute()
         // CHECKSTYLE_ON: LineLength
     }
 
+    private EnforcerRule[] createRulesFromCommandLineOptions() throws MojoExecutionException 
+    {
+        EnforcerRule[] rules = new EnforcerRule[commandLineRules.length];
+        for ( int i = 0; i < commandLineRules.length; i++ ) 
+        {
+            String rule = commandLineRules[i];
+            if ( !rule.contains( "." ) )
+            {
+                rule = getClass().getPackage().getName() 
+                    + "." + Character.toUpperCase( rule.charAt( 0 ) ) + rule.substring( 1 ); 
+            }
+            
+            try 
+            {
+                rules[i] = ( EnforcerRule ) Class.forName( rule ).newInstance();
+            }
+            catch ( Exception e ) 
+            {
+                throw new MojoExecutionException( "Failed to create enforcer rules from command line argument", e );
+            }
+        }
+        return rules;
+    }
+
     /**
      * This method determines if a rule should execute based on the cache
      *
@@ -254,7 +290,7 @@ protected boolean shouldExecute( EnforcerRule rule )
             if ( EnforceMojo.cache.containsKey( key ) )
             {
                 log.debug( "Key " + key + " was found in the cache" );
-                if ( rule.isResultValid( (EnforcerRule) cache.get( key ) ) )
+                if ( rule.isResultValid( cache.get( key ) ) )
                 {
                     log.debug( "The cached results are still valid. Skipping the rule: " + rule.getClass().getName() );
                     return false;


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services