Checkstyle configuration in maven

What most annoying programmers besides reading someone else’s code? That’s right, formatting! In a large team it is difficult to impart to everyone the same requirements for the style of the program code. One way out of this situation is to automatically configure the code style check.
This can easily be configured with the help of a checkstyle.

Checkstyle is a development tool to help programmers write Java code that adheres to a coding standard. It automates the process of checking Java. This makes it ideal for projects that want to enforce a coding standard.

Let’s create a sample code from our article and try to check it for the google check style guide.

Checking for violations as part of the build

We need to edit pom.xml  to add the checkstyle settings.

checkstyle.config.location  – is the name of the file with the settings
checkstyle.suppressions  – an exception file, it allows you to specify which rules can be ignored.

Let’s try to test our project.

So we see that in our project there are 22 inconsistencies to our checkstyle.
On each line there is a file where it happened and the string is very convenient for fixing the problem.

Generate checkstyle report as standalone

You can also generate the Checkstyle report by explicitly executing the checkstyle:checkstyle  goal from the command line. You are not required to specify the Checkstyle Plugin in your pom.xml  unless you want to use a specific configuration.

Generate checkstyle html report as part of the project reports

To generate the Checkstyle report as part of the Project Reports, add the Checkstyle Plugin in the <reporting>  section of your pom.xml .

and add dependencies in build/plugins  sections

Now run mvn site

After the build, open report file target/site/checkstyle.html. We will see all checkstyle violation.

maven generated checkstyle report

In conclusion, I’ll link to the official google style guide.

You can download full source here

3 Comments on “Checkstyle configuration in maven”

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.