4

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.

shabunc
  • 23,119
  • 19
  • 77
  • 102
  • Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it. – Jim Garrison Jun 13 '14 at 16:07
  • @JimGarrison let's me try to edit this to make it clear. It is not about most common used pattern, not about finding a tool or library . – shabunc Jun 13 '14 at 16:36
  • @JimGarrison have edited the question. I understand about what kind of off-topic questions you are talking about, but hope now the question looks like not of that kind. Questions like "best practices for copying files in grunt" are not off-topic. – shabunc Jun 13 '14 at 16:55
  • @Thomas I've tried to use maven-checkstyle-plugin (it is actually mentioned in this question) and calling checkstyle from command line - http://checkstyle.sourceforge.net/cmdline.html – shabunc Jun 14 '14 at 10:21

2 Answers2

15

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

Juvanis
  • 25,802
  • 5
  • 69
  • 87
SubOptimal
  • 22,518
  • 3
  • 53
  • 69
0

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.

rajadilipkolli
  • 3,475
  • 2
  • 26
  • 49
marcello
  • 146
  • 1
  • 5