TL;DR: Use Google Java Format (via the Spotless Gradle plugin) for code formatting, Checkstyle for style enforcement, and PMD for static code analysis. These tools are non-negotiable and must be integrated into every project's CI pipeline.
Java's ecosystem does not include a built-in formatter like Go, so the team standardizes on Google Java Format. Combined with Checkstyle for style rules and PMD for bug detection, this toolchain ensures consistent, high-quality code across all projects.
Google Java Format produces a single canonical formatting for any Java source file. It is applied via the Spotless Gradle plugin, which also handles import ordering and license header enforcement.
plugins {
id 'com.diffplug.spotless' version '6.25.0'
}
spotless {
java {
googleJavaFormat('1.22.0')
removeUnusedImports()
trimTrailingWhitespace()
endWithNewline()
}
}# Auto-format all Java files
gradle spotlessApply
# Check formatting without modifying files (CI mode)
gradle spotlessCheckDo not use alternative formatters. Google Java Format is the team standard and all Java code must be formatted with it.
Spotless handles import ordering automatically when using Google Java Format. The standard grouping is:
com.*-- third-party packagesjava.*-- standard libraryjavax.*/jakarta.*-- extension libraries- Static imports (last)
Each group is separated by a blank line. Unused imports are removed automatically.
Checkstyle enforces coding standards and style rules. The project uses the Google ruleset as a baseline with project-specific customizations.
plugins {
id 'checkstyle'
}
checkstyle {
toolVersion = '10.12.1'
configFile = file('src/main/resources/app/quality/checkstyle-google-ruleset.xml')
maxWarnings = 0
maxErrors = 0
}# Run Checkstyle on main sources
gradle checkstyleMain
# Run Checkstyle on test sources
gradle checkstyleTest| Rule | Description |
|---|---|
IndentationCheck |
Enforces consistent indentation (2 spaces) |
LineLength |
Maximum line length (100 characters) |
NeedBraces |
All control structures must use braces |
JavadocMethod |
Public methods require Javadoc (configurable) |
UnusedImports |
No unused imports allowed |
PMD detects potential bugs, dead code, suboptimal code, and overly complicated expressions.
plugins {
id 'pmd'
}
pmd {
toolVersion = '7.1.0'
ruleSetFiles = files('src/main/resources/app/quality/pmd-custom-ruleset.xml')
consoleOutput = true
}# Run PMD analysis
gradle pmdMain
gradle pmdTestSpotBugs detects potential security vulnerabilities and bug patterns in compiled bytecode.
plugins {
id 'com.github.spotbugs' version '6.0.22'
}
spotbugs {
toolVersion = '4.8.4'
excludeFilter = file('src/main/resources/app/security/spotbugs-security-exclude.xml')
}# Run SpotBugs on main sources
gradle spotbugsMain
# Run SpotBugs on test sources
gradle spotbugsTest- Install the google-java-format plugin.
- Enable it in Settings > google-java-format Settings > Enable google-java-format.
- Enable Settings > Editor > General > Auto Import > Optimize imports on the fly.
- Install the Checkstyle-IDEA plugin and point it to the project's ruleset.
Install the Language Support for Java extension and add the following settings:
{
"java.format.settings.url": "https://raw.githubusercontent.com/google/styleguide/gh-pages/eclipse-java-google-style.xml",
"java.format.enabled": true,
"editor.formatOnSave": true,
"[java]": {
"editor.defaultFormatter": "redhat.java"
}
}