google-java-format: format whole project

1.9k Views Asked by At

I'm using google-java-format to format Java code according to Google Java Style. However, I only find documentation and examples showing how to format one file using the CLI.

Is there a built-in way to format an entire Java project directly using the CLI (without looping using a shell script or something else)?

2

There are 2 best solutions below

0
On

The answer above suggests using xargs, so here's the command that I used:

find my_java_project/ -name "*.java" -type f -print | xargs java -jar google-java-format-1.18.1-all-deps.jar --replace

...where my current working directory contains my Java project (in the my_java_project folder) and the Google Java Format JAR.

1
On

After a quick read of the google-java-format documentation, it is intended to function one file at a time or on a group of files, each listed on the command line.

There appears to be plugins for intelliJ and Eclipse.

If you need to format every file in your project, you will need to do one of the following:

  1. Feed a list of every file. This is fairly easy with a script; use xargs.
  2. If you use IntelliJ, check the plugin options. It likely has some kind of selection mechanism.
  3. As above, the Eclipse plugin likely has some kind of selection mechanism, as well.