'How to run Maven Checkstyle plugin on modified files only

I am running Maven Checkstyle plugin through pre-commit githook written in python (I think that the same question applies to running it directly from terminal).

The command is:

mvn checkstyle:checkstyle

However, I would like to run Maven Checkstyle only on files modified by git. For instance, I could run it once for each file. If I want to run it on a specific file, I may define the following pattern (I am not sure why do I need the pattern of stars and dashes in front):

mvn checkstyle:checkstyle -Dcheckstyle.includes=**\/*File.java

However, I am not able to pass file path and file name, for instance:

mvn checkstyle:checkstyle -Dcheckstyle.includes=src/main/java/File.java

Or, following the above mentioned pattern:

mvn checkstyle:checkstyle -Dcheckstyle.includes=**\/*src/main/java/File.java

I have tried many other combinations as well, but nothing works. I found this issue which is about the same thing, so I am wondering if someone has found a solution to this.

I have also read:

How to run maven checkstyle plugin on incremental code only

Is there a maven-git-checkstyle plugin that runs checkstyle goal on git staged files alone?

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

but they do not solve my problem.



Solution 1:[1]

There is an example here: github project of maven-checkstyle-plugin

Put a placeholder in pom and pass the parameter by maven command line.

    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-checkstyle-plugin</artifactId>
    <version>3.1.2</version>
    <configuration>
      <includes>${checkstyle.includes}</includes>
    </configuration>

Command line: mvn checkstyle:check "-Dcheckstyle.includes=**/File.java"

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 quan.s