Goal #

Goal: Ensuring that the code formatting is consistent while working in different IDEs and should also match with build time formatting.

Readable code is one of the traits of well engineered software.


Overview of Spotless Autoformat setup at buildtime #

I use VsCode and here is my brief setup that enables Autoformat.

  • Formatting is enforced via Spotless plugin through build tools like gradle/maven.
    • But, this is during BUILD time.
    • Here is my sample gradle spotless config

      spotless {
        java {
              googleJavaFormat()
              importOrderFile(rootProject.file('importsorder'))
              removeUnusedImports()
              trimTrailingWhitespace()
        }
      }
      
    • importsorder

      # @author: sairaghava.katepally
      # DO NOT MAKE CHANGES TO THIS
      11=
      10=com.sairaghava.katepally
      9=com.srk
      8=lombok
      7=io
      6=com
      5=org
      4=org.springframework
      3=jakarta
      2=javax
      1=java
      0=#
      

Format Java Code from VSCode #

  • How about formatting code from IDE like on file save?
    • I am not using Spotless for this. Rather I am doing what spotless is doing internally
    • Here is my on save action in vscode

      "emeraldwalk.runonsave": {
          "commands": [
              {
              "match": "\\.java$",
              "cmd": "java -jar /home/srk/google-java-format-1.25.0-all-deps.jar \
              --skip-sorting-imports --replace ${file}"
              }
          ]
      }
      
    • settings.json

      "java.saveActions.organizeImports": true,
      "java.completion.importOrder": [
          "#",
          "java",
          "javax",
          "jakarta",
          "org.springframework",
          "org",
          "com",
          "io",
          "lombok",
          "app-sepecifc",
          ""
      ]
      
  • This will ensure that the formatting is done via IDE
    • Now
    • BUILD Tool formatting = IDE Formatting

    • .gradlew build or .gradlew spotlessApply = VScode On File Save


Format Java Code from Intellij #

This is not enough, as most of the Java Devs use Intellij it’s important to ensure that the formatting done through Intellij IDEA is inline with the above Vscode and Build time formatting.

  • Defined an .editorconfig. Read for intellij specific options

    [*]
    indent_style=space
    ident_size=2
    indent_use_tab=false
    end_of_line=lf
    charset=utf-8
    trim_trailing_whitespace=true
    insert_final_newline=true
    max_line_length=100
    
    # idea specific language agnostic settings
    ij_continuation_indent_size=2
    
    [*.java]
    ij_java_imports_layout=disabled
    
  • This still doesn’t guarantee the formatting as expected.
  • Here are my installed plugins, but File Watchers is want we want at this point
    • Install file watcher plugin
      • Install File Watcher Plugin
    • Create file watcher config
      • Create a File watcher config
        • For better performance, uncheck the 2 options from the above image

      • Next
        • Create a file watcher
    • Once enabled this should be binded to actions on save
      • intellij plugins
  • That’s it. Now, Intellij’s on save is just doing gradlew spotlessApply

Summary #

(Vscode Java Formatting) = (Intellij Java Formatting) = (Build Time Java Formatting)

This will ensure consistent code formatting across the 2 widely used IDEs in Java projects.