Maven Checkstyle不允许导入

时间:2019-02-08 19:41:30

标签: maven checkstyle

我试图理解为什么Maven不遵守我的checkstyle <allow>规则,并得到错误: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包进行了某些检查吗?

让我知道是否要我附加更多代码

import-control-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>

1 个答案:

答案 0 :(得分:3)

  

出现错误:BaseSqlPersistence.java:[4,1](导入)ImportControl:不允许导入-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

中找到更多信息。
  

同一子包/根中的规则顺序由XML文件中的声明顺序定义,即从顶部(第一个)到底部(最后一个)。

作为最后的提示

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

这是一个类,而不是包。如果允许该软件包,则必须将其定义为org.sql2o。如果您允许该类,则必须将其定义为<allow class="org.sql2o.Sql2o"/>