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

霸气de小男生 提交于 2020-08-23 04:58:29

问题


I want to have an opportunity to pass the list of *.java files in commit/changeset to the pre-commit hook which will check those java files for code style.

I've tried to use maven-checkstyle-plugin but it looks like it is not possible to pass to it an arbitrary list of files. Also, running mvn site builds reports which are not supposed to be used exclusively like a human-readable entity, so it is not trivial to use this report in python scripts (which mercurial hooks basically are).

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

By style-checking I mean not only printing report to stdout but returning somehow the final result - whether files had or had not passed the guidelines.


回答1:


if I understand rightly, 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

cheers




回答2:


the solution proposed by SubOptimal can be combined with the Suppression Filter: it allows to use regular expressions for the names of the files to be excluded from check and even to specify some line ranges.



来源:https://stackoverflow.com/questions/24208568/how-to-check-style-arbitrary-list-of-java-files-from-command-line

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!