【问题标题】:Suppressing Google Checkstyle warnings via checkstyle-suppressions.xml通过 checkstyle-suppressions.xml 抑制 Google Checkstyle 警告
【发布时间】:2020-11-09 09:01:45
【问题描述】:

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

我需要能够在我的一个课程中抑制MemberName 警告,并且当且仅当我将SuppressWarningsHolderSuppressWarningsFilter 添加到google_checks.xml 时,我才能使用@SuppressWarnings("checkstyle:MemberName") 来做到这一点 每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 配置文件中的何处执行此操作,或者确切设置它的位置。

【问题讨论】:

    标签: java gradle checkstyle


    【解决方案1】:

    作为系统属性,您可以按照以下配置在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
    }
    

    【讨论】:

    • 我认为 SystemProperty 的正确格式是System.setProperty( "org.checkstyle.google.suppressionfilter.config", "$rootProject.projectDir/checkstyle-suppressions.xml")
    • 它可以双向工作,您也可以按照您的建议使用字符串插值。应根据其单项目或多项目构建来使用对象项目或 rootProject。
    • 谢谢!最终使用:System.setProperty( "org.checkstyle.google.suppressionfilter.config", "$projectDir/config/checkstyle/checkstyle-suppressions.xml")
    【解决方案2】:

    Gradle plugin 为此提供了Map&lt;String, Object&gt; configProperties 选项。

    可在配置文件中使用的属性。这些被替换到配置文件中。

    所以对于 .checkstyle 文件夹中的 suppresss.xml,

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-03
      • 2018-10-11
      • 2010-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-26
      • 1970-01-01
      相关资源
      最近更新 更多