2

I'm using google_checks.xml as a CheckStyle config in my Gradle project.

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.

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. This should be doable per this section of 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. 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.

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.

Amith Kumar
  • 4,400
  • 1
  • 21
  • 28
Gravity Grave
  • 2,802
  • 1
  • 27
  • 39

2 Answers2

3

The Gradle plugin provides the Map<String, Object> configProperties option for this.

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 {
    configProperties = [
        "org.checkstyle.google.suppressionfilter.config":
            "${projectDir}/.checkstyle/suppressions.xml",
    ]
}
Ders
  • 1,068
  • 13
  • 16
2

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.

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
}
Amith Kumar
  • 4,400
  • 1
  • 21
  • 28
  • 1
    I think the correct format for the SystemProperty is `System.setProperty( "org.checkstyle.google.suppressionfilter.config", "$rootProject.projectDir/checkstyle-suppressions.xml")` – mcfan Nov 13 '20 at 23:29
  • it work both ways, you can use string interpolation as well, as you suggested. The object project or rootProject should be used depending on its single or multi project build. – Amith Kumar Nov 14 '20 at 01:13
  • 1
    Thank you! Ended up using: `System.setProperty( "org.checkstyle.google.suppressionfilter.config", "$projectDir/config/checkstyle/checkstyle-suppressions.xml")` – Gravity Grave Dec 19 '20 at 04:12