IntelliJ IDEA 配置 Google Java Format 插件指南
Google Java Format 是一个强大的 Java 代码格式化工具,能够自动将 Java 源代码重新格式化为符合 Google Java 编码规范的标准格式。在 IntelliJ IDEA 中配置 Google Java Format 插件,可以让您的开发工作更加高效,确保团队代码风格的一致性。
插件安装步骤
从插件市场安装
打开 IntelliJ IDEA,进入 File → Settings → Plugins (macOS: IntelliJ IDEA → Preferences → Plugins),在 Marketplace 中搜索 "google-java-format",点击 Install 按钮完成安装。
手动安装
如果无法通过 Marketplace 安装,您可以从仓库下载插件文件手动安装。
插件配置详解
启用格式化功能
安装完成后,插件默认是禁用状态。需要手动启用:
- 进入
File → Settings → Other Settings → google-java-format Settings - 勾选 "Enable google-java-format" 复选框
- 选择代码风格(Google Style 或 AOSP Style)
JRE 配置要求
由于插件使用了 JDK 内部类,需要在 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 =jdk/com=-UNNAMED =jdk/com=-UNNAMED

