我有一个 checkstyle.xml 文件来检查格式违规。有没有办法提供 checkstyle.xml 作为 diffplug/spotless formatter 的输入。正在使用 Java8,完美的版本是 2.11.0 。如果使用spotless::apply 可以做到这一点,我们就可以删除所有样式违规。Maven 版本是 3.6.3 。目前正在使用 google-java-format 。
code-formatting checkstyle maven-checkstyle-plugin google-java-format spotless
我正在尝试配置 Maven Spring Boot 应用程序以使用具有 google java 格式样式的一尘不染。我也希望 Intellij 像 Maven 插件一样格式化代码。
但我注意到应用的格式略有不同。
举个例子:Intellij不会改变下面两行
@Mock
private UserRepository userRepositoryMock;
Run Code Online (Sandbox Code Playgroud)
但是maven插件(使用命令:mvn Spotless:apply)将格式化如下:
@Mock private UserRepository userRepositoryMock;
Run Code Online (Sandbox Code Playgroud)
项目管理配置
<plugin>
<groupId>com.diffplug.spotless</groupId>
<artifactId>spotless-maven-plugin</artifactId>
<version>${spotless.version}</version>
<configuration>
<!-- optional: limit format enforcement to just the files changed by this feature branch -->
<ratchetFrom>origin/master</ratchetFrom>
<formats>
<!-- you can define as many formats as you want, each is independent -->
<format>
<!-- define the files to apply to -->
<includes>
<include>*.md</include>
<include>.gitignore</include>
</includes>
<!-- define the steps to apply …
Run Code Online (Sandbox Code Playgroud) 我正在寻找配置 google_checks 以4 spaces
在 Maven Checkstyle 插件中使用的方法。我将indentSize
配置参数设置为4,但是不起作用。有配置选项来设置这个吗?我不想拥有自己的版本,google_checks.xml
只是缩进 4 个空格。
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>3.1.1</version>
<dependencies>
<dependency>
<artifactId>checkstyle</artifactId>
<groupId>com.puppycrawl.tools</groupId>
<version>8.36.1</version>
</dependency>
</dependencies>
<configuration>
<configLocation>google_checks.xml</configLocation>
<indentSize>4</indentSize>
<failsOnError>true</failsOnError>
<consoleOutput>true</consoleOutput>
<includeTestSourceDirectory>true</includeTestSourceDirectory>
</configuration>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
Run Code Online (Sandbox Code Playgroud)
更新:似乎没有办法有一个与maven-checkstyle-plugin
,Checkstyle with google_checks
和兼容的单一格式Intellij with google_java_format
。有人能够实现这一目标吗?
我们根据自述文件将https://github.com/sherter/google-java-format-gradle-plugin连接到我们的项目中。
我们还连接了一个 pre-commit 钩子来在提交之前运行插件,这确保了更改列表中的所有代码在推送之前都已格式化,这避免了在我们运行verGJF
任务时 Jenkins 中的错误。
但是我们必须记住在运行goJF
之前在本地运行./gradlew build
,否则构建会因格式错误而失败。
我们通过为 IntelliJ添加https://plugins.jetbrains.com/plugin/8527-google-java-format和https://plugins.jetbrains.com/plugin/7642-save-actions插件来解决这个问题,启用google-java-format 插件,并配置 save-actions 插件以在保存时格式化。
但这是开发人员必须记住要经历的许多额外配置,而且这意味着他们无法在处理代码时按照他们想要的方式格式化代码,只能在构建或提交时重新格式化。
我们更喜欢全 Gradle 解决方案,以便goJF
任务在build
任务之前运行(并且在verGJF
任务之前运行,该任务已经build
通过 google-java-format Gradle 插件绑定到任务)。
我们无法弄清楚如何做到这一点。别人知道吗?
在 Java 11 下运行 Eclipse 时,使用google-java-format eclipse 插件效果很好,但在 Java 16+ 上运行时,它会失败并出现以下错误:
完整错误:A save participant caused problems. The save participant 'Code Clean Up' caused an exception: java.lang.IllegalAccessError: class com.google.googlejavaformat.java.JavaInput (in unnamed module @0x99c5646) cannot access class com.sun.tools.javac.parser.Tokens$TokenKind (in module jdk.compiler) because module jdk.compiler does not export com.sun.tools.javac.parser to unnamed module @0x99c5646. See the error log for details.
google-java-formatter确实注意到--add-exports
,在 JDK 16+ 上运行时,由于 JEP-396(JDK 内部的强封装),您需要在运行格式化程序时设置标志。不清楚的是如何判断--add-exports
Eclipse 插件的设置。
添加以下内容eclipse.ini
(或在我的情况下SpringToolSuite4.ini
)似乎没有帮助(更不用说它感觉不对,因为它不针对该特定插件)。有没有不同的方法来处理/解决这个问题?
-vmargs
-Dosgi.requiredJavaVersion=11
-Dosgi.dataAreaRequiresExplicitInit=true
-Xms256m
-Xmx2048m …
Run Code Online (Sandbox Code Playgroud) eclipse eclipse-plugin spring-tool-suite google-java-format java-16
我刚刚下载了 eclipse 2019-03 (4.11.0),并且需要安装 google-java-format 插件,根据说明我需要将 google-java-format Eclipse 插件放到 eclipse dropins 文件夹中,我环顾四周有没有这样的文件夹,通过谷歌周围我决定手动创建这个文件夹现在文件夹目录看起来像这样
/Users/ddd/eclipse/java-2019-03
/Eclipse.app
/dropins
google-java-format-eclipse-plugin_1.6.0.jar
Run Code Online (Sandbox Code Playgroud)
然后我重新启动 Eclipse 并尝试配置 java 代码格式化程序以使用 google java 格式:
似乎该插件没有被 Eclipse 拾取,我是否将 dropins 文件夹放在了错误的位置?