How to define suppressions-definitions for checkstyle, that work with both ant and eclipse

10,176

Solution 1

Use Checkstyle's property expansion functionality. In your checkstyle.xml declare your SupressionFilter as:

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

Then modify your Checkstyle task in your Ant build script to include a nested property:

<checkstyle config="conf/checkstyle.xml">
    <fileset dir="src" includes="**/*.java"/>
    <property key="checkstyle.suppressions.file" value="conf/suppressions.xml"/>
</checkstyle>

Solution 2

This question is pretty old, but I've found a better way to do it using the Checkstyle Advanced properties:

For the Eclipse Checkstyle plugin, the ${samedir} property expands to the directory that the configuration file is located within:

In your case, your module configuration would look like this:

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

The Ant target would also set the samedir property:

<checkstyle config="${checkstyle.tool.dir}/checks.xml" failOnViolation="false">
    <fileset dir="${src.dir}" includes="**/*.java" />
    <property key="samedir" value="${checkstyle.tool.dir}/conf" />
</checkstyle>
Share:
10,176
Mnementh
Author by

Mnementh

My name is Jörgen Kosche. I'm a programmer using mostly Java.

Updated on June 04, 2022

Comments

  • Mnementh
    Mnementh almost 2 years

    I use in a project checkstyle and I have defined a SuppressionFilter in my checkstyle-configuration. I use Apache ant to make automatic builds via Continuous Integration.

    My problems comes from the following situation: I don't want to fill to much files into the project-basedir, so the checkstyle.xml and the suppressions.xml are both in a subdirectory named conf (for configuration for build). Now Ant and Eclipse work differently for finding the suppressions.xml.

    Ant use the project-basedir as basedir for finding the suppressions.xml, after I declared an ant-task to find the checkstyle.xml with the base-configuration of checkstyle. This checkstyle.xml now contains the following:

    <module name="SuppressionFilter">
        <property name="file" value="conf/suppressions.xml"/>
    </module>
    

    This way the ant-build finds the suppressions.xml, because the basedir of the build is the project-directory.

    Now using the checkstyle-plugin for Eclipse brings a problem. It looks for the suppressions.xml starting with the path the checkstyle.xml has (conf). For Eclipse the declaration had to look like this, to work:

    <module name="SuppressionFilter">
        <property name="file" value="suppressions.xml"/>
    </module>
    

    EDIT: Even that doesn't work, Eclipse seems to need always an absolute path.

    I want to know a way, that both Eclipse and Ant can work with the same checkstyle-configuration. Someone knows a solution to this problem? Absolute paths are no solution, because every developer and the CI-Server have different paths for the project-directory.

  • Mnementh
    Mnementh about 15 years
    Thanks, that works. The one exception is my error in description, Eclipse seems to need an absolute path.
  • David Harkness
    David Harkness over 10 years
    You can use ${basedir} (see the link for more path proprties) for a project-relative path. This allows you to use a common checkstyle.xml for many projects but still store the suppression files with each project.
  • Edward
    Edward about 9 years
    Is there a way to use ${samedir} in the gradle checkstyle-plugin? stackoverflow.com/questions/29602867/…