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 2014/07/26 23:50:22 UTC

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

Author: hboutemy
Date: Sat Jul 26 21:50:22 2014
New Revision: 1613731

URL: http://svn.apache.org/r1613731
Log:
fixed errors reported by Checkstyle

Modified:
    maven/plugins/trunk/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/AbstractCheckstyleReport.java
    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/RuleUtil.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/AbstractCheckstyleReport.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/AbstractCheckstyleReport.java?rev=1613731&r1=1613730&r2=1613731&view=diff
==============================================================================
--- maven/plugins/trunk/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/AbstractCheckstyleReport.java (original)
+++ maven/plugins/trunk/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/AbstractCheckstyleReport.java Sat Jul 26 21:50:22 2014
@@ -37,6 +37,7 @@ import org.apache.maven.doxia.siterender
 import org.apache.maven.doxia.tools.SiteTool;
 import org.apache.maven.model.Dependency;
 import org.apache.maven.model.Plugin;
+import org.apache.maven.model.PluginManagement;
 import org.apache.maven.model.ReportPlugin;
 import org.apache.maven.model.Resource;
 import org.apache.maven.plugin.MojoExecution;
@@ -543,10 +544,10 @@ public abstract class AbstractCheckstyle
         
         List<Artifact> artifacts = new ArrayList<Artifact>();
 
-        if ( project.getBuild().getPluginManagement() != null )
+        PluginManagement pluginManagement = project.getBuild().getPluginManagement();
+        if ( pluginManagement != null )
         {
-            artifacts.addAll( getCheckstylePluginDependenciesAsArtifacts( project.getBuild().getPluginManagement().getPluginsAsMap(),
-                                                                          hint ) );
+            artifacts.addAll( getCheckstylePluginDependenciesAsArtifacts( pluginManagement.getPluginsAsMap(), hint ) );
         }
 
         artifacts.addAll( getCheckstylePluginDependenciesAsArtifacts( project.getBuild().getPluginsAsMap(), hint ) );

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=1613731&r1=1613730&r2=1613731&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 Jul 26 21:50:22 2014
@@ -34,6 +34,7 @@ import java.util.Map;
 import org.apache.maven.artifact.Artifact;
 import org.apache.maven.model.Dependency;
 import org.apache.maven.model.Plugin;
+import org.apache.maven.model.PluginManagement;
 import org.apache.maven.model.Resource;
 import org.apache.maven.plugin.AbstractMojo;
 import org.apache.maven.plugin.MojoExecution;
@@ -51,6 +52,7 @@ import org.apache.maven.plugins.annotati
 import org.apache.maven.project.MavenProject;
 import org.codehaus.plexus.configuration.PlexusConfiguration;
 import org.codehaus.plexus.util.FileUtils;
+import org.codehaus.plexus.util.IOUtil;
 import org.codehaus.plexus.util.ReaderFactory;
 import org.codehaus.plexus.util.StringUtils;
 import org.codehaus.plexus.util.xml.pull.MXParser;
@@ -443,124 +445,129 @@ public class CheckstyleViolationCheckMoj
     public void execute()
         throws MojoExecutionException, MojoFailureException
     {
-
-        if ( !skip )
+        if ( skip )
         {
+            return;
+        }
 
-            if ( !skipExec )
+        if ( !skipExec )
+        {
+            if ( checkstyleRules != null )
             {
-
-                if ( checkstyleRules != null )
+                if ( !"config/sun_checks.xml".equals( configLocation ) )
                 {
-                    if ( ! "config/sun_checks.xml".equals( configLocation ) )
-                    {
-                        throw new MojoExecutionException( "If you use inline configuration for rules don't specify a configLocation" );
-                    }
-                    if ( checkstyleRules.getChildCount() > 1 )
-                    {
-                        throw new MojoExecutionException( "Currently only one root module is supported" );
-                    }
-                    PlexusConfiguration checkerModule = checkstyleRules.getChild( 0 );
-
-                    try
-                    {
-                        FileUtils.forceMkdir( rulesFiles.getParentFile() );
-                        FileUtils.fileWrite( rulesFiles, CHECKSTYLE_FILE_HEADER + checkerModule.toString() );
-                    }
-                    catch ( final IOException e )
-                    {
-                        throw new MojoExecutionException( e.getMessage(), e );
-                    }
-                    configLocation = rulesFiles.getAbsolutePath();
+                    throw new MojoExecutionException( "If you use inline configuration for rules, don't specify "
+                        + "a configLocation" );
+                }
+                if ( checkstyleRules.getChildCount() > 1 )
+                {
+                    throw new MojoExecutionException( "Currently only one root module is supported" );
                 }
 
-                ClassLoader currentClassLoader = Thread.currentThread().getContextClassLoader();
+                PlexusConfiguration checkerModule = checkstyleRules.getChild( 0 );
 
                 try
                 {
-                    CheckstyleExecutorRequest request = new CheckstyleExecutorRequest();
-                    request.setConsoleListener( getConsoleListener() ).setConsoleOutput( consoleOutput )
-                        .setExcludes( excludes ).setFailsOnError( failsOnError ).setIncludes( includes )
-                        .setResourceIncludes( resourceIncludes )
-                        .setResourceExcludes( resourceExcludes )
-                        .setIncludeResources( includeResources )
-                        .setIncludeTestResources( includeTestResources )
-                        .setIncludeTestSourceDirectory( includeTestSourceDirectory ).setListener( getListener() )
-                        .setLog( getLog() ).setProject( project ).setSourceDirectory( sourceDirectory )
-                        .setResources( resources )
-                        .setStringOutputStream( stringOutputStream ).setSuppressionsLocation( suppressionsLocation )
-                        .setTestSourceDirectory( testSourceDirectory ).setConfigLocation( configLocation )
-                        .setConfigurationArtifacts( collectArtifacts( "config" ) )
-                        .setPropertyExpansion( propertyExpansion )
-                        .setHeaderLocation( headerLocation ).setLicenseArtifacts( collectArtifacts( "license" ) )
-                        .setCacheFile( cacheFile ).setSuppressionsFileExpression( suppressionsFileExpression )
-                        .setEncoding( encoding ).setPropertiesLocation( propertiesLocation );
-                    checkstyleExecutor.executeCheckstyle( request );
-
-                }
-                catch ( CheckstyleException e )
-                {
-                    throw new MojoExecutionException( "Failed during checkstyle configuration", e );
-                }
-                catch ( CheckstyleExecutorException e )
-                {
-                    throw new MojoExecutionException( "Failed during checkstyle execution", e );
+                    FileUtils.forceMkdir( rulesFiles.getParentFile() );
+                    FileUtils.fileWrite( rulesFiles, CHECKSTYLE_FILE_HEADER + checkerModule.toString() );
                 }
-                finally
+                catch ( final IOException e )
                 {
-                    //be sure to restore original context classloader
-                    Thread.currentThread().setContextClassLoader( currentClassLoader );
+                    throw new MojoExecutionException( e.getMessage(), e );
                 }
+                configLocation = rulesFiles.getAbsolutePath();
+            }
+
+            ClassLoader currentClassLoader = Thread.currentThread().getContextClassLoader();
+
+            try
+            {
+                CheckstyleExecutorRequest request = new CheckstyleExecutorRequest();
+                request.setConsoleListener( getConsoleListener() ).setConsoleOutput( consoleOutput )
+                    .setExcludes( excludes ).setFailsOnError( failsOnError ).setIncludes( includes )
+                    .setResourceIncludes( resourceIncludes )
+                    .setResourceExcludes( resourceExcludes )
+                    .setIncludeResources( includeResources )
+                    .setIncludeTestResources( includeTestResources )
+                    .setIncludeTestSourceDirectory( includeTestSourceDirectory ).setListener( getListener() )
+                    .setLog( getLog() ).setProject( project ).setSourceDirectory( sourceDirectory )
+                    .setResources( resources )
+                    .setStringOutputStream( stringOutputStream ).setSuppressionsLocation( suppressionsLocation )
+                    .setTestSourceDirectory( testSourceDirectory ).setConfigLocation( configLocation )
+                    .setConfigurationArtifacts( collectArtifacts( "config" ) )
+                    .setPropertyExpansion( propertyExpansion )
+                    .setHeaderLocation( headerLocation ).setLicenseArtifacts( collectArtifacts( "license" ) )
+                    .setCacheFile( cacheFile ).setSuppressionsFileExpression( suppressionsFileExpression )
+                    .setEncoding( encoding ).setPropertiesLocation( propertiesLocation );
+                checkstyleExecutor.executeCheckstyle( request );
 
             }
-            if ( !"xml".equals( outputFileFormat ) )
+            catch ( CheckstyleException e )
             {
-                throw new MojoExecutionException( "Output format is '" + outputFileFormat
-                    + "', checkstyle:check requires format to be 'xml'." );
+                throw new MojoExecutionException( "Failed during checkstyle configuration", e );
             }
-
-            if ( !outputFile.exists() )
+            catch ( CheckstyleExecutorException e )
             {
-                getLog().info(
-                               "Unable to perform checkstyle:check, "
-                                   + "unable to find checkstyle:checkstyle outputFile." );
-                return;
+                throw new MojoExecutionException( "Failed during checkstyle execution", e );
             }
-
-            try
+            finally
             {
-                XmlPullParser xpp = new MXParser();
-                Reader freader = ReaderFactory.newXmlReader( outputFile );
-                BufferedReader breader = new BufferedReader( freader );
-                xpp.setInput( breader );
+                //be sure to restore original context classloader
+                Thread.currentThread().setContextClassLoader( currentClassLoader );
+            }
+        }
 
-                int violations = countViolations( xpp );
-                if ( violations > maxAllowedViolations )
+        if ( !"xml".equals( outputFileFormat ) )
+        {
+            throw new MojoExecutionException( "Output format is '" + outputFileFormat
+                + "', checkstyle:check requires format to be 'xml'." );
+        }
+
+        if ( !outputFile.exists() )
+        {
+            getLog().info( "Unable to perform checkstyle:check, unable to find checkstyle:checkstyle outputFile." );
+            return;
+        }
+
+        Reader reader = null;
+        try
+        {
+            reader = new BufferedReader( ReaderFactory.newXmlReader( outputFile ) );
+
+            XmlPullParser xpp = new MXParser();
+            xpp.setInput( reader );
+
+            int violations = countViolations( xpp );
+
+            if ( violations > maxAllowedViolations )
+            {
+                if ( failOnViolation )
                 {
-                    if ( failOnViolation )
+                    String msg =
+                        "You have " + violations + " Checkstyle violation" + ( ( violations > 1 ) ? "s" : "" ) + ".";
+                    if ( maxAllowedViolations > 0 )
                     {
-                        String msg = "You have " + violations + " Checkstyle violation"
-                            + ( ( violations > 1 ) ? "s" : "" ) + ".";
-                        if ( maxAllowedViolations > 0 )
-                        {
-                            msg += " The maximum number of allowed violations is " + maxAllowedViolations + ".";
-                        }
-                        throw new MojoFailureException( msg );
+                        msg += " The maximum number of allowed violations is " + maxAllowedViolations + ".";
                     }
-
-                    getLog().warn( "checkstyle:check violations detected but failOnViolation set to false" );
+                    throw new MojoFailureException( msg );
                 }
+
+                getLog().warn( "checkstyle:check violations detected but failOnViolation set to false" );
             }
-            catch ( IOException e )
-            {
-                throw new MojoExecutionException( "Unable to read Checkstyle results xml: "
-                    + outputFile.getAbsolutePath(), e );
-            }
-            catch ( XmlPullParserException e )
-            {
-                throw new MojoExecutionException( "Unable to read Checkstyle results xml: "
-                    + outputFile.getAbsolutePath(), e );
-            }
+        }
+        catch ( IOException e )
+        {
+            throw new MojoExecutionException( "Unable to read Checkstyle results xml: " + outputFile.getAbsolutePath(),
+                                              e );
+        }
+        catch ( XmlPullParserException e )
+        {
+            throw new MojoExecutionException( "Unable to read Checkstyle results xml: " + outputFile.getAbsolutePath(),
+                                              e );
+        }
+        finally
+        {
+            IOUtil.close( reader );
         }
     }
 
@@ -575,41 +582,42 @@ public class CheckstyleViolationCheckMoj
         {
             if ( eventType == XmlPullParser.START_TAG )
             {
-                String severity;
-
                 if ( "file".equals( xpp.getName() ) )
                 {
                     file = xpp.getAttributeValue( "", "name" );
                     file = file.substring( file.lastIndexOf( File.separatorChar ) + 1 );
                 }
-                else if ( "error".equals( xpp.getName() )
-                    && isViolation( severity = xpp.getAttributeValue( "", "severity" ) ) )
+                else if ( "error".equals( xpp.getName() ) )
                 {
-                    count++;
-
-                    if ( logViolationsToConsole )
+                    String severity = xpp.getAttributeValue( "", "severity" );
+                    if ( isViolation( severity ) )
                     {
-                        String line = xpp.getAttributeValue( "", "line" );
-                        String column = xpp.getAttributeValue( "", "column" );
-                        String message = xpp.getAttributeValue( "", "message" );
-                        String source = xpp.getAttributeValue( "", "source" );
-                        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
+                        count++;
+
+                        if ( logViolationsToConsole )
                         {
-                            getLog().error( logMessage );
+                            String line = xpp.getAttributeValue( "", "line" );
+                            String column = xpp.getAttributeValue( "", "column" );
+                            String message = xpp.getAttributeValue( "", "message" );
+                            String source = xpp.getAttributeValue( "", "source" );
+                            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 );
+                            }
                         }
                     }
                 }
@@ -728,10 +736,10 @@ public class CheckstyleViolationCheckMoj
         
         List<Artifact> artifacts = new ArrayList<Artifact>();
 
-        if ( project.getBuild().getPluginManagement() != null )
+        PluginManagement pluginManagement = project.getBuild().getPluginManagement();
+        if ( pluginManagement != null )
         {
-            artifacts.addAll( getCheckstylePluginDependenciesAsArtifacts( project.getBuild().getPluginManagement().getPluginsAsMap(),
-                                                                          hint ) );
+            artifacts.addAll( getCheckstylePluginDependenciesAsArtifacts( pluginManagement.getPluginsAsMap(), hint ) );
         }
 
         artifacts.addAll( getCheckstylePluginDependenciesAsArtifacts( project.getBuild().getPluginsAsMap(), hint ) );

Modified: maven/plugins/trunk/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/RuleUtil.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/RuleUtil.java?rev=1613731&r1=1613730&r2=1613731&view=diff
==============================================================================
--- maven/plugins/trunk/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/RuleUtil.java (original)
+++ maven/plugins/trunk/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/RuleUtil.java Sat Jul 26 21:50:22 2014
@@ -1,7 +1,5 @@
 package org.apache.maven.plugin.checkstyle;
 
-import com.puppycrawl.tools.checkstyle.api.AuditEvent;
-
 /*
  * Licensed to the Apache Software Foundation (ASF) under one
  * or more contributor license agreements.  See the NOTICE file
@@ -21,6 +19,8 @@ import com.puppycrawl.tools.checkstyle.a
  * under the License.
  */
 
+import com.puppycrawl.tools.checkstyle.api.AuditEvent;
+
 /**
  * Tooling for Checkstyle rules conventions: names, categories.
  *

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=1613731&r1=1613730&r2=1613731&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 Jul 26 21:50:22 2014
@@ -115,8 +115,7 @@ public class DefaultCheckstyleExecutor
 
         Checker checker = new Checker();
 
-        // setup classloader, needed to avoid "Unable to get class information
-        // for ..." errors
+        // setup classloader, needed to avoid "Unable to get class information for ..." errors
         List<String> classPathStrings = new ArrayList<String>();
         List<String> outputDirectories = new ArrayList<String>();
         File sourceDirectory = request.getSourceDirectory();
@@ -209,7 +208,8 @@ public class DefaultCheckstyleExecutor
         }
         else
         {
-            addSourceDirectory( checkerListener, sourceDirectory, testSourceDirectory, request.getResources(), request );
+            addSourceDirectory( checkerListener, sourceDirectory, testSourceDirectory, request.getResources(),
+                                request );
         }
 
         checker.addListener( checkerListener );
@@ -623,7 +623,6 @@ public class DefaultCheckstyleExecutor
     private FilterSet getSuppressionsFilterSet( final String suppressionsFilePath )
         throws CheckstyleExecutorException
     {
-
         if ( suppressionsFilePath == null )
         {
             return null;