Analyzing code with the Maven Checkstyle plugin

Checkstyle is a tool that helps programmers follow coding standards. It automates the process of checking if defined coding standards are followed. It can support any coding standards by suitable configuration. Like other tools, it can be run standalone as well as integrated with Maven.

How to do it...

Use the following steps to analyze code with the Maven Checkstyle plugin:

  1. Open the Maven project for which you want to do a Checkstyle analysis (for instance, project-with-violations).
  2. Run the following command:
    mvn checkstyle:checkstyle
    
  3. Observe the output as shown in the following screenshot:
    [INFO]
    [INFO] --- maven-checkstyle-plugin:2.13:checkstyle (default-cli) @ project-with-violations ---
    [INFO]
    [INFO] There are 29 checkstyle errors.
    [WARNING] Unable to locate Source XRef to link to - DISABLED
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    
  4. Open the checkstyle-result.xml report in the target folder:
    How to do it...

How it works...

Unlike the pmd, checkstyle goal of Maven, the Checkstyle plugin is not bound to any phase.

When the checkstyle goal is run, it generates a Checkstyle site report using default rulesets and the configuration set in the plugin. It also generates a Checkstyle output file in the XML format.

The Maven Checkstyle plugin supports several configuration options to customize the rules, exclude files from being checked, and so on. Let's briefly discuss the examples that show usage of Maven Checkstyle plugin in some advanced usecases:

  1. Checkstyle rules can be specified inline in the configuration section of the plugin:
    <configuration>
      <checkstyleRules>
        <module name="Checker">
          <module name="TreeWalker">
            <module name="FinalLocalVariable">
              <property name="tokens" value="VARIABLE_DEF,PARAMETER_DEF"/>
            </module>
          </module>
        </module>
      </checkstyleRules>
    </configuration>
  2. They can also be specified in an external file and referred using the configLocation element:
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-checkstyle-plugin</artifactId>
      <version>2.14</version>
      <configuration>
        <configLocation>checkstyle.xml</configLocation>
      </configuration>
    </plugin>
  3. A Suppressions filter can be created to tell Checkstyle not to report violations on specific files and specific sections of the files:
    <suppressions>
      <suppress checks="JavadocStyleCheck"
                 files="GeneratedObject.java"
                 lines="50-9999"/>
      <suppress checks="MagicNumberCheck"
                 files="LegacyDatasetConvertor.java"
                 lines="221,250-295"/>
    </suppressions>

There's more...

As in the case of PMD, we can configure the Maven Checkstyle plugin such that it fails a build in case of errors:

  1. Add the following code to the pom file of project-with-violations:
    <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-checkstyle-plugin</artifactId>
            <version>2.14</version>
            <executions>
              <execution>
                <id>verify-style</id>
                <phase>process-sources</phase>
                <goals>
                  <goal>check</goal>
                </goals>
              </execution>
            </executions>
          </plugin>      
        </plugins>
      </build>
  2. Run the following Maven command:
    mvn verify
    
  3. Observe the output as shown in the following screenshot:
    There's more...
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.141.38.121