Skip to content

Latest commit

 

History

History
176 lines (127 loc) · 5.18 KB

File metadata and controls

176 lines (127 loc) · 5.18 KB

Java Formatting and Linting

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.

Overview

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.

Formatter: Google Java Format (via Spotless)

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.

Gradle Configuration

plugins {
    id 'com.diffplug.spotless' version '6.25.0'
}

spotless {
    java {
        googleJavaFormat('1.22.0')
        removeUnusedImports()
        trimTrailingWhitespace()
        endWithNewline()
    }
}

Usage

# Auto-format all Java files
gradle spotlessApply

# Check formatting without modifying files (CI mode)
gradle spotlessCheck

Do not use alternative formatters. Google Java Format is the team standard and all Java code must be formatted with it.

Import Ordering

Spotless handles import ordering automatically when using Google Java Format. The standard grouping is:

  1. com.* -- third-party packages
  2. java.* -- standard library
  3. javax.* / jakarta.* -- extension libraries
  4. Static imports (last)

Each group is separated by a blank line. Unused imports are removed automatically.

Linter: Checkstyle

Checkstyle enforces coding standards and style rules. The project uses the Google ruleset as a baseline with project-specific customizations.

Gradle Configuration

plugins {
    id 'checkstyle'
}

checkstyle {
    toolVersion = '10.12.1'
    configFile = file('src/main/resources/app/quality/checkstyle-google-ruleset.xml')
    maxWarnings = 0
    maxErrors = 0
}

Usage

# Run Checkstyle on main sources
gradle checkstyleMain

# Run Checkstyle on test sources
gradle checkstyleTest

Key Rules Enforced

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

Static Analysis: PMD

PMD detects potential bugs, dead code, suboptimal code, and overly complicated expressions.

Gradle Configuration

plugins {
    id 'pmd'
}

pmd {
    toolVersion = '7.1.0'
    ruleSetFiles = files('src/main/resources/app/quality/pmd-custom-ruleset.xml')
    consoleOutput = true
}

Usage

# Run PMD analysis
gradle pmdMain
gradle pmdTest

Security Analysis: SpotBugs

SpotBugs detects potential security vulnerabilities and bug patterns in compiled bytecode.

Gradle Configuration

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')
}

Usage

# Run SpotBugs on main sources
gradle spotbugsMain

# Run SpotBugs on test sources
gradle spotbugsTest

Editor Configuration

IntelliJ IDEA

  1. Install the google-java-format plugin.
  2. Enable it in Settings > google-java-format Settings > Enable google-java-format.
  3. Enable Settings > Editor > General > Auto Import > Optimize imports on the fly.
  4. Install the Checkstyle-IDEA plugin and point it to the project's ruleset.

Visual Studio Code

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"
    }
}

References