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

5 Comments on “Checkstyle configuration in maven”

  1. I have been trying to skipCheckStyle in the automation module alone of my multi module maven project.

    below is my config and maven version in my mac os is 3.1.1 and it skips the check style when I run
    -test
    -DtestNG=TestNgFitmentSevice.xml
    -Dendpoint=xxxtest.com:8080
    -Dcustomgroup=REGRESSION
    -DskipCheckstyle=true

    org.apache.maven.plugins
    maven-checkstyle-plugin
    2.17

    true
    ${checkstyle.config.location}
    UTF-8
    true
    false

    But the very same does not work in Jemkins , In Jenkins the check-style gets kicked off even if I want to skip it . Any idea on how to fix this ? The maven version is 3.1.1 in jenkins as in local and java version is 1.8 JDK .

    1. It depends on your Jenkins configuration. You need to sure that the maven receives your parameter -DskipCheckStyle=true. Check your Jenkins job output.

Leave a Reply

Your email address will not be published.

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