我正在尝试让 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
现在,不可能在中间没有空行的情况下分别对 java
和 javax
进行排序,这就是违规的原因。
我提出了 issue on GitHub要解决这个问题,需要更改 Checkstyle 代码。
作为解决方法,您可以在 IntelliJ IDEA 配置中的 javax
和 java
之间添加空行,然后应该很容易调整 Checkstyle 以使用它。
关于java - 如何让 Checkstyle CustomImportOrder 正确地与 IntelliJ 一起工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32690907/