通过 checkstyle-suppressions.xml 抑制 Google Checkstyle 警告

Suppressing Google Checkstyle warnings via checkstyle-suppressions.xml

我在 Gradle 项目中使用 google_checks.xml 作为 CheckStyle 配置。

我需要能够在我的 类 之一中抑制 MemberName 警告,并且当且仅当我添加 [=16= 时,我可以使用 @SuppressWarnings("checkstyle:MemberName") 来实现] 和 SuppressWarningsFiltergoogle_checks.xmlthis post.

问题是我定期更新 google_checks.xml,我不想记住需要重新添加这些,所以我想在单独的 [=20= 中处理这些抑制] 文件。根据 google_checks.xml:

的这一部分,这应该是可行的
  <module name="SuppressionFilter">
    <property default="checkstyle-suppressions.xml" name="file"
      value="${org.checkstyle.google.suppressionfilter.config}"/>
    <property name="optional" value="true"/>
  </module>

但是,我不知道如何让它在我的项目的根目录而不是默认的 .gradle\daemon.5.1\checkstyle-suppressions.xml 路径中查找这个文件。我怎样才能将它指向另一个位置?

如果我设置 value="${config_loc}/checkstyle-suppressions.xml",它会做我想要的,但我们又回到了我不想修改 google_style.xml.

的问题

似乎我需要以某种方式设置 org.checkstyle.google.suppressionfilter.config 系统 属性,但我不确定在我的 Gradle 配置文件中的什么位置进行设置,或者到底要做什么设置为。

作为一个系统 属性,您可以按照以下配置在 build.gradle 中覆盖它,假设您在项目根文件夹中有 checkstyle-suppressions.xml

NOTE: The config file is pointing to google_checks.xml from the checkstyle jar, and is not part of your project.

System.setProperty( "org.checkstyle.google.suppressionfilter.config", project.projectDir.toString()+"/checkstyle-suppressions.xml" )
checkstyle {
    toolVersion = checkStyleVersion
    configFile = file("/google_checks.xml")
    ignoreFailures = false
    showViolations = false
    maxWarnings = 0
}

Gradle plugin provides the Map<String, Object> configProperties 选项。

The properties available for use in the configuration file. These are substituted into the configuration file.

因此对于 .checkstyle 文件夹中的 suppressions.xml,

checkstyle {
    configProperties = [
        "org.checkstyle.google.suppressionfilter.config":
            "${projectDir}/.checkstyle/suppressions.xml",
    ]
}