Hi there fellow reader,

today I’m gonna show how you can configure your Jenkins job to use your custom Checkstyle rules.

I assume you already know the development tool Checkstyle to improve your code quality.
If not, you should check it out! Follow me to the Checkstyle homepage 🙂

 

References for Checkstyle

The Checkstyle routine is based on an XML document which contains information about the rules that should be checked and applied.
You can find the original (Vanilla) Sun Coding checks here: https://github.com/checkstyle/checkstyle/blob/master/src/main/resources/sun_checks.xml

If you want to read more about the available checks, check out this page
http://checkstyle.sourceforge.net/checks.html

 

Jenkins plugins

Personally I use these two plugins to display my test results. However you just need the first one
Checkstyle Plugin
Analysis Collector Plugin

 

Jenkins Configuration

You now need to add the checkstyle goal to your build configuration

checkstyle:checkstyle -Dcheckstyle.config.location="https://dustplanet.de/checkstyle.xml"

The -Dcheckstyle.config.location=”” is the location of your Checkstyle rules
My checkstyle example can be found here
https://dustplanet.de/checkstyle.xml

 

Using CloudFlare?

At first I tried to access my XML file via the SSL option of CloudFlare but encountered the following error

sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

It seems that Maven could not validate the SSL cert – sadly a known issue of CloudFlare’s SSL and self signed certificates.

Edit: 2017/01/06: Recent Java and Maven (at least 3.2.5) versions are capable of CloudFlare and Let’s Encrypt certificates!
However, Browser Integrity Check is recommended to be turned off.

I decided to host the file without SSL but ran into the Browser Integrity Check of CloudFlare.
This can be bypassed with a Page rule
Make sure that

  • SSL – off
  • Browser Integrity Check – off
  • Custom caching – Bypass cache
  • Apps – off
  • Performance – off

You can see a screenshot of it here
pagerule

 

Troubleshooting

If you receive an error like

cannot initialize module TreeWalker - Unable to instantiate RedundantThrows: Unable to instantiate RedundantThrowsCheck

you will need to modify your checkstyle.xml.
With version 6.2 of Checkstyle some checks were removed without any replacement.
Affected checks are StrictDuplicate and RedundantThrows
Please refer to the release notes, too.

Thanks for reading and feel free to comment if you need help.
Tim

HowTo – Use your own Checkstyle rules in your Jenkins/Maven job
The short URL of this article is: https://dustpla.net/DSi0W

Leave a Reply

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