Portable Bash tooling for installing and using google-java-format.
The one-line installer downloads the versioned java-format.sh release asset to ~/.local/bin/java-format, then installs the latest google-java-format release for the current user.
這行指令會下載已發佈版本的 java-format.sh 到 ~/.local/bin/java-format,再為目前使用者安裝最新版的 google-java-format。
curl -fsSL https://github.com/samzhu/java-format-scripts/releases/latest/download/install.sh | bashThe installer saves the script at a permanent path because install-hook records that path in the Git pre-commit hook. Do not run the main formatter only from a temporary directory.
安裝程式會把腳本存放在固定路徑,因為 install-hook 會將該路徑寫入 Git pre-commit hook。請勿只從暫存目錄執行主腳本。
To install a particular google-java-format version instead of its latest release:
若不想安裝最新版,可在一鍵安裝指令最後指定 google-java-format 的版本:
curl -fsSL https://github.com/samzhu/java-format-scripts/releases/latest/download/install.sh | bash -s -- --version 1.35.0To install a fixed version of this script, replace latest with a release tag such as 1:
若要固定此工具本身的版本,請把 latest 改成 Release tag,例如 1:
curl -fsSL https://github.com/samzhu/java-format-scripts/releases/download/1/install.sh | bashcurl | bash executes remote code. Review the tagged release before running it in environments you do not control.
curl | bash 會直接執行遠端程式碼;在非自行控制的環境執行前,請先檢閱指定版本的 Release 內容。
The installer uses ~/.local/bin. If it is not already on your PATH, add it once for zsh:
安裝程式使用 ~/.local/bin。若此目錄尚未在 PATH 中,zsh 使用者可執行一次:
echo 'export PATH="$HOME/.local/bin:$PATH"' >> ~/.zshrc
source ~/.zshrcYou can then run java-format format from any directory.
完成後,就能在任何目錄直接執行 java-format format。
java-format install [--version <x.y.z>] [--force]
java-format format [path...]
java-format diff [--staged | --base <git-ref>]
java-format check [path...]
java-format install-hook
java-format uninstall-hookformatrecursively formats all*.javafiles below the supplied paths, excluding.git.diffformats every Java file in the unstaged Git diff;--stagedformats only index content and preserves any unstaged changes to the same file.checkexits nonzero if google-java-format would make a change.install-hooksafely preserves an existingpre-commithook and only updates staged Java blobs.uninstall-hookrestores the prior hook.
- macOS Apple Silicon, Linux x64/arm64, and Windows x64 through Git Bash/MSYS/WSL use official native binaries.
- Intel Mac and other unsupported architectures use the all-deps JAR and require a JDK 21+.
- The script defaults to the newest google-java-format release. It records the known version when the script was released at the top of
java-format.sh.
-
Shell comments explain design and safety decisions, such as platform fallback, the stable hook launcher, and preserving partial staging; they intentionally do not narrate every line of syntax.
-
Runtime output reports command scope, file counts, installation targets, and hook changes. Detailed formatter output is shown only for errors or files that need attention.
-
Keep
install.sh,README.md, and the Release assets in sync whenever changing the installer name, path, or Release tag. -
Shell 註解用於說明平台 fallback、穩定 hook launcher、保留部分暫存等設計與安全原因,不逐行解釋語法。
-
執行訊息會顯示命令範圍、檔案數量、安裝位置與 hook 變更;只有錯誤或需處理的檔案才會顯示 formatter 詳細輸出。
-
修改安裝程式名稱、路徑或 Release tag 時,必須同步更新
install.sh、README.md與 Release assets。
-
Update
RELEASE_TAGininstall.shto the new tag, then update the README's fixed-version example if needed. -
Commit and push the change, then create and push the tag.
-
Upload both shell files as assets with the same names:
gh release create 1 java-format.sh install.sh --title 1 --generate-notes
The latest installation URL works because GitHub provides a stable URL for a release asset with a fixed filename.