Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

checkstyle + suppression filters

I have a checkstyle suppression filter setup (e.g. ignore magic numbers in unit test code).

The suppression xml file resides in the same folder as the checkstyle xml file. However, where this file actually is varies: on my windows dev box it is in d:\dev\shared\checkstyle\config on the Linux CI server it will be in /root/repo/shared/checkstyle/config on another developers box it could be anywhere (they check out their svn repo to).

The only "consistent" thing is that the suppression file is always in the same folder as the checkstyle xml file. I cannot work out how to ensure that this file is always consistently picked up. Also I don't know why checkstyle does not support embedded suppression within the checkstyle xml file.

any help?

like image 213
Gary McWilliams Avatar asked Oct 13 '08 16:10

Gary McWilliams


People also ask

What is checkstyle suppression filter?

Checkstyle allows the definition of a list of files and their line ranges that should be suppressed from reporting any violations (known as a suppressions filter ).

How do I stop checkstyle warnings?

It is possible to suppress all the checkstyle warnings with the argument "all" . You can also use a checkstyle: prefix to prevent compiler from processing these annotations. You can also define aliases for check names that need to be suppressed.

How do I exclude files from checkstyle?

To skip all checks in all files under directories (packages) named "generated", you must use regex pattern <suppress checks="[a-zA-Z0-9]*" files="[\\/]generated[\\/]" /> It was really little hard for me to find out the right pattern for files parameter.

What is Maven checkstyle plugin?

The Checkstyle Plugin generates a report regarding the code style used by the developers. For more information about Checkstyle, see https://checkstyle.org/. This version of the plugin uses Checkstyle 9.3 by default and requires Java 8. But you can upgrade the version used at runtime.


1 Answers

I had this same problem with the Checkstyle suppression configuration when I was going back and forth between Linux and Windows. Here's how I solved it in my Ant-based build system:

Basically, I inject the proper, platform-specific directory value into the main Checkstyle configuration file by configuring a Checkstyle properties file with an Ant build script.

My main Checkstyle configuration file has a SuppressionFilter module declaration as shown below. The value of the checkstyle-suppressions-file property comes from a Checkstyle properties file:

<module name="SuppressionFilter">
    <property name="file" value="${checkstyle-suppressions-file}"/>
</module>

The Checkstyle properties file is not static, it is generated by an Ant build script from a properties file template called template-checkstyle.properties. Here's what the template looks like for the suppressions file property:

checkstyle-suppressions-file=@SCM_DIR@/checkstyle_suppressions.xml

My Ant build script copies this file to a file named checkstyle.properties. The copy has the special token replaced with the proper value of the directory in which the suppressions file is found:

<copy file="${scm.dir}/template-checkstyle.properties" tofile="${scm.dir}/checkstyle.properties">
    <filterset>
        <filter token="SCM_DIR" value="${scm.dir.unix}"/>
    </filterset>
</copy>

Now, where does the value of scm.dir.unix come from? Well, it's derived from a property of my build, read on. You'll need to specify such a value with the directory values that you mentioned.

Note that there is one slightly non-obvious issue concerning the way in which you specify this directory. I say that the scm.dir.unix value is derived from a build property because I observed that the main Checkstyle configuration file cannot contain backslashes, i.e. Windows path separator characters, in the value of the file property of the SuppressionFilter module. For example, specifying something like C:\foo\bar\baz leads to a Checkstyle error message saying that C:foobarbaz cannot be found. I work around this by "converting" the scm.dir directory build property to a "unix" format with Ant's pathconvert task:

<pathconvert targetos="unix" property="scm.dir.unix">
    <path location="${scm.dir}"/>
</pathconvert>

Then I call the checkstyle Ant task like this:

<checkstyle config="${scm.dir}/checkstyle_checks.xml"
            properties="${scm.dir}/checkstyle.properties">
    <!-- details elided -->
</checkstyle>

The call to the checkstyle task injects the key/value pairs contained in the checkstyle.properties file into the main Checkstyle configuration.

If you like, you can see the full scripts here

Hope this helps

like image 164
Greg Mattes Avatar answered Sep 24 '22 00:09

Greg Mattes