DEV Community

Cover image for Java Checkstyle Tool: Enforce Coding Standards with Ease
Serhat Ozdursun
Serhat Ozdursun

Posted on

Java Checkstyle Tool: Enforce Coding Standards with Ease

As software projects grow, maintaining code consistency becomes increasingly challenging—especially in teams. Java, being a verbose and widely-used language, is particularly prone to inconsistencies in formatting, naming, and structure. That’s where Checkstyle comes in.

What is Checkstyle?

Checkstyle is a static code analysis tool for Java that helps developers write code that adheres to a defined coding standard. It enforces style guidelines by checking source code against a configurable set of rules, making your codebase cleaner, more readable, and easier to maintain.

Whether you're working solo or in a team, using Checkstyle ensures uniformity and reduces time spent in code reviews discussing stylistic issues.

Key Features

  • Enforces coding standards (Google, Sun, or custom)
  • Detects common code smells
  • Integrates with Maven and Gradle
  • Supports IDE plugins (IntelliJ, Eclipse)
  • Highly customizable via XML config

Getting Started

For Maven Projects

Add the following plugin to your pom.xml:

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-checkstyle-plugin</artifactId>
      <version>3.3.0</version>
      <configuration>
        <configLocation>google_checks.xml</configLocation>
        <encoding>UTF-8</encoding>
        <consoleOutput>true</consoleOutput>
        <failsOnError>true</failsOnError>
      </configuration>
      <executions>
        <execution>
          <phase>validate</phase>
          <goals>
            <goal>check</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>
Enter fullscreen mode Exit fullscreen mode

For Gradle Projects

Add the plugin to build.gradle:

plugins {
    id 'checkstyle'
}

checkstyle {
    toolVersion = '10.3.4'
    configFile = file('config/checkstyle/checkstyle.xml')
}

tasks.withType(Checkstyle) {
    reports {
        xml.required = false
        html.required = true
    }
}
Enter fullscreen mode Exit fullscreen mode

Built-in or Custom Rules

Checkstyle supports built-in configurations:

  • google_checks.xml
  • sun_checks.xml

Or you can define custom rules. Below are some practical examples for test automation projects, like I did:

Prevent Thread.sleep()

<module name="Regexp">
  <property name="format" value="Thread\.sleep\(.*\)"/>
  <property name="illegalPattern" value="true"/>
  <property name="message" value="Do not use Thread.sleep(). Use an explicit wait instead."/>
</module>
Enter fullscreen mode Exit fullscreen mode

Prevent By.xpath(...)

<module name="Regexp">
  <property name="format" value="By\.xpath\(.*\)"/>
  <property name="illegalPattern" value="true"/>
  <property name="message" value="Avoid using XPath locators. Use ID, class, or accessibility locators instead."/>
</module>
Enter fullscreen mode Exit fullscreen mode

Prevent System.out.println

<module name="Regexp">
  <property name="format" value="System\.out\.println"/>
  <property name="illegalPattern" value="true"/>
  <property name="message" value="Use a logger instead of System.out.println."/>
</module>
Enter fullscreen mode Exit fullscreen mode

Running Checkstyle

Maven:

mvn checkstyle:check
Enter fullscreen mode Exit fullscreen mode

Gradle:

./gradlew checkstyleMain
Enter fullscreen mode Exit fullscreen mode

Manual CLI:

java -jar checkstyle-10.3-all.jar -c /path/to/config.xml MyClass.java
Enter fullscreen mode Exit fullscreen mode

GitHub Actions CI Integration

You can automate Checkstyle checks with GitHub Actions and prevent merging code that doesn't comply with your defined coding standards. This ensures every commit meets your team's style expectations before it's integrated into your main branch.

You can automate Checkstyle checks with GitHub Actions:

Create .github/workflows/checkstyle.yml:

name: Checkstyle

on: [push, pull_request]

jobs:
  checkstyle:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v3

      - name: Set up Java
        uses: actions/setup-java@v4
        with:
          java-version: '17'
          distribution: 'temurin'

      - name: Build and Run Checkstyle
        run: mvn checkstyle:check
Enter fullscreen mode Exit fullscreen mode

Use ./gradlew checkstyleMain instead if you're on Gradle.

IDE Support

  • IntelliJ IDEA: Use the CheckStyle-IDEA plugin
  • Eclipse: Use the Checkstyle Plugin via Eclipse Marketplace

Final Thoughts

Checkstyle is more than just a style enforcer—it's a tool that encourages code quality, team consistency, and professional discipline. Whether you're building a new application or maintaining legacy code, integrating Checkstyle is a low-effort, high-reward decision.

Start simple with Google or Sun checks, then evolve your configuration with custom rules that make sense for your team and project.

Top comments (0)