Overview id-mask bkdf singlestep-kdf bcrypt slf4j-timber armadillo bytes-java hkdf dice under-the-hood uber-apk-signer uber-adb-tools indoor-positioning density-converter Dali BlurTestAndroid
Maintaining Checkstyle configs in a multi-repo project can be a chore. Let maven help you to create a global one.
Checkstyle, Maven, Java, Static Code Analysis, Configuration Management
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
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.
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:
Create a new Maven project only containing your checkstyle.xml
Reference it in the Checkstyle Maven plugin of your consumer project
Make your project deployable to publish it to your maven repo
Done!
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
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.
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
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).
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
.
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.
This article was published on 4/19/2020 on medium.com.
Overview How to Centralize your Checkstyle Configuration with Maven A Better Way to Protect Your IDs Security Best Practices: Symmetric Encryption with AES in Java and Android: Part 2: AES-CBC + HMAC The Bcrypt Protocol… is kind of a mess The Concise Interface Implementation Pattern Improving ProGuard Name Obfuscation Handling Proguard as Library Developer Managing Logging in a Multi-Module Android App Security Best Practices: Symmetric Encryption with AES in Java and Android
Patrick Favre-Bulle 2020