6

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.

nevermind
  • 129
  • 1
  • 7

2 Answers2

0

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"

quan.s
  • 1
  • Hi quan.s, thank you for your answer. I am not using Java and checkstyle right now so I cannot check if it works for me, but hopefully someone might find your answer useful. – nevermind May 08 '22 at 14:25
  • This would require invoking maven on every changed file? Or is there a way to pass a list to checkstyle.includes ? looks like an antpath matcher? – dan carter May 12 '23 at 03:15
0

I got it working by using some Node JS tools. Here is what I have:

Node JS Packaged

package.json file:

{
  "version": "1.0.0",
  "scripts": {
    "prepare": "husky install",
    "pre-commit": "./scripts/pre-commit-run"
  },
  "license": "ISC",
  "devDependencies": {
    "husky": "^8.0.1",
    "lint-staged": "^13.0.3"
  }
}

Lint Staged Config

lint-staged.config.js file:

const path = require('path');

module.exports = {
    '**/*.java': (javaFiles) => {
        const linters = [];
        for (let i = 0; i < javaFiles.length; i++) {
            const filePath = javaFiles[i];
            const fileName = path.parse(filePath).base;
            linters.push(`npm run pre-commit -- ${fileName}`);
        }
        return linters;
    }
}

Pre-Commit Script

I couldn't make it work with lint-staged only. If you find a way, please share ;)

pre-commit-run file:

#!/usr/bin/env sh
mvn checkstyle:check -Dcheckstyle.includes="**\\/$1"

Pre-Commit Git Hook

.husky/pre-commit file:

#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"
echo "Running pre-commit hook"
npx lint-staged

Checkstyle Maven Plugin

pom.xml file (the part that matters):


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-checkstyle-plugin</artifactId>
                    <version>3.2.0</version>
                    <dependencies>
                        <dependency>
                            <groupId>com.puppycrawl.tools</groupId>
                            <artifactId>checkstyle</artifactId>
                            <version>10.7.0</version>
                        </dependency>
                    </dependencies>
                </plugin>
            </plugins>
        </pluginManagement>

        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-checkstyle-plugin</artifactId>
                <version>${checkstyle-maven-plugin.version}</version>
                <!-- Properties for "mvn checkstyle:check" to execute as part of maven build
                     They are conflicting with properties of checkstyle:checkstyle
                     so only one set should be used
                -->
                <configuration>
                    <configLocation>checkstyle.xml</configLocation>
                    <logViolationsToConsole>true</logViolationsToConsole>
                    <consoleOutput>true</consoleOutput>
                    <failOnViolation>true</failOnViolation>
                    <failsOnError>true</failsOnError>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <reporting>
        <plugins>
            <!-- Specifying configuration here will take effect
                 on the execution of "mvn site",
                 but will NOT take effect on the execution of "mvn checkstyle:check"
                 or "mvn checkstyle:checkstyle"
             -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-checkstyle-plugin</artifactId>
                <version>${checkstyle-maven-plugin.version}</version>
                <configuration>
                    <configLocation>checkstyle.xml</configLocation>
                    <failOnViolation>false</failOnViolation>
                </configuration>
            </plugin>
        </plugins>
    </reporting>
</project>