How to check arbitrary list of java files from command line?

I want to be able to pass a list of * .java files in a commit / changeset file to a pre-commit hook that will check these java files for code style.

I tried using maven-checkstyle-plugin , but it seems like it is not possible to pass an arbitrary list of files to it. In addition, at startup mvn site, reports are built that should not be used exclusively as a human-readable object, so it is not trivial to use this report in python scripts (mostly hooks).

So the question is: how to check the style of an arbitrary list of * .java files on the command line (just like we check an arbitrary list of python files with pep8 or javascript files with jshint / jslint)?

By style checking, I mean not only printing the report in stdout, but also somehow returning the final result - regardless of whether the files were there or the recommendations failed.

+4
source share
2 answers

If I understand correctly, you want to check not all Java sources in the project, only some specific files?

# this checks all *.java sources
mvn checkstyle:checkstyle -Dcheckstyle.includes=**\/*.java

# this checks only sources matched by Foo*.java
mvn checkstyle:checkstyle -Dcheckstyle.includes=**\/Foo*.java

# this checks only sources matched by Foo*.java and the source Bar.java
mvn checkstyle:checkstyle -Dcheckstyle.includes=**\/Foo*.java,**\/Bar.java

In all cases, the result will be

# for your further automatic processing
target/checkstyle-result.xml

# for humans to read
target/site/checkstyle.html

amuses

+8
source

the solution proposed by SubOptimal can be combined with the Suppression Filter : it allows you to use regular expressions for file names that should be excluded from the scan and even specify some line ranges.

0
source

Source: https://habr.com/ru/post/1544499/


All Articles