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

Join the conversation


Send me notifications when other members comment.

Please create a username to comment.

Which static code analysis tools for Java do you incorporate in your software development builds?
I think Gradle is a build tool like Maven and not a continuous integration tool.
You're not wrong.

But with every new Gradle release, they move deeper and deeper into the world of CI and CD. When they started releasing schedulers, I really started to wonder what the heck was the difference between Gradle and a CI/CD tool. And with their DSL and deep integration with Groovy, it's easy for programmers to program Gradle pipelines that go right from compilation, through testing and into deployment. In fact, the Jenkins pipeline as code implementation which I am a fan of could learn a lot from the way Gradle does things.

So yes, you are fundamentally correct. When I grouped Jenkins and Gradle together, I was thinking more about the potential for Gradle as the tool continually approves, as opposed to the manner in which it is typically used in enterprise development.