java - 如何让 Checkstyle CustomImportOrder 正确地与 IntelliJ 一起工作?

标签 java maven intellij-idea checkstyle

我正在尝试让 Checkstyle(通过 maven-checkstyle-plugin)使用 Checkstyle CustomImportOrder 模块检查我的 IntelliJ 导入。尽管已根据 IntelliJ 的默认规则对我的导入进行排序,但 Checkstyle 仍然说导入顺序是错误的。

这是我的导入(根据 IntelliJ 规则 (ctrl+o) 排序):

import org.codehaus.jackson.JsonNode;

import javax.sql.rowset.serial.SQLOutputImpl;
import java.util.ArrayList;
import java.util.List;

这是来自 Checkstyle 的警告消息:

[WARNING] src\main\java\com\example\hej\EnKlass.java[5] (imports) CustomImportOrder: Import statement is in the wrong order. Should be in the 'SPECIAL_IMPORTS' group.
[WARNING] src\main\java\com\example\hej\EnKlass.java[6] (imports) CustomImportOrder: Import statement is in the wrong order. Should be in the 'STANDARD_JAVA_PACKAGE' group.
[WARNING] src\main\java\com\example\hej\EnKlass.java[7] (imports) CustomImportOrder: Import statement is in the wrong order. Should be in the 'STANDARD_JAVA_PACKAGE' group.

这是我的 checkstyle.xml CustomImportOrder 规则(Checkstyle 网站为 IntelliJ 推荐):

<module name="CustomImportOrder">
    <property name="customImportOrderRules" value="THIRD_PARTY_PACKAGE###SPECIAL_IMPORTS###STANDARD_JAVA_PACKAGE###STATIC"/>
    <property name="specialImportsRegExp" value="^javax\."/>
    <property name="standardPackageRegExp" value="^java\."/>
    <property name="sortImportsInGroupAlphabetically" value="true"/>
    <property name="separateLineBetweenGroups" value="false"/>
</module>

这可能是什么问题?我一直在尝试改变规则,但没有运气。我也曾尝试删除/操纵正则表达式,但没有成功。

最佳答案

IntelliJ 中的默认格式如下所示:

all other imports
<blank line>
javax.* in alphabetical order
java.* in alphabetical order
<blank line>
static imports in alphabetical order

现在,不可能在中间没有空行的情况下分别对 javajavax 进行排序,这就是违规的原因。

我提出了 issue on GitHub要解决这个问题,需要更改 Checkstyle 代码。

作为解决方法,您可以在 IntelliJ IDEA 配置中的 javaxjava 之间添加空行,然后应该很容易调整 Checkstyle 以使用它。

关于java - 如何让 Checkstyle CustomImportOrder 正确地与 IntelliJ 一起工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32690907/

相关文章:

java - 双向链表

java - 可以将父属性设置为最终属性,以使其不能被子属性覆盖

java - "Find usages"作为 IntelliJ 插件的功能

java - 如何强制intellij idea建议覆盖方法

java - Scala 泛型未在 IntelliJ 中正确转换为 Java

java - 使用 Java 更改/替换 PDF 中的文本

java - 无法在 Java 中刷新 DataOutputStream

java - 从 ColdFusion 启动测试 SMTP 服务器

maven - Travis CI 无法安装 openjdk11

java - mvn 发布 :prepare in spring boot 时无法将文件提交到 github