Maven Checkstyle 不允许导入

Maven Checkstyle not allowing import

我想了解为什么我的 checkstyle <allow> 规则没有被 Maven 遵守并收到错误:src\main\java\com\jon\ttf\adapter\persistence\BaseSqlPersistence.java:[4,1] (imports) ImportControl: Disallowed import - org.sql2o.Sql2o。我删除了 import-control-error.xml 中的大部分代码,因为它们都非常相似。问题可能出在 checkstyle-checks.xml 某处,其中某项检查覆盖了我的 org.sql2o.Sql2o 包吗?

如果您想让我附加更多代码,请告诉我

导入控制-error.xml

<import-control pkg="com.jon.ttf">
    <disallow pkg=".*" regex="true"/>
    <allow pkg="org.sql2o.Sql2o"/>
    <allow pkg="spark"/>
</import-control>

checkstyle-checks.xml

<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
        "-//Puppy Crawl//DTD Check Configuration 1.3//EN"
        "http://www.puppycrawl.com/dtds/configuration_1_3.dtd">

<module name="Checker">

    <module name="SuppressWarningsFilter"/>

    <property name="charset" value="UTF-8"/>

    <property name="severity" value="warning"/>

    <property name="fileExtensions" value="java, properties, xml"/>

    <module name="TreeWalker">
        <module name="RegexpSinglelineJava">
            <property name="format" value="^\t* "/>
            <property name="message" value="Indent must use tab characters"/>
            <property name="ignoreComments" value="true"/>
        </module>

        <module name="SuppressWarningsHolder"/>
        <module name="UnusedImports">
            <property name="severity" value="error"/>
        </module>
        <module name="OuterTypeFilename"/>
        <module name="ImportControl">
            <property name="severity" value="error"/>
            <property name="file" value="import-control-error.xml"/>
        </module>
        <module name="IllegalTokenText">
            <property name="tokens" value="STRING_LITERAL, CHAR_LITERAL"/>
            <property name="format"
                      value="\u00(08|09|0(a|A)|0(c|C)|0(d|D)|22|27|5(C|c))|\(0(10|11|12|14|15|42|47)|134)"/>
            <property name="message" value="Avoid using corresponding octal or Unicode escape."/>
        </module>
        <module name="AvoidEscapedUnicodeCharacters">
            <property name="allowEscapesForControlCharacters" value="true"/>
            <property name="allowByTailComment" value="true"/>
            <property name="allowNonPrintableEscapes" value="true"/>
        </module>
        <module name="LineLength">
            <property name="severity" value="error"/>
            <property name="max" value="300"/>
            <property name="ignorePattern" value="^package.*|^import.*|a href|href|http://|https://|ftp://"/>
        </module>
        <module name="AvoidStarImport">
            <property name="severity" value="error"/>
            <property name="allowStaticMemberImports" value="true"/>
        </module>
        <module name="OneTopLevelClass"/>
        <module name="NoLineWrap"/>
        <module name="EmptyBlock">
            <property name="option" value="TEXT"/>
            <property name="tokens" value="LITERAL_TRY, LITERAL_FINALLY, LITERAL_IF, LITERAL_ELSE, LITERAL_SWITCH"/>
        </module>
        <module name="NeedBraces">
            <property name="severity" value="error"/>
        </module>
        <module name="LeftCurly"/>
        <module name="RightCurly">
            <property name="option" value="alone"/>
            <property name="tokens"
                      value="CLASS_DEF, METHOD_DEF, CTOR_DEF, LITERAL_FOR, LITERAL_WHILE, LITERAL_DO, STATIC_INIT, INSTANCE_INIT"/>
        </module>
        <module name="EqualsHashCode">
            <property name="severity" value="error"/>
        </module>
        <module name="WhitespaceAround">
            <property name="severity" value="error"/>
            <property name="allowEmptyConstructors" value="true"/>
            <property name="allowEmptyMethods" value="true"/>
            <property name="allowEmptyTypes" value="true"/>
            <property name="allowEmptyLoops" value="true"/>
            <message key="ws.notFollowed"
                     value="WhitespaceAround: ''{0}'' is not followed by whitespace. Empty blocks may only be represented as '{}' when not part of a multi-block statement (4.1.3)"/>
            <message key="ws.notPreceded"
                     value="WhitespaceAround: ''{0}'' is not preceded with whitespace."/>
        </module>
        <module name="OneStatementPerLine">
            <property name="severity" value="error"/>
        </module>
        <module name="MultipleVariableDeclarations">
            <property name="severity" value="error"/>
        </module>
        <module name="ArrayTypeStyle">
            <property name="severity" value="error"/>
        </module>
        <module name="MissingSwitchDefault">
            <property name="severity" value="error"/>
        </module>
        <module name="FallThrough">
            <property name="severity" value="error"/>
        </module>
        <module name="UpperEll">
            <property name="severity" value="error"/>
        </module>
        <module name="EmptyLineSeparator">
            <property name="severity" value="error"/>
            <property name="allowNoEmptyLineBetweenFields" value="true"/>
        </module>
        <module name="SeparatorWrap">
            <property name="severity" value="error"/>
            <property name="tokens" value="DOT"/>
            <property name="option" value="nl"/>
        </module>
        <module name="SeparatorWrap">
            <property name="severity" value="error"/>
            <property name="tokens" value="COMMA"/>
            <property name="option" value="EOL"/>
        </module>
        <module name="PackageName">
            <property name="severity" value="error"/>
            <property name="format" value="^[a-z]+(\.[a-z][a-z0-9]*)*$"/>
            <message key="name.invalidPattern"
                     value="Package name ''{0}'' must match pattern ''{1}''."/>
        </module>
        <module name="TypeName">
            <property name="severity" value="error"/>
            <message key="name.invalidPattern"
                     value="Type name ''{0}'' must match pattern ''{1}''."/>
        </module>
        <module name="MethodTypeParameterName">
            <property name="severity" value="error"/>
            <property name="format" value="(^[A-Z][0-9]?)$|([A-Z][a-zA-Z0-9]*[T]$)"/>
            <message key="name.invalidPattern"
                     value="Method type name ''{0}'' must match pattern ''{1}''."/>
        </module>
        <module name="InterfaceTypeParameterName">
            <property name="severity" value="error"/>
            <property name="format" value="(^[A-Z][0-9]?)$|([A-Z][a-zA-Z0-9]*[T]$)"/>
            <message key="name.invalidPattern"
                     value="Interface type name ''{0}'' must match pattern ''{1}''."/>
        </module>
        <module name="NoFinalizer">
            <property name="severity" value="error"/>
        </module>
        <module name="GenericWhitespace">
            <property name="severity" value="error"/>
            <message key="ws.followed"
                     value="GenericWhitespace ''{0}'' is followed by whitespace."/>
            <message key="ws.preceded"
                     value="GenericWhitespace ''{0}'' is preceded with whitespace."/>
            <message key="ws.illegalFollow"
                     value="GenericWhitespace ''{0}'' should followed by whitespace."/>
            <message key="ws.notPreceded"
                     value="GenericWhitespace ''{0}'' is not preceded with whitespace."/>
        </module>
        <module name="Indentation">
            <property name="severity" value="error"/>
            <property name="basicOffset" value="8"/>
            <property name="braceAdjustment" value="0"/>
            <property name="caseIndent" value="8"/>
            <property name="throwsIndent" value="8"/>
            <property name="lineWrappingIndentation" value="8"/>
            <property name="arrayInitIndent" value="16"/>
        </module>
        <module name="MethodName">
            <property name="severity" value="error"/>
            <property name="format" value="^[a-z][a-z0-9][a-zA-Z0-9_]*$"/>
            <message key="name.invalidPattern" value="Method name ''{0}'' must match pattern ''{1}''."/>
        </module>
        <module name="EmptyCatchBlock">
            <property name="severity" value="error"/>
        </module>
    </module>

</module>

am getting the error: BaseSqlPersistence.java:[4,1] (imports) ImportControl: Disallowed import - org.sql2o.Sql2o

import-control-error.xml

<disallow pkg=".*" regex="true"/>
<allow pkg="org.sql2o.Sql2o"/>

ImportControl 的规则顺序与 XML 文件中定义的顺序相同。由于首先定义了 .*,这意味着不允许所有导入。如果您删除它或将它移到底部,那么它应该会按预期工作。

如果您试图禁止所有不允许的导入,那么这是 ImportControl 的默认行为,不需要特定的禁止。

您可以在 http://checkstyle.sourceforge.net/config_imports.html#ImportControl_Description

找到更多信息

Order of rules in the same subpackage/root are defined by the order of declaration in the XML file, which is from top (first) to bottom (last).

最后一点,

<allow pkg="org.sql2o.Sql2o"/>

这是 class,不是包裹。如果允许包,则必须将其定义为 org.sql2o。如果允许 class,则必须将其定义为 <allow class="org.sql2o.Sql2o"/>