[ 
https://issues.apache.org/jira/browse/MENFORCER-142?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16515133#comment-16515133
 ] 

ASF GitHub Bot commented on MENFORCER-142:
------------------------------------------

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:
us...@infra.apache.org


> Specify enforcer rule in command line without modifying any pom
> ---------------------------------------------------------------
>
>                 Key: MENFORCER-142
>                 URL: https://issues.apache.org/jira/browse/MENFORCER-142
>             Project: Maven Enforcer Plugin
>          Issue Type: Improvement
>          Components: Plugin
>            Reporter: Arnaud Bourrée
>            Assignee: Karl Heinz Marbaise
>            Priority: Major
>             Fix For: 3.0.0
>
>
> Hello,
> How could we specify enforcer:enforce rules from command line?
> I want to run command line like following without updating any pom.xml:
> mvn enforcer:enforce -Drules=com.acme.UseAcmeParentPom
> The goal of this enforcer:enforce rule is to check that Acme's
> developers write pom.xml which inherit from acme's parent pom.xml
> And because they may not inherit from acme's parent pom.xml, I cannot
> specify enforcer rule in.
> Regards,
> Arnaud.



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to