Checkstyle
Tags: java, codequality, productivity
Published: 2021-01-22 22:54:16

*Hello there!*

This is the second tool I encountered in my search journey and it looks like we had lots of styling issues.

It is true that most of them can be easily resolved by having a common *formatter* style installed in the IDE, but for custom and more accurate styling, this tool can come in handy for you.

##[Checkstyle](https://checkstyle.sourceforge.io/)

It is an awesome tool from developers to developers, meant to automate a bit the process of checking Java code, sparing humans from it, and letting them focusing on important things.

####Installation

There are a few ways to install it into your projects, such as:
* Direct JAR which can be taken from [Github](https://github.com/checkstyle/checkstyle/releases/)
* Using it through your IDE, check this [list](https://checkstyle.sourceforge.io/#Related_Tools_Active_Tools) for the suitable one for you
* Maven [plugin](https://mvnrepository.com/artifact/com.puppycrawl.tools/checkstyle/8.39) set at build step in your project's *pom.xml* file

####Usage
Depending on your chosen path, there are different ways to customize your code conventions. For this article, I chose to write more about the last option, which implies a simple addition in your *pom.xml* file within the **plugins** section:

```xml
<plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-checkstyle-plugin</artifactId>
   <version>3.1.1</version>
   <configuration>
     <consoleOutput>true</consoleOutput>
	 <violationSeverity>warning</violationSeverity>  <!-- now build fails for Warnings -->
	 <failOnViolation>true</failsOnViolation>
	 <configLocation>some-dir/your-checkstyle.xml</configLocation>
	 <sourceDirectories>
		<sourceDirectory>src/main/java</sourceDirectory> <!-- Root directory from where the checking process starts -->
	 </sourceDirectories>
   </configuration>
   <executions>
     <execution>
       <id>validate</id>
       <phase>validate</phase>
       <goals>
         <goal>check</goal>
       </goals>
     </execution>
   </executions>
 </plugin>
```

Now, whenever the project is built, this checker will be triggered, and in case there at least one warning, then the build will **Fail**.

####Code conventions
The official documentation provides a nice and easy to use [list](https://checkstyle.sourceforge.io/checks.html) of all standards checks that this tool supports. You should always refer to it when you are looking for some explanations or details for a certain module out there.

On the other hand, there are a couple of presets files that can be used almost out of the box: 
* [google_checks.xml](https://github.com/checkstyle/checkstyle/blob/master/src/main/resources/google_checks.xml)
* [sun_checks.xml](https://github.com/checkstyle/checkstyle/blob/master/src/main/resources/sun_checks.xml)

Or, you can create a custom one that suits your team and your project starting from the list or from the presets.

***
That's pretty much it, I hope you enjoy it and that it will come in handy for your current or incoming projects, but don't necessarily believe me, try it yourself.
***

*Stay tuned for the next article in the series!*


> *[Cover photo](https://unsplash.com/photos/iHeX3jWkv9s) by Markus Spiske on [Unsplash](https://unsplash.com/)*