Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to run Checkstyle only on .java files WITHOUT ANT?

I'm trying have Checkstyle be given any file type, but ignore anything that is not a .java file. I created a filter, but that doesn't seem to be working:

public class DotJavaFilter
    extends AutomaticBean
    implements Filter
{

    public DotJavaFilter()
        throws PatternSyntaxException
    {
    }

    public boolean accept(AuditEvent aEvent)
    {
        final String fileName = aEvent.getFileName();
        return fileName.endsWith(".java");
    }
}

I'd like to give CS a directory of files and have it only process the .java ones.

like image 423
ed_is_my_name Avatar asked Apr 30 '12 15:04

ed_is_my_name


1 Answers

You can run it on the command line like this:

java -jar checkstyle-5.5-all.jar -c docs/sun_checks.xml -r /path/to/src

If you are using bash, you can turn on globstar and then process only java files like this:

shopt -s globstar
java -jar checkstyle-5.5-all.jar -c docs/sun_checks.xml -r /path/to/src/**/*.java

Checkstyle command line documentation is here.


Update: Using a suppression filter

Create a suppressions file which to ignore all checks on class files. You can add regexes for other file types you are not interested as well.

suppressions.xml:

<suppressions>
    <suppress checks="." files=".*\.class"/>
</suppressions>

Add a suppression filter to your checks file:

my_checks.xml:

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

Run it:

java -jar checkstyle-5.5-all.jar -c my_checks.xml -r /path/to/src

Documentation on Suppression Filters can be found here.

like image 71
dogbane Avatar answered Sep 22 '22 14:09

dogbane