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
- Resolve Class design problems.
- Resolve Method design problems.
- Ability to check code layout.
- Resolve Formatting issues.
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.
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:
- 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.
- 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.
- 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.
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.
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
- To Configure in IntelliJ:
- Open Settings & search “Checkstyle”
- A window will be shown which has an option to select the checks.
- Click on the + button and a window will open which will let us specify the location of the file to be used.
- Now, select a configuration XML file and click Next.
- This will open up the previous window and show the newly added custom configuration option.
- We select the new configuration and click on OK to start using it in our project.
2. Eclipse IDEA
- To Configure in Eclipse:
- Go to Window –> Preferences -> Checkstyle.
- At the Global Check Configurations section, click on New.
- This will open up a dialogue that will provide you with an option to specify the custom configuration file.
Custom Checkstyle Configuration
Here is the custom configuration file used for the above checks:
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.
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.
- Checker: Modules are structured in a tree that has the Checker module at the root. This module defines the properties that are inherited by all other modules of the configuration.
- TreeWalker: This module checks the individual Java source files and defines properties that are applicable to checking such files.
- AvoidStarImport: This module sets a standard for not using Star imports in our Java code. It also has a property that asks the plugin to report the severity of such issues as a warning. Thus, whenever such violations are found in the code, a warning will be flagged against them.