views:

2412

answers:

2

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?

+6  A: 

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

Greg Mattes
Thanks for the tips, my ant build is now fine, ignoring magic numbers in Test classes (it's a start!).I had to make one change to your supplied code (for reference anyone else who may come to this later), the <copy> task is missing a "filtering=true"; at least when running with my version of ant.
Gary McWilliams
+2  A: 

I get the absolute path to the directory where build.xml resides using the ant.file variable and the name of the project:

<project name="common" ... >
  <dirname property="thisdir" file="${ant.file.common}"/>

Then I can concatenate an absolute path to my checkstyle config files:

checkstyle.suppressions.file=${thisdir}/qclib/checkstyle-suppressions.xml

Since the thisdir variable comes from ant, it does not seem to need path separator conversion.

gibbss