# 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?

Asked by: Roman449 | Posted: 28-01-2022

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 Answered by: Carina590 | Posted: 01-03-2022 # Answer 2 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>


Answered by: Rafael454 | Posted: 01-03-2022

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.

Answered by: Max669 | Posted: 01-03-2022

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}  Answered by: Abigail271 | Posted: 01-03-2022 # Answer 5 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.

Answered by: Sarah532 | Posted: 01-03-2022

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

• ${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.

Answered by: Joyce755 | Posted: 01-03-2022

# Similar questions

java - How to use a single checkstyle suppression file in Maven for all modules

I have a project that consists of several Maven modules which are all children of a parent module. I have the parent set up to use checkstyle and the child modules all inherit this behaviour correctly. I would like all the child modules to use the parents suppression file defined in its plugin. I define a property checkstyle.suppression which is used in the checkstyle plugin &lt;propertie...

java - Maven: On Windows Checkstyle plug-in does not apply suppression filter

On Linux/MacOS, Checkstyle plug-in applies suppression filter as expected, but on Windows it seems as if the filter is not applied and the build fails with the exact warnings intended to be suppressed in the suppressions.xml file. What could be the problem? Snippet from the pom.xml: &lt;plugin&gt; &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt; &lt;artifactId&gt...

java - How to use a single checkstyle suppression file in Maven for all modules

I have a project that consists of several Maven modules which are all children of a parent module. I have the parent set up to use checkstyle and the child modules all inherit this behaviour correctly. I would like all the child modules to use the parents suppression file defined in its plugin. I define a property checkstyle.suppression which is used in the checkstyle plugin &lt;propertie...

java - Using noise suppression module in webrtc in android

I am developing a Speaker Identification app in android and I wanted to process the audio input from microphone before analysing it with the speaker identification algorithm. I came across to the noise suppression module in WebRTC. I have already set it up in android using JNI. I have a question about the function WebRtcNs_Process in the module. In the header file, it says

java - Suppression problems with generically typed Arrays

Why does this give me a type safety warning? MyAwesomeObject&lt;T&gt;[] parent = new MyAwesomeObject[1];

java - Suppression warnings for methods called via reflection

I have java code were the methods are all private and called via reflection. Any IDE i use throws unused method warning. I don't want any warnings in my code. Is there any suppression warnings for the methods that are called via reflection?

jvm - How to compress variables in Java using leading zero byte suppression?

Leading zero byte suppression means that leading zero bytes of an integer value will be removed and instead the number of eliminated bytes is stored. For example: suppose we have 32-bit integers, hexadecimal value 00000090 will be encoded as binary value 01110010000, where 011 means there are 3 zero bytes in 00000090. My question is how to implement leading zero byte suppress...

java - maven eclipse with m2e: suppression in multi module project

I can't get eclipse-cs to use the suppressions.xml. I use the m2e-code-quality plugin. The checkstyle.xml and suppressions.xml are in their own (aggregated/non-child) module: -parent |-build-tools ||-pom.xml ||-src.main.resources.checkstyle | |-checkstyle.xml | |-suppressions.xml |-pom.xml |-core |-utils |-... The build-tools pom is minimal, no dependency. The other (child)modul...

java - Child activity thread causes the app to be frozen on suppression

I organised my code as follow : MainMenu (an activity) starts an intent on Game (another activity). Game sets GamePanel (a surface holder) which starts GameThread (a thread). MainMenu.java : public void buttonPlay(View view) { Intent intent = new Intent(this, Game.class); startActivity(intent); }

java - Maven: On Windows Checkstyle plug-in does not apply suppression filter

On Linux/MacOS, Checkstyle plug-in applies suppression filter as expected, but on Windows it seems as if the filter is not applied and the build fails with the exact warnings intended to be suppressed in the suppressions.xml file. What could be the problem? Snippet from the pom.xml: &lt;plugin&gt; &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt; &lt;artifactId&gt...

# Still can't find your answer? Check out these amazing Java communities for help...

Java Reddit Community | Java Help Reddit Community | Dev.to Java Community | Java Discord | Java Programmers (Facebook) | Java developers (Facebook)