Maven checkstyle 插件:原因:com.puppycrawl.tools.checkstyle.api.CheckstyleException

Maven checkstyle plugin: Caused by: com.puppycrawl.tools.checkstyle.api.CheckstyleException

我正在尝试使用 gradle(在 java 11 中)项目配置 checkstyle 插件,如下所示:

checkstyle {
    toolVersion = '8.2'
    configFile = rootProject.file('config/checkstyle/checkstyle.xml')
    configProperties = [
            'checkstyle.cache.file': "${buildDir}/checkstyle.cache",
    ]
    ignoreFailures = true
    showViolations = true
}

我的checkstyle.xml是这样的:

<?xml version="1.0"?>

<code_scheme name="CustomStyle" version="1">
  <AndroidXmlCodeStyleSettings>
    <option name="USE_CUSTOM_SETTINGS" value="true"/>
    <option name="LAYOUT_SETTINGS">
      <value>
        <option name="INSERT_BLANK_LINE_BEFORE_TAG" value="false"/>
      </value>
    </option>

......

我得到以下异常

Caused by: com.puppycrawl.tools.checkstyle.api.CheckstyleException: unable to parse configuration stream - Document is invalid: no grammar found.:3:13 at com.puppycrawl.tools.checkstyle.ConfigurationLoader.loadConfiguration(ConfigurationLoader.java:441) at com.puppycrawl.tools.checkstyle.ConfigurationLoader.loadConfiguration(ConfigurationLoader.java:386) at com.puppycrawl.tools.checkstyle.ant.CheckstyleAntTask.createRootModule(CheckstyleAntTask.java:407) ... 106 more Caused by: org.xml.sax.SAXParseException; systemId: file:/home/workspace/service/config/checkstyle/checkstyle.xml; lineNumber: 3; columnNumber: 13; Document is invalid: no grammar found.

有解决此问题的建议吗?

您需要定义要检查的内容;例如:

<!DOCTYPE module PUBLIC
    "-//Checkstyle//DTD Checkstyle Configuration 1.3//EN"
    "https://checkstyle.org/dtds/configuration_1_3.dtd">

应该是 these .dtd files.

之一

My checkstyle.xml is like as follows:
<code_scheme name="CustomStyle" version="1">

这不是 checkstyle 的正确标记。 Checkstyle 配置如下:

<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
          "-//Puppy Crawl//DTD Check Configuration 1.3//EN"
          "https://checkstyle.org/dtds/configuration_1_3.dtd">

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

    <module name="TreeWalker">
    </module>
</module>

您可以阅读 https://checkstyle.sourceforge.io/config.html 了解更多信息。

看起来你给它的是一个 IntelliJ 配置文件。无论哪种方式,它都与 checkstyle 不兼容。