Maven Checkstyle插件,带有google_checks和4个空格indentSize 项目:pom.xmlCheckstyle 配置文件:maven-checkstyle-suppressions-google_checks.xmlCheckstyle 配置文件:maven-checkstyle-custom_checks.xml

问题描述

我正在寻找方法来配置google_checks以在maven Checkstyle插件中使用4 spaces。我将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-pluginCheckstyle with google_checksIntellij with google_java_format兼容。有人能做到吗?

解决方法

概述

目前Checkstyle不支持这种配置组合。

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

  1. How to extend/override an existing configuration (sun,google) · Issue #4484 · checkstyle/checkstyle

  2. create concept of inheritance/override and compositions/extension of configs · Issue #2873 · checkstyle/checkstyle

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

解决方法

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

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

此处还解释了此解决方法:create concept of inheritance/override and compositions/extension of configs · Issue #2873 · checkstyle/checkstyleThe comment

缩进相关示例(草稿)

项目: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>

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...