首页 > 解决方案 > 通过 checkstyle-suppressions.xml 抑制 Google Checkstyle 警告

问题描述

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

我需要能够MemberName在我的一个课程中抑制警告,并且我可以使用@SuppressWarnings("checkstyle:MemberName")if 且仅当我添加SuppressWarningsHolderand SuppressWarningsFilterto google_checks.xml per this post时才能这样做。

问题是我google_checks.xml定期更新,我不想记得需要重新添加这些,所以我想在单独的checkstyle-suppressions.xml文件中处理这些抑制。这应该按照以下部分可行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\6.5.1\checkstyle-suppressions.xml路径中查找此文件。如何将其指向另一个位置?

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

似乎我需要以org.checkstyle.google.suppressionfilter.config某种方式设置系统属性,但我不确定在我的 Gradle 配置文件中的哪个位置执行此操作,或者确切设置它的位置。

标签: javagradlecheckstyle

解决方案


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

注意:配置文件指向 checkstyle jar 中的 google_checks.xml,并且不是您项目的一部分。

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
}

推荐阅读