nathaner
nathaner

Reputation: 553

google-java-format: format whole project

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)?

Upvotes: 3

Views: 3053

Answers (3)

silver_mx
silver_mx

Reputation: 1382

You can download the executable (google-java-format-[version]-all-deps.jar) JAR from the github page and execute:

java -jar google-java-format-[version]-all-deps.jar --replace /your-project/**/*.java

This will format all your Java files directly, but the formatted support other options. If you want to see all supported options you can do:

java -jar google-java-format-[version]-all-deps.jar --help

Upvotes: 0

Chris Walker
Chris Walker

Reputation: 21

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.

Upvotes: 2

DwB
DwB

Reputation: 38290

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.

Upvotes: 1

Related Questions