Configuring CheckStyle
CheckStyle is a tool to help developers write Java code that adheres to a particular coding convention. CheckStyle analyzes the code to check for certain rules, such as formatting and code layout, and alerts any issues to the developer. As a result, projects that utilize CheckStyle have easily-readable and well-formatted code.
Each CheckStyle configuration file (.xml) has their own unique rulesets defined. Developers can choose to use the bundled configuration files or customized configuration files.
This section will examine how to integrate CheckStyle into your IntelliJ IDEA and run CheckStyle tests against your code.
Table of Contents
Installing CheckStyle-IDEA Plugin
This instruction set contains steps to install the CheckStyle-IDEA plugin needed to run CheckStyle on IntelliJ IDEA.
Open IntelliJ IDEA to a new project.
Click on [IntelliJ IDEA] > [Preferences] from the toolbar at the top of your screen. A new “Preferences Window” should appear.
In the “Preferences Window”, click on [Plugins] on the left side. Check [Marketplace] is highlighted at the top of the “Preferences Window”, and type
CheckStyle-IDEA
in the search bar.
Click on [Install] and wait for the download to finish.
Restart the IntelliJ IDEA by clicking on [Restart IDE].
After the IntelliJ IDEA has reloaded, navigate to [Preferences] > [Plugins] and click on [Installed].
Ensure CheckStyle-IDEA is enabled with a check mark. You should also see [CheckStyle] near the bottom-left corner of the “Main Window”.
Caution: Make sure CheckStyle-IDEA is enabled before you proceed to the next section.
Now you have successfully installed CheckStyle-IDEA to your IntelliJ IDEA.
Running CheckStyle Tests
Let’s try running a few CheckStyle tests to determine how clean our code is. We will first configure which CheckStyle rules to enforce for the current project.
Navigate to [IntelliJ IDEA] > [Preferences] > [Other Settings] > [Checkstyle].
Under ‘Configuration File’, click the checkbox next to the desired CheckStyle configuration file. For now, check ‘Sun Checks’ and click on [Apply] > [OK] to save the settings and close the “Preferences Window”.
Note: If you wish to add a customized CheckStyle configuration file, click on [+] under ‘Configuration File’.
Download the CheckStyle.java file and add it to your project directory. The file should appear on the left side of the IntelliJ IDEA.
Note: If the project navigation bar is hidden, try clicking on [1: Project] on the left side.Open the CheckStyleTest.java file by double-clicking on the file name. Once you select the file to test, run the tests by clicking on [CheckStyle] at the bottom, then the green play button.
CheckStyle will now begin to analyze the selected file.After analysis is complete, CheckStyle will inform you of all the violated standard checks. For CheckStyleTest.java, there should be a few standard check errors including ‘LocalVariableName’ and ‘MagicNumber’.
Note: For more information on standard check errors, the official documentation can be found here.
Now you can use CheckStyle to detect any stylistic errors and write consistent code throughout your project.
Another commonly used feature of IntelliJ is unit testing.