google-java-format

Ori / 2023-05-12 / 原文

统一代码格式化

项目代码通常是一个团队共同完成,要保障代码质量的首要前提就是统一代码的风格。统一代码风格的第一条就是统一代码格式化。

不同的人提交的代码格式化不一样将导致 merge 代码造成大概率冲突,而统一的代码风格无论对项目的可维护性,还是降低 merge 冲突都极为重要。

 

广泛使用的两种 Java 代码规范:

Google Java Style Guide

Alibaba Java Coding Guidelines

 

这里介绍两个代码格式化工具:

  • IDE插件:google-java-format
  • Maven插件:git-code-format-maven-plugin

google-java-format

google-java-format 源码

google-java-format插件可用于重新格式化 Java 源代码。启用后,它将替换通常的Reformat Code操作。该操作可以通过Code->Reformat Code触发,也可以使用Ctrl+Alt+L触发。

使用快捷键Shift+Ctrl+Alt+L然后勾选Optimize importsCtrl+Alt+L还将会触发Optimize imports

IDEA 中使用 google-java-format 插件

File->Settings->Plugins中下载插件。

从1.16.0版本起,google-java-format插件支持在优化导入时使用。

File->Settings->google-java-format Settings中勾选Enable google-java-format为当前项目启用google-java-format插件。

File->New Peojects Setup->Seeting for New Projects->Other Settings->google-java-format Settings中勾选Enable google-java-format为新项目启用google-java-format插件。

google-java-format 插件使用了一些内部类,需要一些额外的配置才可用。

Help->Edit Custom VM Options...中加入以下设置

--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED
--add-exports=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED
--add-exports=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED
--add-exports=jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED
--add-exports=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED
--add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED

完成后重启 IDE 即可。

git-code-format-maven-plugin

git-code-format-maven-plugin 源码

自动部署google-java-format代码格式化程序作为预提交 git 钩子的插件。

在 Maven 中使用 git-code-format-maven-plugin 插件

将以下代码添加到pom.xml文件中

pom.xml
<properties>
    <git-code-format-maven-plugin.version>4.2</git-code-format-maven-plugin.version>
</properties>

<build>
    <plugins>
        <plugin>
            <groupId>com.cosium.code</groupId>
            <artifactId>git-code-format-maven-plugin</artifactId>
            <version>${git-code-format-maven-plugin.version}</version>
            <executions>
                <!-- Format code during validate phase -->
                <execution>
                    <id>formatter-hook</id>
                    <phase>validate</phase>
                    <goals>
                        <goal>format-code</goal>
                    </goals>
                </execution>
                <!-- On commit, format the modified files -->
                <execution>
                    <id>install-formatter-hook</id>
                    <goals>
                        <goal>install-hooks</goal>
                    </goals>
                </execution>
                <!-- On Maven verify phase, fail if any file
                    (including unmodified) is badly formatted -->
                <execution>
                    <id>validate-code-format</id>
                    <goals>
                        <goal>validate-code-format</goal>
                    </goals>
                </execution>
            </executions>
            <dependencies>
                <dependency>
                    <groupId>com.cosium.code</groupId>
                    <artifactId>google-java-format</artifactId>
                    <version>${git-code-format-maven-plugin.version}</version>
                </dependency>
            </dependencies>
        </plugin>
    </plugins>
</build>

一起回顾一下 maven 构建生命周期

validate阶段,git-code-format:format-code格式化代码

initialize阶段,git-code-format:install-hooks安装一个gitpre-commit钩子

verify阶段,git-code-format:validate-code-format验证代码格式


使用命令格式化代码

mvn git-code-format:format-code -Dgcf.globPattern=**/*

使用命令验证代码格式

mvn git-code-format:validate-code-format -Dgcf.globPattern=**/*