带有 google_checks 和 4 个空格 indentSize 的 Maven Checkstyle 插件

Nik*_*hil 6 checkstyle maven maven-checkstyle-plugin google-java-format

我正在寻找配置 google_checks 以4 spaces在 Maven Checkstyle 插件中使用的方法。我将indentSize配置参数设置为4,但是不起作用。有配置选项来设置这个吗?我不想拥有自己的版本,google_checks.xml只是缩进 4 个空格。

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-checkstyle-plugin</artifactId>
        <version>3.1.1</version>
        <dependencies>
          <dependency>
            <artifactId>checkstyle</artifactId>
            <groupId>com.puppycrawl.tools</groupId>
            <version>8.36.1</version>
          </dependency>
        </dependencies>
        <configuration>
          <configLocation>google_checks.xml</configLocation>
          <indentSize>4</indentSize>
          <failsOnError>true</failsOnError>
          <consoleOutput>true</consoleOutput>
          <includeTestSourceDirectory>true</includeTestSourceDirectory>
        </configuration>
        <executions>
          <execution>
            <phase>validate</phase>
            <goals>
              <goal>check</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
Run Code Online (Sandbox Code Playgroud)

更新:似乎没有办法有一个与maven-checkstyle-plugin,Checkstyle with google_checks和兼容的单一格式Intellij with google_java_format。有人能够实现这一目标吗?

Ser*_*nov 8

概述

\n

目前,Checkstyle 不支持此类配置组合。

\n

以下是一些相关的 GitHub 问题:

\n
    \n
  1. 如何扩展/覆盖现有配置(sun、google) \xc2\xb7 Issue #4484 \xc2\xb7 checkstyle/checkstyle

    \n
  2. \n
  3. 创建配置 \xc2\xb7 的继承/覆盖和组合/扩展的概念 Issue #2873 \xc2\xb7 checkstyle/checkstyle

    \n
      \n
    • 请注意,这是一个活跃的问题。
    • \n
    \n
  4. \n
\n

解决方法

\n

有一个非常简单的解决方法可以覆盖配置文件的某些检查:将单个 Checkstyle Maven 插件执行拆分为两个执行:

\n
    \n
  1. 创建使用整个配置文件的第一个执行并禁止覆盖检查。
  2. \n
  3. 创建第二个执行,该执行使用仅带有 \xc2\xaboverridden\xc2\xbb 检查的自定义配置文件。
  4. \n
\n

此处还解释了此解决方法:创建配置的继承/覆盖和组合/扩展的概念 \xc2\xb7 问题 #2873 \xc2\xb7 checkstyle/checkstyle注释

\n

缩进相关示例(草稿)

\n

项目:pom.xml

\n

插件/build/pluginManagement/plugins定义:

\n
<plugin>\n    <groupId>org.apache.maven.plugins</groupId>\n    <artifactId>maven-checkstyle-plugin</artifactId>\n    <version>3.1.2</version>\n    <dependencies>\n        <dependency>\n            <groupId>com.puppycrawl.tools</groupId>\n            <artifactId>checkstyle</artifactId>\n            <version>8.43</version>\n        </dependency>\n    </dependencies>\n</plugin>\n
Run Code Online (Sandbox Code Playgroud)\n

插件/build/plugins定义:

\n
<plugin>\n    <groupId>org.apache.maven.plugins</groupId>\n    <artifactId>maven-checkstyle-plugin</artifactId>\n    <executions>\n        <execution>\n            <id>check-google-checks</id>\n            <phase>validate</phase>\n            <goals>\n                <goal>check</goal>\n            </goals>\n            <configuration>\n                <configLocation>google_checks.xml</configLocation>\n                <suppressionsLocation>maven-checkstyle-suppressions-google_checks.xml</suppressionsLocation>\n                <suppressionsFileExpression>checkstyle.suppressions.file</suppressionsFileExpression>\n            </configuration>\n        </execution>\n        <execution>\n            <id>check-custom-checks</id>\n            <phase>validate</phase>\n            <goals>\n                <goal>check</goal>\n            </goals>\n            <configuration>\n                <configLocation>maven-checkstyle-custom_checks.xml</configLocation>\n            </configuration>\n        </execution>\n    </executions>\n    <configuration>\n        <failsOnError>true</failsOnError>\n        <violationSeverity>warning</violationSeverity>\n        <includeTestSourceDirectory>true</includeTestSourceDirectory>\n    </configuration>\n</plugin>\n
Run Code Online (Sandbox Code Playgroud)\n

检查样式配置文件:maven-checkstyle-suppressions-google_checks.xml

\n
<?xml version="1.0"?>\n<!DOCTYPE suppressions PUBLIC\n        "-//Checkstyle//DTD SuppressionFilter Configuration 1.2//EN"\n        "https://checkstyle.org/dtds/suppressions_1_2.dtd">\n<suppressions>\n    <suppress checks="Indentation" files="." />\n</suppressions>\n
Run Code Online (Sandbox Code Playgroud)\n

检查样式配置文件:maven-checkstyle-custom_checks.xml

\n
<?xml version="1.0"?>\n<!DOCTYPE module PUBLIC\n    "-//Checkstyle//DTD Checkstyle Configuration 1.3//EN"\n    "https://checkstyle.org/dtds/configuration_1_3.dtd">\n<module name="Checker">\n    <module name="TreeWalker">\n        <module name="Indentation">\n            <property name="basicOffset" value="4" />\n            <property name="braceAdjustment" value="4" />\n            <property name="caseIndent" value="4" />\n            <property name="throwsIndent" value="4" />\n            <property name="lineWrappingIndentation" value="4" />\n            <property name="arrayInitIndent" value="4" />\n        </module>\n    </module>\n</module>\n
Run Code Online (Sandbox Code Playgroud)\n