JollyRoger
JollyRoger

Reputation: 847

How To Exclude Some Files From Checking Operations

I am using checkstyle plugin to format my code and I need to exclude some java classes from checking operations. To do that, I wrote the code below:

<module name="BeforeExecutionExclusionFileFilter">
    <property name="fileNamePattern" value="src/main/java/package1/*.java"/>
</module>

<module name="BeforeExecutionExclusionFileFilter">
    <property name="fileNamePattern" value="src/main/java/package2/*.java"/>
</module>

I tried to exclude all classes in 2 different packages. But the code does not works unfortunately. I also tried as below:

<module name="BeforeExecutionExclusionFileFilter">
    <property name="fileNamePattern" value="src/main/java/package1/*.java"/>
    <property name="fileNamePattern" value="src/main/java/package2/*.java"/>
</module>

But this did not worked as well. The checkstyle plugin again checks all classes. How can I achieve this?

Upvotes: 3

Views: 949

Answers (2)

Dzmitry Sankouski
Dzmitry Sankouski

Reputation: 177

Your problem was fileNamePattern is treated as regex, so wildcard in package1/*.java would not match class name letters, but will be a quantifier for /.

Also, let's consider exclusion logic code in checkstyle(as of 10.09.2023):

public boolean accept(String uri) {
    return fileNamePattern == null || !fileNamePattern.matcher(uri).find();
}

Since it uses find, not match, common path can be omitted, and you can use regex or expression, so your xml may be stripped to

<module name="BeforeExecutionExclusionFileFilter">
    <property name="fileNamePattern" value="package1|package2"/>
</module>

Upvotes: 0

JollyRoger
JollyRoger

Reputation: 847

I found the solution after long tries. My format for giving a path was wrong. Correct format should be like this one:

    <module name="BeforeExecutionExclusionFileFilter">
    <property name="fileNamePattern" value="[/\\]src[/\\]main[/\\]java[/\\]package1"/>
    </module>

    <module name="BeforeExecutionExclusionFileFilter">
        <property name="fileNamePattern" value="[/\\]src[/\\]main[/\\]java[/\\]package2"/>
    </module>

If we wanna give more than one package to ignore, this is the way we should do. Giving two property with same name does not work. Hope this helps to others.

Upvotes: 4

Related Questions