首页 > 解决方案 > 带有 google_checks 和 4 个空格 indentSize 的 Maven Checkstyle 插件

问题描述

我正在寻找配置 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>

更新:似乎没有办法拥有与和兼容maven-checkstyle-plugin的单一格式。有没有人能够做到这一点?Checkstyle with google_checksIntellij with google_java_format

标签: mavencheckstylemaven-checkstyle-plugingoogle-java-format

解决方案


概述

目前,Checkstyle 不支持这样的配置组合。

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

  1. 如何扩展/覆盖现有配置(sun、google) · 问题 #4484 · checkstyle/checkstyle

  2. 创建配置的继承/覆盖和组合/扩展的概念 · 问题 #2873 · checkstyle/checkstyle

    • 请注意,这是一个活跃的问题。

一种解决方法

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

  1. 创建使用整个配置文件的第一个执行并禁止覆盖检查。
  2. 创建第二个执行,它使用自定义配置文件,仅带有«overridden»检查。

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

缩进相关示例(草稿)

项目:pom.xml

/build/pluginManagement/plugins插件定义:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-checkstyle-plugin</artifactId>
    <version>3.1.2</version>
    <dependencies>
        <dependency>
            <groupId>com.puppycrawl.tools</groupId>
            <artifactId>checkstyle</artifactId>
            <version>8.43</version>
        </dependency>
    </dependencies>
</plugin>

/build/plugins插件定义:

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

Checkstyle 配置文件:maven-checkstyle-suppressions-google_checks.xml

<?xml version="1.0"?>
<!DOCTYPE suppressions PUBLIC
        "-//Checkstyle//DTD SuppressionFilter Configuration 1.2//EN"
        "https://checkstyle.org/dtds/suppressions_1_2.dtd">
<suppressions>
    <suppress checks="Indentation" files="." />
</suppressions>

Checkstyle 配置文件:maven-checkstyle-custom_checks.xml

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

推荐阅读