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

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:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-checkstyle-plugin</artifactId>
    <configuration>
        <configLocation>${basedir}/checks/checkstyle.xml</configLocation>
    </configuration>
</plugin>

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”):

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-checkstyle-plugin</artifactId>
    <configuration>
        <configLocation>${basedir}/../checks/checkstyle.xml</configLocation>
    </configuration>
</plugin>

This will overwrite the parents’ settings. Indeed this is an ugly fix because you have to copy the same piece of configuration code to every module.

Solution 2

Alternatively you can introduce a new property “checkstyleDir” and change the configLocation to:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-checkstyle-plugin</artifactId>
    <configuration>
        <configLocation>${checkstyleDir}/checkstyle.xml</configLocation>
    </configuration>
</plugin>

In your main-project’s pom you also introduce:

<properties>
    <checkstyleDir>${basedir}/checks</checkstyleDir>
</properties>

and in every module’s pom:

<properties>
    <checkstyleDir>${basedir}/../checks</checkstyleDir>
</properties>

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:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-checkstyle-plugin</artifactId>
    <configuration>
        <configLocation>${checkstyleDir}/checkstyle.xml</configLocation>
    </configuration>
</plugin>

and

<properties>
    <checkstyleDir>${basedir}/checks</checkstyleDir>
</properties>

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”:

<profiles>
	<profile>
		<id>activate-in-module</id>
		<activation>
			<file>
				<exists>${baseDir}/../check/checkstyle.xml</exists>
			</file>
		</activation>
		<properties>
			<checkstyleDir>${baseDir}/../checks</checkstyleDir>
		</properties>
	</profile>
</profiles>

Obvious this solution is also worthy of discussion, but if I have to choose between the solutions shown here – solution 3 is still my favorite :-)

Reference

  1. [1] https://rolf-engelhard.de/2011/04/using-the-same-suppression-filter-for-checkstyle-in-eclipse-and-maven

Rolf Engelhard

 

Comments (5) Write a comment

  1. How would you set-up the Maven scripts to have different checkstyle configurations for different modules (or e.g. to override the parent configuration)?

    Reply

    • Hello Roman,

      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.

      Reply

    • Hello Swirl,

      I haven’t tried it, but I don’t see a point why this shouldn’t work… at least solution 1 should work if you don’t run the maven-checkstyle-plugin in the main-project’s pom but only on the modules…

      Reply

  2. Another solution would be to use ${session.executionRootDirectory} instead of ${baseDir}.

    Reply

Leave a Reply

Required fields are marked *.