﻿# 如何套用 Google Java Style Guide 在不同的 Java 開發工具之間

知名的 [Google Java Style Guide](https://google.github.io/styleguide/javaguide.html) 是許多 Java 開發人員遵循 Java 程式碼撰寫風格的指引之一，該指引也定義了一組格式化定義檔，其中包含了 [Eclipse](https://www.eclipse.org/)、[IntellJ IDEA](https://www.jetbrains.com/idea/) 與其他 IDE 的整合，可以幫助你跨越開發工具之間的格式化差異，讓不同人使用不同 Java 開發工具，也能統一基礎 Java 程式碼的撰寫風格。

### Eclipse

你可以在 [Eclipse](https://www.eclipse.org/) 或 [Spring Tool Suite 4](https://spring.io/tools) (`STS4`) 開發工具中套用 [eclipse-java-google-style.xml](https://google.github.io/styleguide/eclipse-java-google-style.xml) 格式化設定。

以下是設定步驟：

1.  下載 [eclipse-java-google-style.xml](https://google.github.io/styleguide/eclipse-java-google-style.xml) 格式設定檔
    
2.  點選主選單的 `Window` > `Preferences` 開啟 **Preferences** 視窗
    
3.  點選左側頁籤到 `Java` > `Code Style` > `Formatter` 並點擊 `Import` 按鈕
    
    ![image](https://stwillblogassets.blob.core.windows.net/files/images/external/user-images.githubusercontent.com/799508da0e7a49f306ab-188254670-bf8c877e-204b-4365-84c0-aa02fd92618f.png)
    
4.  選取 `eclipse-java-google-style.xml` 檔案，匯入 `GoogleStyle` 格式設定檔，按下 `Apply and Close` 套用設定即可
    
    ![image](https://stwillblogassets.blob.core.windows.net/files/images/external/user-images.githubusercontent.com/13088be11131121120ee-188254689-c74cda23-31c1-4e85-9209-6ad802a39b31.png)
    

### Visual Studio Code

在 [Visual Studio Code](http://code.visualstudio.com/) 裡面開發 Java 通常會安裝 [Language Support for Java(TM) by Red Hat](https://marketplace.visualstudio.com/items?itemName=redhat.java) 擴充套件，參考它的 [Formatter settings](https://github.com/redhat-developer/vscode-java/wiki/Formatter-settings) 文件說明，該擴充套件其實已經內建了 Java 格式化工具，並且可以直接套用 Google Java Style 的 Eclipse 格式化定義檔([eclipse-java-google-style.xml](https://google.github.io/styleguide/eclipse-java-google-style.xml))。

你只要將以下內容加入到**使用者設定**或**工作區設定**即可套用 Google 官方建議的格式化設定，以下是設定步驟：

```json
{
    "[java]": {
        "editor.defaultFormatter": "redhat.java",
        "editor.formatOnSave": true
    },
    "java.format.enabled": true,
    "java.format.onType.enabled": true,
    "java.format.settings.url": "https://raw.githubusercontent.com/google/styleguide/gh-pages/eclipse-java-google-style.xml",
    "java.format.settings.profile": "GoogleStyle"
}
```

> 由於 [eclipse-java-google-style.xml](https://google.github.io/styleguide/eclipse-java-google-style.xml) 檔案中包含一組名為 `GoogleStyle` 的的設定。如果你下載該檔案回來，就可以添加更多不同的設定，但請記得修改 `java.format.settings.url` 路徑，指向放在專案中的相對連結，並且在 `java.format.settings.profile` 設定中調整 Profile 名稱即可。

### IntellJ IDEA

你可以在 [IntellJ IDEA](https://www.jetbrains.com/idea/) 開發工具套用 [intellij-java-google-style.xml](https://google.github.io/styleguide/intellij-java-google-style.xml) 格式化設定。該設定將會與 Eclipse 的 [eclipse-java-google-style.xml](https://raw.githubusercontent.com/google/styleguide/gh-pages/eclipse-java-google-style.xml) 格式設定完全相同。

以下是設定步驟：

1.  下載 [intellij-java-google-style.xml](https://google.github.io/styleguide/intellij-java-google-style.xml) 格式設定檔
    
2.  按下 `Ctrl+Alt+S` 開啟 `Settings` 視窗，展開到 `Editor` > `Code Style` > `Java`
    
3.  執行 `Scheme` > `Import Scheme` > `IntelliJ IDEA code style XML` (如下圖示)
    
    ![image](https://stwillblogassets.blob.core.windows.net/files/images/external/user-images.githubusercontent.com/dafd891b04fb70dcc3ee-188253586-36f9e4e6-120e-4711-a6a0-8a2cf9778389.png)
    
4.  選擇 `intellij-java-google-style.xml` 設定檔，並按下 `OK` 以 `GoogleStyle` 這個名稱儲存
    
    ![image](https://stwillblogassets.blob.core.windows.net/files/images/external/user-images.githubusercontent.com/e6608c5165782b048b1a-188253662-41e28a0f-f3bf-4131-9a51-65d7a46b0905.png)
    
5.  最後按下 `OK` 套用設定即可
    
    ![image](https://stwillblogassets.blob.core.windows.net/files/images/external/user-images.githubusercontent.com/c318896db08dfdbc49a5-188253691-5a06e4fd-0c69-4c23-afcf-24edb67df7fe.png)
    

### 如何更方便的在「專案層級」共用格式化設定

由於 Java 的開發工具很多，如果我們想在團隊中共用格式化設定，最方便的方式還是將格式化設定檔放入專案中，並將設定加入版控，藉此自動統一所有團隊成員的格式設定。

首先，在 Visual Studio Code 的 [Language Support for Java(TM) by Red Hat](https://marketplace.visualstudio.com/items?itemName=redhat.java) 擴充套件有一個方法，可以讓你直接讀取 STS4 / Eclipse 常用的 Project-specific (專案層級) 格式化設定檔，讓這些設定跟著專案走，加入版控，讓開發團隊自動統一風格，這點非常方便。

你只要下載 [.settings/org.eclipse.jdt.core.prefs](https://gist.github.com/fbricon/30c5971f7e492c8a74ca2b2d7a7bb966) 檔案，並且儲存到專案根目錄下的 `.settings` 目錄下即可。

不過，而且這個動作在 Eclipse 操作會比較方便，其設定步驟如下：

1.  下載 [eclipse-java-google-style.xml](https://google.github.io/styleguide/eclipse-java-google-style.xml) 格式設定檔
    
2.  點擊專案節點，按下滑鼠右鍵，選擇 `Properties`
    
3.  切換到 `Java Code Style` > `Formatter` 頁籤
    
4.  勾選 `Enable project specific settings` 並點擊 `Import` 按鈕
    
5.  選取 `eclipse-java-google-style.xml` 檔案，匯入 `GoogleStyle` 格式設定檔，按下 `Apply and Close` 套用設定即可
    
    ![image](https://stwillblogassets.blob.core.windows.net/files/images/external/user-images.githubusercontent.com/da29e50c1a59b7df5f17-188255450-5274d36e-3fe1-4841-8a55-c5a096b884b3.png)
    

上述步驟會自動在專案下建立一個 `.settings/org.eclipse.jdt.core.prefs` 檔案，這裡儲存的就是你在 Eclipse 的專案設定，記得要將該檔案加入版控。

> 注意: 如果 Visual Studio Code 偵測到有 `.settings/org.eclipse.jdt.core.prefs` 檔案存在，原有的 Java 格式化設定就會停用，自動採用 [.settings/org.eclipse.jdt.core.prefs](https://gist.github.com/fbricon/30c5971f7e492c8a74ca2b2d7a7bb966) 宣告的格式化設定！

然而 [IntellJ IDEA](https://www.jetbrains.com/idea/) 開發工具並沒有與其他工具共用格式化設定的功能，因此你必須自行管理專案層級的格式化設定。重新匯入一次 `GoogleStyle` 設定，記得參考下圖選擇 `Project` 範圍即可：

![image](https://stwillblogassets.blob.core.windows.net/files/images/external/user-images.githubusercontent.com/8a6a5ab0a25def50cf30-188255515-d1b87e7f-6bca-47cc-b25e-937bff15db97.png)

匯入成功後，會自動建立一個 `.idea\codeStyles\codeStyleConfig.xml` 檔案，記得加入到版控之中。

### 總結

其實程式碼撰寫風格與排版一直都是非常個人且主觀的感受，每個人對於「美」的定義都不盡相同，因此要在團隊中吵什麼格式才漂亮，永遠吵不完！

現在我們可以看到的各種不同的程式語言間，只要有人想要出來主導撰寫風格，其發展趨勢大多都是非常武斷的(opinionated)，甚至於完全不提供風格的客製化設定也屢見不鮮。像是 [Prettier](https://prettier.io/) 這套格式化工具，其副標題就是 **Opinionated Code Formatter**，只提供相當少量的客製化選項，試圖不讓格式這件事過於發散，你不想用可以完全不用。

而像是 [Google Java Style Guide](https://google.github.io/styleguide/javaguide.html) 就是 Google 跳出來主導 Java 的撰寫風格，並且給你一個 Google 官方的預設值。本文介紹的格式化方法，還是有 `300+` 項格式化的細節可以調整，但我還是建議你乾脆就不要改了，直接用下去就好，這樣大家都沒有爭議，也不會日後吵成一團。😅

### 相關連結

-   [Google Java Style Guide](https://google.github.io/styleguide/javaguide.html)
-   [Google Style Guides](https://github.com/google/styleguide/) (Repo)
-   [Formatter settings · redhat-developer/vscode-java Wiki](https://github.com/redhat-developer/vscode-java/wiki/Formatter-settings)
