Open In App

Introduction to Checkstyle Plugin for Checking Java Code Quality

Checkstyle is a development tool to help programmers to write Java code that sticks to a coding standard. It automates the process of checking Java code. It is an open-source tool that checks code against a configurable set of rules. It allows you to define your own set of rules and check your code against it. These rules can be used in your IDE or via Maven and Gradle.

Features of Checkstyle

How to integrate Checkstyle into a Java project via Maven and IDEs?

The plugins are independent of each other and can be integrated individually in our build or IDEs. For example, the Maven plugin isn’t required in the pom.xml to run the validations in the IntelliJ or Eclipse IDEs. To Configure the Checkstyle in our Project, we need to add the plugins with the help of Maven Configuration.



Plugin:




<reporting>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-checkstyle-plugin</artifactId>
            <version>3.0.0</version>
            <configuration>
                <configLocation>checkstyle.xml</configLocation>
            </configuration>
        </plugin>
    </plugins>
</reporting>

We can use a Sun-style check and a Google-style check (two Predefined checks by Checkstyle). The default check for a project is sun_checks.xml.



To Download the Latest Release of Checkstyle, Click Here.

Generation of Report

After the Maven Configuration, Let’s generate a report for the code by running the mvn site command. After the finishing of the build, the report will be available in the target/site folder under the name checkstyle.html.

The Three Major Parts in the Report is:

  1. Files: Files provide us with the list of files in which the violations have happened. It also shows us the counts of the violations against their severity levels.
  2. Rules: Rules give us an overview of the rules that were used to check for violations. It shows the category of the rules, the number of violations, and the severity of those violations.
  3. Details: The details section of the report provides us with the details of the violations that have happened. The details provided are at line number level.

Build Integration

If there’s a need to have strict checks on the style of coding, we can configure the plugin in such a way that the build fails when the code doesn’t stick to the standards.




<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-checkstyle-plugin</artifactId>
    <version>${checkstyle-maven-plugin.version}</version>
    <configuration>
        <configLocation>checkstyle.xml</configLocation>
    </configuration>
    <executions>
        <execution>
            <goals>
                <goal>check</goal>
            </goals>
        </execution>
    </executions>
</plugin>

The config file will be checkstyle.xml. The goal check mentioned in the execution section asks the plugin to run in the verify phase of the build and forces a build failure when a violation of coding standards occurs. After this, if we run the mvn clean install command then it will scan every file for violations and the build will fail if any violations deduct. We can also run only the check goal of the plugin using mvn checkstyle:check , without configuring the execution goal. Running this step will result in a build failure as well if there are any validation errors.

IDE Checkstyle Plugins

1. IntelliJ IDEA

2. Eclipse IDEA

Custom Checkstyle Configuration

Here is the custom configuration file used for the above checks:




<!DOCTYPE module PUBLIC
  "-//Puppy Crawl//DTD Check Configuration 1.3//EN"
<module name="Checker">
    <module name="TreeWalker">
        <module name="AvoidStarImport">
            <property name="severity" value="warning" />
        </module>
    </module>
</module>

DOCTYPE Definition

The first line of the i.e. the DOCTYPE definition is an important part of the file and it tells where to download the DTD from so that the configurations can be understood by the system.

Modules

A configuration file is primarily composed of Modules. A module has an attribute name that represents what the module does. The value of the name attribute corresponds to a class in the plugin’s code which is executed when the plugin is run.

Module Details


Article Tags :