LoginSignup
4
3

More than 1 year has passed since last update.

【google-java-format】すべてのファイルを一発でフォーマットする方法

Last updated at Posted at 2023-02-06

はじめに

google-java-formatは、Javaのソースコードをフォーマットするプログラムです。
Google Java Styleに基づいて、フォーマットします。

IntelliJやEclipseなどのプラグインもありますが、ここではコマンドラインツールを利用します。

google-java-formatには、さまざまなオプションがありますが、
「すべてのファイルを一発でフォーマットする」ためのオプションはありません。

オプションの一覧
オプションの一覧
Usage: google-java-format [options] file(s)

Options:
  -i, -r, -replace, --replace
    Send formatted output back to files, not stdout.
  -
    Format stdin -> stdout
  --assume-filename, -assume-filename
    File name to use for diagnostics when formatting standard input (default is <stdin>).
  --aosp, -aosp, -a
    Use AOSP style instead of Google Style (4-space indentation).
  --fix-imports-only
    Fix import order and remove any unused imports, but do no other formatting.
  --skip-sorting-imports
    Do not fix the import order. Unused imports will still be removed.
  --skip-removing-unused-imports
    Do not remove unused imports. Imports will still be sorted.
  --skip-reflowing-long-strings
    Do not reflow string literals that exceed the column limit.
  --skip-javadoc-formatting
    Do not reformat javadoc.
  --dry-run, -n
    Prints the paths of the files whose contents would change if the formatter were run normally.
  --set-exit-if-changed
    Return exit code 1 if there are any formatting changes.
  --lines, -lines, --line, -line
    Line range(s) to format, like 5:10 (1-based; default is all).
  --offset, -offset
    Character offset to format (0-based; default is all).
  --length, -length
    Character length to format.
  --help, -help, -h
    Print this usage statement.
  --version, -version, -v
    Print the version.
  @<filename>
    Read options and filenames from file.

If -i is given with -, the result is sent to stdout.
The --lines, --offset, and --length flags may be given more than once.
The --offset and --length flags must be given an equal number of times.
If --lines, --offset, or --length are given, only one file (or -) may be given.

google-java-format: Version 1.15.0
https://github.com/google/google-java-format

そのなかで、どうすれば実現できるのかを記事に書いていきます。

実現方針

google-java-formatでは、引数で「対象のファイル」を指定します。

対象ファイルの指定の例
 java -jar /path/google-java-format-1.15.0-all-deps.jar --replace src\SampleApplication.java src\SampleService.java

コマンドで「対象のファイル」を取得し、引数に渡すことで実現します。

Gitで管理されていることを前提とし、ここではGitのコマンドを使用します。(git ls-files
Gitで管理されていなくても、OSのコマンドを使用すれば、同様に実現できるはずです。(dir, ls, find)

つぎのIssueに着想を得ました。
google/google-java-format | Glob support google-java-format [options] "**/*.java"

実現方法

Windows | PowerShell

つぎのコマンドで実現できます。

java -jar /path/google-java-format-1.15.0-all-deps.jar --replace $(git ls-files *.java)

Windows | Gradle

つぎのタスクで実現できます。

task googleJavaFormat(group: 'format', type: Exec) {
    commandLine 'Powershell', '-C', '"iex \'java -jar /path/google-java-format-1.15.0-all-deps.jar --replace $(git ls-files *.java)\'"'
}

Mac | Gradle

つぎのタスクで実現できます。

task googleJavaFormat(group: 'format', type: Exec) {
    commandLine 'zsh', '-c', 'java -jar /path/google-java-format-1.15.0-all-deps.jar --replace $(git ls-files "*.java")'
}
4
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
3