How to add suppression filter to checkstyle config file

This blog is a continuation of 2 previous blogs:
Checkstyle Configuration file explained
and
Maven checkstyle plugin with checkstyle configuration file.

So on this blog will continue to work on the same project.

  1. First of all, create a checkstyle error which will be filtered out later. In order to create the checkstyle error, create a new class called A.java in package com.wordpress.happilyblogging.
  2. Replace the content of A.java class with the code below:
  3. /*
     * 
     ====================================================================================================================
     *  Name        : A.java
     *  Part of     : code quality project.
     *  Description : 
     * 
     *  Copyright (c) 2013 xxx Corporation. All rights reserved.
     * 
     *  This material, including documentation and any related computer programs, is protected by copyright controlled by
     *  xxx Corporation. All rights are reserved. Copying, including reproducing, storing, adapting or translating, any
     *  or all of this material requires the prior written consent of xxx Corporation. This material also contains
     *  confidential information which may not be disclosed to others without the prior written consent of
     *  xxx Corporation.
     * 
     ====================================================================================================================
     */
    package com.wordpress.happilyblogging;
    
    /**
     *
     * @author greenhorn
     */
    public class A {
    
    }
    /*
     * 
     ====================================================================================================================
     *  Name        : A.java
     *  Part of     : code quality project.
     *  Description : 
     * 
     *  Copyright (c) 2013 xxx Corporation. All rights reserved.
     * 
     *  This material, including documentation and any related computer programs, is protected by copyright controlled by
     *  xxx Corporation. All rights are reserved. Copying, including reproducing, storing, adapting or translating, any
     *  or all of this material requires the prior written consent of xxx Corporation. This material also contains
     *  confidential information which may not be disclosed to others without the prior written consent of
     *  xxx Corporation.
     * 
     ====================================================================================================================
     */
  4. Now try building the project with “mvn clean install checkstyle:checkstyle”. To find out if the expected checkstyle error was created.
  5. Open generated file: ../good_qualiy_project/target/site/checkstyle.html. The below screenshot should be a portion of the generated checkstyle.html
  6. Figure 1.0

    Figure 1.0

  7. From Figure 1.0, 1 as shown explains that there are 17 lines of duplicate codes in A.java. StrictDuplicateCodeCheck checkstyle rule found that the first 17 lines of A.java is being repeated again in the same file.
  8. We are about to change the rule configuration. The below filter file(Figure 2.0) is created for this purpose. The configuration states that this rule will ignore line 1 to 17 of the directed file. So the rule only applies to line 18 till to the last line on the file.
  9. <?xml version="1.0"?>
    <!DOCTYPE suppressions PUBLIC
        "-//Puppy Crawl//DTD Suppressions 1.1//EN"
        "http://www.puppycrawl.com/dtds/suppressions_1_1.dtd">
    
    <suppressions>
      <!--ignore line 1 to 18 from duplicate checking-->
      <suppress checks="StrictDuplicateCodeCheck"
              files="java$"
              lines="1-17"/>
    </suppressions>

    Figure 2.0

  10. Copy the content of Figure 2.0, create a file called “checkstyle-suppressions.xml” in ../good_qualiy_project/src/main/resources folder.
  11. Make modification to maven-checkstyle-plugin configuration in pom.xml file to recognize the new filter file. After the changes, the maven-checkstyle-plugin configuration will look something similar to Figure 3.0.(Higlighted in yellow are the added new changes)
  12. <plugin>
            <!--To support checkstyle goals. For example: "mvn checkstyle:checkstyle"-->
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-checkstyle-plugin</artifactId>
            <configuration>	
              <suppressionsLocation>src/main/resources/pcc-checkstyle-suppressions.xml</suppressionsLocation>
              <suppressionsFileExpression>checkstyle.suppressions.file</suppressionsFileExpression>
              <configLocation>src/main/resources/greenhorn-checkstyle.xml</configLocation>
            </configuration>
    </plugin>

    Figure 3.0

  13. Add the below(Figure 4.0) to the existing greenhorn-checkstyle.xml file.
  14. <module name="SuppressionFilter">
        <property name="file" value="${checkstyle.suppressions.file}"/>
    </module>

    Figure 4.0

  15. After adding content of Figure 4.0. The greenhorn-checkstyle.xml will look similar to the below Figure 5.0. New changes are highlighted in yellow.
  16. <?xml version="1.0"?>
    <!DOCTYPE module PUBLIC
                "-//Puppy Crawl//DTD Check Configuration 1.3//EN"
                "http://www.puppycrawl.com/dtds/configuration_1_3.dtd">
    <module name="Checker">
      <module name="JavadocPackage">
        <property name="severity" value="warning"/>
      </module>
      <module name="SuppressionFilter">
        <property name="file" value="${checkstyle.suppressions.file}"/>
      </module>
      <module name="StrictDuplicateCode">
        <property name="severity" value="warning"/>
      </module>
      <module name="FileLength">
        <property name="severity" value="warning"/>
      </module>
      <module name="TreeWalker">
        <module name="FileContentsHolder"/>
        <module name="ConstantName">
          <property name="severity" value="warning"/>
        </module>
        <module name="AvoidStarImport">
          <property name="severity" value="error"/>
        </module>
        <module name="EmptyBlock">
          <property name="severity" value="error"/>
        </module>
      </module>
    </module>
    

    Figure 5.0

  17. All the settings are completed. Now build the project with “mvn clean install checkstyle:checkstyle” command. Check the generated checkstyle.html in ../good_qualiy_project/target/site directory. The mentioned error as in Figure 1.0 will NOT be there anymore.
  18. end.

Reference

  1. March 24th, 2013

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: