Using a custom Checkstyle Ruleset on a Maven Multi Module Project

I already wrote an article about using the same Checkstyle ruleset and Checkstyle suppressions file in Eclipse and Maven [1]. One arising question in the comment section was, how one can use the ruleset on a Maven multi-module project.

The problem

Let’s assume we have a Maven project “main-project” and some modules inside this project. Also there is a checkstyle ruleset “checkstyle.xml” inside the main-project. See the picture for details.

The folder structure including a main project, it's sub-modules and the checkstyle ruleset.

In this case the following configuration in the main-project’s pom.xml will fail when building the modules:

This is because Maven will resolve ${basedir} to the modules directory. But there isn’t a check-directory containing the checkstyle.xml – it is in the parent directory.

Solution 1

A simple solution would be to just add the checkstyle-plugin to every module-pom. There you refer to the checkstyle.xml via “${basedir}/../checks/checkstyle.xml” (or “${project.parent.basedir}/checks/checkstyle.xml”):

This solution is a bit cleaner than solution 1. But you still have to copy a piece of configuration to every module-pom.

Solution 3

If you do not want to edit your module-poms you can use profiles to overwrite the checkstyleDir-property. Therefore you have to introduce the property “checkstyleDir” in the main-project’s pom like in solution 2:

By adding the following configuration code to your main-project’s pom, Maven will check whether the checkstyle directory exists in the basedir and if not it will activate a profile which overwrites the property “checkstyleDir”:

first of all I would suggest to double-check whether you really need different checkstyle configurations. Often you can handle different needs on different modules by using a suppression filter, like “suppress rule x on all files in module 1” and “suppress rule y on all files in module 2” and so on.

But for sure – sometimes that’s not enough, eg. if you want to set a properties on a rule different for different modules.

In that case I would suggest to store the checkstyle.xml-files inside the associated modules (eg. create a folder ‘checks’ in every module and put the associated checkstyle rule set inside). If all of your rule sets have the same name (like ‘checkstyle.xml’), you can simply add the configuration in the parent-pom with configLocation set to ${basedir}/checks/checkstyle.xml and propertiesLocation set to ${basedir}/checks/checkstyle_maven.properties.

If your rules sets also differ in naming (like ‘checkstyle-module-1.xml’, ‘checkstyle-module-2.xml’) I strongly suggest to simply add the checkstyle configration in every module’s pom, refering to the module-included rule set. That’s not a great solution because you have to dublicate the same configuration block in every pom – but it’s simple and easy to understand (=KISS ;-)). Every other solution would lack in this point.

About this blog

The intention of this blog is to give clues and solutions to problems I struggle with in my every day work. As not everything is worth to write about it and I also don't want to post stuff which could be found on
every corner of the internet this blog is updated continuously but not regularly.

If this blog helped you in some way or you want to share your point of view to
a given topic—feel free to do so. I appreciate every comment and try to answer
as fast as my spare time allows me to do so.