linkHow to Centralize your Checkstyle Configuration with Maven

How to Centralize your Checkstyle Configuration with Maven

Checkstyle, loved by those who set it up, hated by those who didn’t, is Javas most popular tool to force your code style flavor onto others. Whatever your feelings about this static analyzer may be, if you have to manage it in your project(s) this article is for you.

A familiar sight for Checkstyle users A familiar sight for Checkstyle users

The default setup is quite easy. Create your checkstyle.xml add the rules you prefer and reference it in your Checkstyle Maven plugin. This may be fine for a single project, managing more would require copying the configuration file over and manually syncing them. Since most developers nowadays can’t escape the ubiquitous micro-services style architecture, where the myriads of services are managed either in a monorepo or in multiple individual ones, the latter needs a better solution for this problem.

linkMaven to the Rescue

Of course there is a direct solution for this in your favorite build management system (which is, of course, Maven)! The required steps are as follows:

  1. Create a new Maven project only containing your checkstyle.xml

  2. Reference it in the Checkstyle Maven plugin of your consumer project

  3. Make your project deployable to publish it to your maven repo

Done!

link1. Create a Project for your Checkstyle Config

Create a new project with your preferred method. We name our configuration file checkstyle.xml and put it into /src/main/resource.

As test configuration I used this unbearable annoying line length restriction (which helps to see if it works later):

1

2 "-//Puppy Crawl//DTD Check Configuration 1.2//EN"

3 "http://www.puppycrawl.com/dtds/configuration_1_2.dtd">

4

5

6

7

8

9

Now all you have to do is to install it to your local repo with:

1mvn clean install

link2. Reference it in your Consumer Project

Find the Checkstyle Plugin in your consumer POM and add your newly create config project as dependency. This may look like this:

1

2 org.apache.maven.plugins

3 maven-checkstyle-plugin

4

5

6 com.puppycrawl.tools

7 checkstyle

8 8.31

9

10 **

11 com.company.projectname

12 checkstyle-config

13 1

14 **

15

16

17 **checkstyle.xml**

18

19

The filename used in configLocation must match the filename used in your_ checkstyle-config_ project. Don’t forget to delete your local Checkstyle configuration file. Now if you do:

1mvn checkstyle:check

it should use the config from your config project.

link3. Make your Config Project Deployable

Some Maven repositories, like Maven Central, require a sources and javadoc -jar if you want to deploy them there. Since there is neither source code, nor javadoc we have to create placeholders. In our_ checkstyle-config_ project add the plugin config for the source code:

1

2 org.apache.maven.plugins

3 maven-source-plugin

4

5

6 attach-sources

7

8 jar-no-fork

9

10

11

12

then for the javadoc

1

2 org.apache.maven.plugins

3 maven-jar-plugin

4

5

6 empty-javadoc-jar

7 package

8

9 jar

10

11

12 javadoc

13 ${basedir}/javadoc

14

15

16

17

Now after setting the correct coordinates for the distribution management, you should be able to deploy and publish your configuration project with

1mvn deploy

A full example, deployed to Maven Central, can be found here

patrickfav/checkstyle-config_ Externalized checkstyle configuration which can be used in other Maven projects. This is a hand-picked, incrementally…_github.com

linkHow to use Suppressions

While it is possible to also package a checkstyle-suppression.xml in the same way as described above, I do not think a global suppression file makes a whole lot of sense.

It is, however, possible to set one locally, in basically the same way you would normally. Create your suppression config in your project and reference it in your Checkstyle plugin configuration. In this example we name the file checkstyle-suppression.xml and put it into the project’s root folder. Then we add the reference in the POM:

1

2 org.apache.maven.plugins

3 maven-checkstyle-plugin

4 ...

5

6 checkstyle.xml

7 checkstyle-suppressions.xml

8

9

An example using suppressions with a global_ checkstyle-config_ can be found here (Checkstyle config is found in parent POM).

linkKeep using your IDE Checkstyle Plugin

If you use the excellent Checkstyle IntelliJ plugin (or a similar tool), you may wonder where the local configuration can be found. After you mvn install it will be located at /target/checkstyle-checker.xml.

linkSummary

We’ve created a separate Maven project to put a global Checkstyle configuration in it for easier distribution among other projects. We adjusted this project to be able to deploy it to Maven repositories with stricter rules, like Maven Central. We then discussed how to handle suppressions and local Checkstyle plugins.

linkSources

How to upload an artifact to Maven Central with an empty javadoc jar (or empty sources jar)…_ Thanks for contributing an answer to Stack Overflow! Please be sure to answer the question. Provide details and share…_stackoverflow.com

how to externalise the checkstyle config for maven-checkstyle-plugin_ Thanks for contributing an answer to Stack Overflow! Please be sure to answer the question. Provide details and share…_stackoverflow.com

This article was published on 4/19/2020 on medium.com.

How to Centralize your Checkstyle Configuration with MavenMaven to the Rescue1. Create a Project for your Checkstyle Config2. Reference it in your Consumer Project3. Make your Config Project DeployableHow to use SuppressionsKeep using your IDE Checkstyle PluginSummarySources

Home

OpenSourcechevron_right



Patrick Favre-Bulle 2020