Problem solve Get help with specific problems with your technologies, process and projects.

Maven Checkstyle Plugin example: How to enforce Java quality rules

It's important to ensure that every developer on the team writes code that complies with conventions and governance rules. It's a key part of ensuring software quality. Of course, if you want a team of developers to produce thousands of lines of code every single sprint, a line-by-line inspection of each Git commit simply isn't possible. That's why you need static code analysis tools that apply Java Checkstyle rules.

Maven Checkstyle Plugin

While it's not unusual for developers to have personal preferences when it comes to formatting code on their local machines, it is necessary to ensure that the code that gets checked into a version control system follows prescribed guidelines. When you consider that Maven is the most common build tool used by continuous integration servers such as Jenkins and Gradle, it shouldn't be surprising that Maven Checkstyle Plugin exists. Given that its integration is relatively simple, its configuration is minimal and its reports are highly informative, there really isn't any reason for development teams not to integrate Maven Checkstyle Plugin into their builds.

Maven Checkstyle integration

In its most primitive form, Checkstyle is distributed as a stand-alone Java application packaged as an executable JAR file. But the integration of an external Java JAR into a Maven build is a hassle, which is exactly why Maven Checkstyle Plugin exists. By simply including a Checkstyle goal as part of a Maven build, Java Checkstyle rules automatically apply to the code base, and a Checkstyle report is generated.

Maven Checkstyle Plugin
Maven Checkstyle Plugin invoked from the command line.

Java Checkstyle rules reports

In the associated video, Maven Checkstyle Plugin applies various formatting rules to a Spring Boot microservices application through the use of the following command:

mvn clean install checkstyle:checkstyle

The generated Java Checkstyle rules report reveals a great number of issues, ranging from missing Javadoc comments to the use of shadow variables in the code. Garnering similar insights from your own projects is as simple as incorporating Maven Checkstyle Plugin into your own Gradle, Ant or Jenkins build jobs.

The source code used in this example can be downloaded from GitHub.

View All Videos