How to suppress all checks for a file in Checkstyle?

JavaCheckstyle

Java Problem Overview


I'm doing an override for a third party class and I want to suppress all checks for it (since I'm only keeping it around until the patch is accepted).

Is there a way to suppress all checks for a file?

I tried using "*" but that fails.

Java Solutions


Solution 1 - Java

Don't know whether you're using command line or in an IDE, but you'll basically need a suppresions file. If you're manually editing the Checkstyle config file, add a new module to it:

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

Your mysuppression.xml can be something like:

<?xml version="1.0"?>

<!DOCTYPE suppressions PUBLIC
    "-//Puppy Crawl//DTD Suppressions 1.1//EN"
    "http://www.puppycrawl.com/dtds/suppressions_1_1.dtd">

<suppressions>
    <suppress files="TheClassToIgnore\.java" checks="[a-zA-Z0-9]*"/>
</suppressions>

The value for "files" attribute is basically a regex for your source files, and the value for the "checks" attribute is regex for what checks to skip ("[a-zA-Z0-9]*" basically means skip everything). This is the pattern you're looking for I guess?

Solution 2 - Java

I was able to suppress the checks in a file by adding the SuppressionCommentFilter tag in my checks.xml:

First, I added the FileContentHolder tag as a child of TreeWalker tag (this step is not needed since version 8.1 of com.puppycrawl.tools:checkstyle):

<module name="TreeWalker">
    ...
    <module name="FileContentsHolder"/>
    ...
</module>

Then I added the SuppressionCommentFilter in the checks.xml (since version 8.1 of com.puppycrawl.tools:checkstyle - under "TreeWalker" node):

<module name="SuppressionCommentFilter"/>

In each file that I wanted to suppress the checks I inserted the following comment in the first line of the file:

// CHECKSTYLE:OFF

Solution 3 - Java

aberrant80's answer helped a lot. With the help of his hint - using regex pattern matching, I was also able to suppress checkstyle for an entire package by adding this to the checkstyle-suppressions.xml file. For eg. to skip checkstyle checks on all auto generated java files under the jaxb package,

    <suppressions>
        <suppress checks="[a-zA-Z0-9]*" files="[\\/]jaxb[\\/]" />
    </suppressions>

Solution 4 - Java

If you're using the Checkclipse Eclipse plugin for Checkstyle, you can include or exclude file patterns (including directories) by going to the Checkclipse > File Filter tab under project properties. For example, my project contains src and test directories at the top level. I want Checkstyle applied to only files in the src directory (omitting test), so I added an include pattern that looks like this:

src/.+java$

As you can see, it uses a regex-style syntax for pattern specification.

Solution 5 - Java

@aberrant80's answer is very inspiring although it didn't work in my project. In order to suppress any warnings that checkstyle looks for in Foo.java, according to [link][1], if maven is used:

<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-checkstyle-plugin</artifactId>
    ...
		<configuration>
			...
			<configLocation>checkstyle.xml</configLocation>
			<suppressionsLocation>checkstyle-suppressions.xml</suppressionsLocation>
			...
		</configuration>				
	...
</plugin>

And in checkstyle-suppressions.xml

<!DOCTYPE suppressions PUBLIC "-//Puppy Crawl//DTD Suppressions 1.1//EN" "http://www.puppycrawl.com/dtds/suppressions_1_1.dtd">
<suppressions>
    <suppress files="Foo.java" checks="[a-zA-Z0-9]*" />
</suppressions>

So far the highest version is 1.1 (1.2 does not exist). [1]: http://maven.apache.org/plugins/maven-checkstyle-plugin/examples/suppressions-filter.html

Solution 6 - Java

There is an option to ignore write protected files. Or files in a package.

Solution 7 - Java

If you wish to not have a group of files within a project inspected, you can filter these files out so they are not inspected by creating a file filter

The file filter uses regex to determine what files to exclude. The regex operates on the complete file name - because of this, you could also exclude whole folders. In this case you could exclude the whole package if you wished.

If you google around a bit - you could probably find some Checkstyle Configuration Propertie files that have examples of what you're looking for. I would suggest after you do so - save it as a bit of a template so you can refer to it in future situations

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAlceu CostaView Question on Stackoverflow
Solution 1 - Javaaberrant80View Answer on Stackoverflow
Solution 2 - JavaAlceu CostaView Answer on Stackoverflow
Solution 3 - JavaInxsibleView Answer on Stackoverflow
Solution 4 - JavaRob HView Answer on Stackoverflow
Solution 5 - JavaTiinaView Answer on Stackoverflow
Solution 6 - JavaakarnokdView Answer on Stackoverflow
Solution 7 - Javaist_lionView Answer on Stackoverflow