Introduction to Checkstyle Plugin for Checking Java Code Quality
Last Updated :
29 Oct, 2021
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.
Plugin:
XML
<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:
- 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.
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.
XML
<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
- 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:
XML
<!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="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
- 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.
Similar Reads
Introduction to Java Mission Control for JVM Monitoring Java Mission Control (JMC) is a powerful monitoring, diagnostics, and performance analysis tool. Oracle provides this to run the Java applications on the Java Virtual Machine. It is part of the Java Development Kit and offers developers and administrators insights into the runtime behaviour of Java
5 min read
7 Best Plugins For Spring Boot and Java in Eclipse With the fast-paced technology, every developer wants to build something which requires less time and effort. Being in one of the most growing communities with a count of more than 10 million, developers have to maintain that by working more efficiently. The best plugin you use for your project, the
5 min read
How to create a user defined javap tool? What is a javap tool? The javap tool is used to get the information of any class or interface. The javap command (also known as the Java Disassembler) disassembles one or more class files. Its output depends on the options used ("-c" or "-verbose" for byte code and byte code along with innards info,
4 min read
10 Best Java IDE's to Consider When we talk about popular programming languages, it is important to remember Java! In fact, it is currently the most popular programming language in the world with approximately 70 Lakh Developers using it daily. And one of the reasons for this is the versatile nature of Java. Be it desktop apps, m
8 min read
How to Setup IntelliJ IDEA For Java Competitive Programming Environment? IntelliJ IDEA is a popular Integrated Development Environment (IDE) used by many developers for Java programming. In this article, we'll walk through the steps to set up IntelliJ IDEA for competitive programming. Competitive programming is a popular activity among computer science students and profe
4 min read
Encode and Decode Strings in Java with JUnit Tests Strings are very useful and they can contain sequences of characters and there are a lot of methods associated with Strings. Moreover, encoding and decoding are possible for a given use case and it can be validated whether are they equal or not for a given requirement. They can be tested for validit
4 min read
7 Must Have IntelliJ IDEA Plugins For Java Developers IntelliJ IDEA is one of the popular IDEs for programming in Java. Java developers really like it but sometimes we have to do many things manually which is kind of boring and time-consuming. But IntelliJ IDEA is smart, it knows exactly when to come to the rescue with the armor and guard of various ti
6 min read
How to Generate Code Coverage Report with JaCoCo in Java Application? Testing is a most important part of a software development lifecycle. Without testing, the software is not ready for deployment. To test Java applications we mostly used Junit. JUnit framework is a Java framework that is used for testing. And now, JUnit is used as a standard when there is a need to
4 min read
10 Best Java Developer Tools to Boost Productivity Java is an object-oriented programming language that is used for developing various mobile apps and in website development. There are multiple tools available in the market for Java management. This software helps in the process of creating, testing and deploying in Java.Therefore, The Top 10 Java t
8 min read
PrintWriter checkError() method in Java with Examples The checkError() method of PrintWriter Class in Java is used to check the error state of this PrintWriter instance. This method flushes the stream in order to check the error state. It returns a boolean value that tells if the stream has encountered any error or not. Syntax: public boolean checkErro
2 min read