java_format_check

This script will check if the formatting of the *.java files in the given directory ($PWD if not given) and its subdirectories adhere to the Google Java Style.

This script’s exit code is 0 if all *.java files adhere to Google Java Style or 1 if not.

The following parameters are optional:

v

display the paths of the files whose formatting does not adhere to Google Java Style

Both module-info.java and package-info.java are checked as well.

This script needs internet access if it does not find the cached JAR file.

It will download and cache the google-java-format JAR.

The JAR is cached in the following location (in order of preference):

  • $GOOGLE_JAVA_FORMAT_HOME if $GOOGLE_JAVA_FORMAT_HOME is set

  • $XDG_CACHE_HOME/googlejavaformat if $XDG_CACHE_HOME is set

  • $HOME/Library/Application Support/Google/googlejavaformat on MacOS

  • $HOME/.m2/repository/com/google/googlejavaformat-all-deps

If you are using Gradle or Maven you might want to use Spotless instead of this script:

Gradle
plugins {
  id("com.diffplug.spotless") version "..."
}

spotless {
  java {
    googleJavaFormat()
  }
}
Maven
<plugin>
  <groupId>com.diffplug.spotless</groupId>
  <artifactId>spotless-maven-plugin</artifactId>
  <version>...</version>
  <configuration>
...
    <java>
      <googleJavaFormat/>
    </java>
  </configuration>
</plugin>

If you are using a JetBrains IDE you might want to use the google-java-format plugin.

Usage

$ scripts/java/java_format_check.sh
$ scripts/java/java_format_check.sh /tmp/example/src/main/java
$ echo $?
1
$ scripts/java/java_format_check.sh -v /tmp/example/src/main/java
/tmp/example/src/main/java/Example.java
$ echo $?
1
$ scripts/java/java_format.sh /tmp/example/src/main/java
$ scripts/java/java_format_check.sh /tmp/example/src/main/java
$ echo $?
0

Prerequisites