Question

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?

Was it helpful?

Solution

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

OTHER TIPS

In eclipse I put the following which did not require me to add any additional properties:

<module name="SuppressionFilter">
    <property name="file" value="${samedir}/suppressions.xml"/>
</module>

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.

If you are working with eclipse and you have the suppression file in the same directory as the external checkstyle config, you can set up a suppression filter like this:

<module name="SuppressionFilter">
    <property name="file" value="${config_dir}/my_suppressions.xml"/>
</module>

You also must define the ${config_dir} property in the checkstyle configuration:

Eclipse Preferences -> "Checkstyle" -> Choose your cs config -> "Properties .." -> "Additional Properties .."

Define a property for the checkstyle config dir:

config_dir --->  ${config_loc}

I think Robert's answer can be extended to an easy solution for ant and Eclipse:

Include the suppression file inside your config XML like this:

<module name="SuppressionFilter">
    <property name="file" value="${samedir}/suppressions.xml"/>
</module>

Now, Eclipse is satisfied and finds the file.

To get ant to work update your target to something like this:

<checkstyle config="${checkstyle.config}/checkstyle-checks.xml">
    <!-- ... -->
    <property key="samedir" value="${checkstyle.config}"/>
</checkstyle>

Hope this helps.

Since CheckStyle 4.26.0 you can use predefined constants in your config files.

(https://github.com/jshiell/checkstyle-idea/issues/217) :

  • ${basedir} & ${project_loc} - gets mapped to the current project directory
  • ${workspace_loc} - gets mapped to the current Eclipse workspace directory
  • ${config_loc} & ${samedir} - get mapped to the directory the configuration file lies in

If you want to share the config with maven you will need to "alias" the "eclipse constants" in your POM configuration (in the reporting section) using the "propertyExpansion" configuration element :

<plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-checkstyle-plugin</artifactId>
   <version>3.0.0</version>
   <configuration>
      <configLocation>${project.basedir}/quality/checkstyle/dap_checkstyle_checks.xml</configLocation>
      <propertyExpansion>basedir=${project.basedir}</propertyExpansion>
   </configuration>
   <reportSets>
      <reportSet>
         <reports>
            <report>checkstyle</report>
         </reports>
      </reportSet>
   </reportSets>
</plugin>

The "propertyExpansion" is inspired from : https://github.com/checkstyle/checkstyle/blob/master/pom.xml#L582.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top