简体   繁体   English

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

[英]Suppressing Google Checkstyle warnings via checkstyle-suppressions.xml

I'm using google_checks.xml as a CheckStyle config in my Gradle project.我在我的 Gradle 项目中使用google_checks.xml作为 CheckStyle 配置。

I need to be able to suppress the MemberName warning in one of my classes, and I can do so using @SuppressWarnings("checkstyle:MemberName") if and only if I add SuppressWarningsHolder and SuppressWarningsFilter to google_checks.xml per this post .我需要能够在我的一个类中抑制MemberName警告,并且当且仅当我根据这篇文章SuppressWarningsHolderSuppressWarningsFilter添加到google_checks.xml时,我才能使用@SuppressWarnings("checkstyle:MemberName")来做到这一点。

The problem is that I update google_checks.xml regularly, and I don't want to remember to need to re-add these, so I'd like to handle these suppressions in a separate checkstyle-suppressions.xml file.问题是我定期更新google_checks.xml ,我不想记得需要重新添加这些,所以我想在单独的checkstyle-suppressions.xml文件中处理这些抑制。 This should be doable per this section of google_checks.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>

However, I can't figure out how to get it to look for this file in my project's root directory instead of in the default .gradle\daemon\6.5.1\checkstyle-suppressions.xml path.但是,我不知道如何让它在我的项目的根目录中而不是在默认的.gradle\daemon\6.5.1\checkstyle-suppressions.xml路径中查找此文件。 How can I point it to another location?如何将其指向另一个位置?

If I set value="${config_loc}/checkstyle-suppressions.xml" , it does what I want, but we're back to the problem of me not wanting to have to modify google_style.xml .如果我设置value="${config_loc}/checkstyle-suppressions.xml" ,它会做我想要的,但我们又回到了我不想修改google_style.xml的问题。

It seems like I need to set the org.checkstyle.google.suppressionfilter.config system property somehow, but I'm not sure where to do this in my Gradle config files, or what exactly to set it to.似乎我需要以某种方式设置org.checkstyle.google.suppressionfilter.config系统属性,但我不确定在我的 Gradle 配置文件中的何处执行此操作,或者确切设置它的位置。

As its a system property, you can override it in the build.gradle as per below config, say you have checkstyle-suppressions.xml in the project root folder.作为系统属性,您可以按照以下配置在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.注意:配置文件指向来自 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
}

The Gradle plugin provides the Map<String, Object> configProperties option for this. Gradle 插件为此提供了Map<String, Object> configProperties选项。

The properties available for use in the configuration file.可在配置文件中使用的属性。 These are substituted into the configuration file.这些被替换到配置文件中。

So for suppressions.xml in the.checkstyle folder,所以对于.checkstyle文件夹中的抑制.xml,

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM