You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by kh...@apache.org on 2015/11/14 13:41:46 UTC

svn commit: r1714309 - /maven/enforcer/trunk/maven-enforcer-plugin/src/main/java/org/apache/maven/plugins/enforcer/EnforceMojo.java

Author: khmarbaise
Date: Sat Nov 14 12:41:46 2015
New Revision: 1714309

URL: http://svn.apache.org/viewvc?rev=1714309&view=rev
Log:
Refactoring code.

Modified:
    maven/enforcer/trunk/maven-enforcer-plugin/src/main/java/org/apache/maven/plugins/enforcer/EnforceMojo.java

Modified: maven/enforcer/trunk/maven-enforcer-plugin/src/main/java/org/apache/maven/plugins/enforcer/EnforceMojo.java
URL: http://svn.apache.org/viewvc/maven/enforcer/trunk/maven-enforcer-plugin/src/main/java/org/apache/maven/plugins/enforcer/EnforceMojo.java?rev=1714309&r1=1714308&r2=1714309&view=diff
==============================================================================
--- maven/enforcer/trunk/maven-enforcer-plugin/src/main/java/org/apache/maven/plugins/enforcer/EnforceMojo.java (original)
+++ maven/enforcer/trunk/maven-enforcer-plugin/src/main/java/org/apache/maven/plugins/enforcer/EnforceMojo.java Sat Nov 14 12:41:46 2015
@@ -52,12 +52,7 @@ import org.codehaus.plexus.personality.p
  * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
  * @version $Id$
  */
-@Mojo( 
-    name = "enforce", 
-    defaultPhase = LifecyclePhase.VALIDATE, 
-    requiresDependencyCollection = ResolutionScope.TEST, 
-    threadSafe = true 
-)
+@Mojo( name = "enforce", defaultPhase = LifecyclePhase.VALIDATE, requiresDependencyCollection = ResolutionScope.TEST, threadSafe = true )
 public class EnforceMojo
     extends AbstractMojo
     implements Contextualizable
@@ -132,6 +127,11 @@ public class EnforceMojo
         container = (PlexusContainer) context.get( PlexusConstants.PLEXUS_KEY );
     }
 
+    private boolean havingRules()
+    {
+        return rules != null && rules.length > 0;
+    }
+
     /**
      * Entry point to the mojo
      * 
@@ -142,116 +142,106 @@ public class EnforceMojo
     {
         Log log = this.getLog();
 
-        EnforcerExpressionEvaluator evaluator = new EnforcerExpressionEvaluator( session, translator, project, 
-                                                                                 mojoExecution );
+        EnforcerExpressionEvaluator evaluator =
+            new EnforcerExpressionEvaluator( session, translator, project, mojoExecution );
 
-        // the entire execution can be easily skipped
-        if ( !skip )
+        if ( isSkip() )
         {
-            // list to store exceptions
-            List<String> list = new ArrayList<String>();
+            log.info( "Skipping Rule Enforcement." );
+            return;
+        }
 
-            // make sure the rules exist
-            if ( rules != null && rules.length > 0 )
-            {
-                String currentRule = "Unknown";
+        if ( !havingRules() )
+        {
+            // CHECKSTYLE_OFF: LineLength
+            throw new MojoExecutionException( "No rules are configured. Use the skip flag if you want to disable execution." );
+            // CHECKSTYLE_ON: LineLength
+        }
 
-                // create my helper
-                EnforcerRuleHelper helper = new DefaultEnforcementRuleHelper( session, evaluator, log, container );
+        // list to store exceptions
+        List<String> list = new ArrayList<String>();
 
-                // if we are only warning, then disable
-                // failFast
-                if ( !fail )
-                {
-                    failFast = false;
-                }
+        String currentRule = "Unknown";
 
-                boolean hasErrors = false;
+        // create my helper
+        EnforcerRuleHelper helper = new DefaultEnforcementRuleHelper( session, evaluator, log, container );
 
-                // go through each rule
-                for ( int i = 0; i < rules.length; i++ )
-                {
+        // if we are only warning, then disable
+        // failFast
+        if ( !fail )
+        {
+            failFast = false;
+        }
 
-                    // prevent against empty rules
-                    EnforcerRule rule = rules[i];
-                    final EnforcerLevel level = getLevel( rule );
-                    if ( rule != null )
+        boolean hasErrors = false;
+
+        // go through each rule
+        for ( int i = 0; i < rules.length; i++ )
+        {
+
+            // prevent against empty rules
+            EnforcerRule rule = rules[i];
+            final EnforcerLevel level = getLevel( rule );
+            if ( rule != null )
+            {
+                // store the current rule for
+                // logging purposes
+                currentRule = rule.getClass().getName();
+                log.debug( "Executing rule: " + currentRule );
+                try
+                {
+                    if ( ignoreCache || shouldExecute( rule ) )
                     {
-                        // store the current rule for
-                        // logging purposes
-                        currentRule = rule.getClass().getName();
-                        log.debug( "Executing rule: " + currentRule );
-                        try
-                        {
-                            if ( ignoreCache || shouldExecute( rule ) )
-                            {
-                                // execute the rule
-                                // noinspection
-                                // SynchronizationOnLocalVariableOrMethodParameter
-                                synchronized ( rule )
-                                {
-                                    rule.execute( helper );
-                                }
-                            }
-                        }
-                        catch ( EnforcerRuleException e )
+                        // execute the rule
+                        // noinspection
+                        // SynchronizationOnLocalVariableOrMethodParameter
+                        synchronized ( rule )
                         {
-                            // i can throw an exception
-                            // because failfast will be
-                            // false if fail is false.
-                            if ( failFast && level == EnforcerLevel.ERROR )
-                            {
-                                throw new MojoExecutionException( currentRule + " failed with message:\n"
-                                    + e.getMessage(), e );
-                            }
-                            else
-                            {
-                                if ( level == EnforcerLevel.ERROR )
-                                {
-                                    hasErrors = true;
-                                    list.add( "Rule " + i + ": " + currentRule + " failed with message:\n"
-                                        + e.getMessage() );
-                                    log.debug( "Adding failure due to exception", e );
-                                }
-                                else
-                                {
-                                    list.add( "Rule " + i + ": " + currentRule + " warned with message:\n"
-                                        + e.getMessage() );
-                                    log.debug( "Adding warning due to exception", e );
-                                }
-                            }
+                            rule.execute( helper );
                         }
                     }
                 }
-
-                // if we found anything
-                // CHECKSTYLE_OFF: LineLength
-                if ( !list.isEmpty() )
+                catch ( EnforcerRuleException e )
                 {
-                    for ( String failure : list )
+                    // i can throw an exception
+                    // because failfast will be
+                    // false if fail is false.
+                    if ( failFast && level == EnforcerLevel.ERROR )
                     {
-                        log.warn( failure );
+                        throw new MojoExecutionException( currentRule + " failed with message:\n" + e.getMessage(), e );
                     }
-                    if ( fail && hasErrors )
+                    else
                     {
-                        throw new MojoExecutionException(
-                                                          "Some Enforcer rules have failed. Look above for specific messages explaining why the rule failed." );
+                        if ( level == EnforcerLevel.ERROR )
+                        {
+                            hasErrors = true;
+                            list.add( "Rule " + i + ": " + currentRule + " failed with message:\n" + e.getMessage() );
+                            log.debug( "Adding failure due to exception", e );
+                        }
+                        else
+                        {
+                            list.add( "Rule " + i + ": " + currentRule + " warned with message:\n" + e.getMessage() );
+                            log.debug( "Adding warning due to exception", e );
+                        }
                     }
                 }
-                // CHECKSTYLE_ON: LineLength
-            }
-            else
-            {
-                // CHECKSTYLE_OFF: LineLength
-                throw new MojoExecutionException(
-                                                  "No rules are configured. Use the skip flag if you want to disable execution." );
-                // CHECKSTYLE_ON: LineLength
             }
         }
-        else
+
+        // if we found anything
+        // CHECKSTYLE_OFF: LineLength
+        if ( !list.isEmpty() )
         {
-            log.info( "Skipping Rule Enforcement." );
+            for ( String failure : list )
+            {
+                log.warn( failure );
+            }
+            if ( fail && hasErrors )
+            {
+                throw new MojoExecutionException( "Some Enforcer rules have failed. Look above for specific messages explaining why the rule failed." );
+            }
         }
+        // CHECKSTYLE_ON: LineLength
     }
 
     /**