You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by hb...@apache.org on 2015/01/11 00:01:09 UTC

svn commit: r1650824 - in /maven/plugins/trunk/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle: CheckstyleViolationCheckMojo.java exec/DefaultCheckstyleExecutor.java

Author: hboutemy
Date: Sat Jan 10 23:01:09 2015
New Revision: 1650824

URL: http://svn.apache.org/r1650824
Log:
[MCHECKSTYLE-255] improved info message on errors: print count of ignored

Modified:
    maven/plugins/trunk/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/CheckstyleViolationCheckMojo.java
    maven/plugins/trunk/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/exec/DefaultCheckstyleExecutor.java

Modified: maven/plugins/trunk/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/CheckstyleViolationCheckMojo.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/CheckstyleViolationCheckMojo.java?rev=1650824&r1=1650823&r2=1650824&view=diff
==============================================================================
--- maven/plugins/trunk/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/CheckstyleViolationCheckMojo.java (original)
+++ maven/plugins/trunk/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/CheckstyleViolationCheckMojo.java Sat Jan 10 23:01:09 2015
@@ -605,62 +605,81 @@ public class CheckstyleViolationCheckMoj
         throws XmlPullParserException, IOException
     {
         int count = 0;
+        int ignoreCount = 0;
         RuleUtil.Matcher[] ignores =
             ( violationIgnore == null ) ? null : RuleUtil.parseMatchers( violationIgnore.split( "," ) );
 
-        int eventType = xpp.getEventType();
         String file = "";
-        while ( eventType != XmlPullParser.END_DOCUMENT )
+        for ( int eventType = xpp.getEventType(); eventType != XmlPullParser.END_DOCUMENT; eventType = xpp.next() )
         {
-            if ( eventType == XmlPullParser.START_TAG )
+            if ( eventType != XmlPullParser.START_TAG )
             {
-                if ( "file".equals( xpp.getName() ) )
+                continue;
+            }
+            else if ( "file".equals( xpp.getName() ) )
+            {
+                file = xpp.getAttributeValue( "", "name" );
+                file = file.substring( file.lastIndexOf( File.separatorChar ) + 1 );
+            }
+            else if ( "error".equals( xpp.getName() ) )
+            {
+                String severity = xpp.getAttributeValue( "", "severity" );
+
+                if ( !isViolation( severity ) )
                 {
-                    file = xpp.getAttributeValue( "", "name" );
-                    file = file.substring( file.lastIndexOf( File.separatorChar ) + 1 );
+                    continue;
                 }
-                else if ( "error".equals( xpp.getName() ) )
+
+                String source = xpp.getAttributeValue( "", "source" );
+
+                if ( ignore( ignores, source ) )
+                {
+                    ignoreCount++;
+                }
+                else
                 {
-                    String severity = xpp.getAttributeValue( "", "severity" );
-                    String source = xpp.getAttributeValue( "", "source" );
+                    count++;
 
-                    if ( isViolation( severity ) && !ignore( ignores, source ) )
+                    if ( logViolationsToConsole )
                     {
-                        count++;
+                        String line = xpp.getAttributeValue( "", "line" );
+                        String column = xpp.getAttributeValue( "", "column" );
+                        String message = xpp.getAttributeValue( "", "message" );
+                        String rule = RuleUtil.getName( source );
+                        String category = RuleUtil.getCategory( source );
 
-                        if ( logViolationsToConsole )
-                        {
-                            String line = xpp.getAttributeValue( "", "line" );
-                            String column = xpp.getAttributeValue( "", "column" );
-                            String message = xpp.getAttributeValue( "", "message" );
-                            String rule = RuleUtil.getName( source );
-                            String category = RuleUtil.getCategory( source );
-
-                            String logMessage =
-                                file + '[' + line + ( ( column == null ) ? "" : ( ':' + column ) ) + "] (" + category
-                                    + ") " + rule + ": " + message;
-                            if ( "info".equals( severity ) )
-                            {
-                                getLog().info( logMessage );
-                            }
-                            else if ( "warning".equals( severity ) )
-                            {
-                                getLog().warn( logMessage );
-                            }
-                            else
-                            {
-                                getLog().error( logMessage );
-                            }
-                        }
+                        log( severity, file + '[' + line + ( ( column == null ) ? "" : ( ':' + column ) ) + "] ("
+                            + category + ") " + rule + ": " + message );
                     }
                 }
             }
-            eventType = xpp.next();
+        }
+
+        if ( ignoreCount > 0 )
+        {
+            getLog().info( "Ignored " + ignoreCount + " error" + ( ( ignoreCount > 1 ) ? "s" : "" ) + ", " + count
+                               + " violation" + ( ( count > 1 ) ? "s" : "" ) + " remaining." );
         }
 
         return count;
     }
 
+    private void log( String severity, String message )
+    {
+        if ( "info".equals( severity ) )
+        {
+            getLog().info( message );
+        }
+        else if ( "warning".equals( severity ) )
+        {
+            getLog().warn( message );
+        }
+        else
+        {
+            getLog().error( message );
+        }
+    }
+
     /**
      * Checks if the given severity is considered a violation.
      *

Modified: maven/plugins/trunk/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/exec/DefaultCheckstyleExecutor.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/exec/DefaultCheckstyleExecutor.java?rev=1650824&r1=1650823&r2=1650824&view=diff
==============================================================================
--- maven/plugins/trunk/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/exec/DefaultCheckstyleExecutor.java (original)
+++ maven/plugins/trunk/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/exec/DefaultCheckstyleExecutor.java Sat Jan 10 23:01:09 2015
@@ -80,8 +80,6 @@ public class DefaultCheckstyleExecutor
     @Requirement( hint = "license" )
     private ResourceManager licenseLocator;
 
-    private static final File[] EMPTY_FILE_ARRAY = new File[0];
-
     public CheckstyleResults executeCheckstyle( CheckstyleExecutorRequest request )
         throws CheckstyleExecutorException, CheckstyleException
     {
@@ -272,19 +270,29 @@ public class DefaultCheckstyleExecutor
 
         if ( request.getStringOutputStream() != null )
         {
-            request.getLog().info( request.getStringOutputStream().toString() );
-        }
+            String message = request.getStringOutputStream().toString().trim();
 
-        if ( request.isFailsOnError() && nbErrors > 0 )
-        {
-            // TODO: should be a failure, not an error. Report is not meant to
-            // throw an exception here (so site would
-            // work regardless of config), but should record this information
-            throw new CheckstyleExecutorException( "There are " + nbErrors + " checkstyle errors." );
+            if ( message.length() > 0 )
+            {
+                request.getLog().info( message );
+            }
         }
-        else if ( nbErrors > 0 )
+
+        if ( nbErrors > 0 )
         {
-            request.getLog().info( "There are " + nbErrors + " checkstyle errors." );
+            String message = "There are " + nbErrors + " checkstyle errors.";
+
+            if ( request.isFailsOnError() )
+            {
+                // TODO: should be a failure, not an error. Report is not meant to
+                // throw an exception here (so site would
+                // work regardless of config), but should record this information
+                throw new CheckstyleExecutorException( message );
+            }
+            else
+            {
+                request.getLog().info( message );
+            }
         }
 
         return checkerListener.getResults();