如何仅对修改过的文件运行Maven Checkstyle插件

huangapple 未分类评论76阅读模式
英文:

How to run Maven Checkstyle plugin on modified files only

问题

我通过在Python中编写的pre-commit githook来运行Maven Checkstyle插件(我认为同样的问题也适用于直接从终端运行它)。

命令是:

mvn checkstyle:checkstyle

然而,我想只在被Git修改的文件上运行Maven Checkstyle。例如,我可以针对每个文件运行一次。如果我想要在特定文件上运行它,我可以定义以下模式(我不确定为什么需要在前面加上星号和破折号的模式):

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

然而,我无法传递文件路径和文件名,例如:

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

或者,按照上述提到的模式:

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

我尝试了许多其他组合,但都无效。我发现了这个问题,与我的情况相同,所以我想知道是否有人找到了解决办法。

我还阅读了:

https://stackoverflow.com/questions/34453774/how-to-run-maven-checkstyle-plugin-on-incremental-code-only

https://stackoverflow.com/questions/50016710/is-there-a-maven-git-checkstyle-plugin-that-runs-checkstyle-goal-on-git-staged-f

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

但它们都没有解决我的问题。

英文:

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:

https://stackoverflow.com/questions/34453774/how-to-run-maven-checkstyle-plugin-on-incremental-code-only

https://stackoverflow.com/questions/50016710/is-there-a-maven-git-checkstyle-plugin-that-runs-checkstyle-goal-on-git-staged-f

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

but they do not solve my problem.

答案1

得分: 0

这里有一个示例:maven-checkstyle-plugin的GitHub项目

在pom中放置一个占位符,并通过Maven命令行传递参数。

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

命令行:mvn checkstyle:check "-Dcheckstyle.includes=**/File.java"

英文:

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

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

    &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
    &lt;artifactId&gt;maven-checkstyle-plugin&lt;/artifactId&gt;
    &lt;version&gt;3.1.2&lt;/version&gt;
    &lt;configuration&gt;
      &lt;includes&gt;${checkstyle.includes}&lt;/includes&gt;
    &lt;/configuration&gt;

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

答案2

得分: 0

以下是您提供的内容的翻译:

我通过使用一些Node.js工具使其工作。这是我的配置:

Node.js包

package.json 文件:

{
  "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配置

lint-staged.config.js 文件:

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脚本

我无法只使用 lint-staged 使其工作。如果您找到方法,请分享 如何仅对修改过的文件运行Maven Checkstyle插件

pre-commit-run 文件:

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

Pre-Commit Git钩子

.husky/pre-commit 文件:

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

Checkstyle Maven插件

pom.xml 文件(重要部分):

<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>
                <!-- 用于 "mvn checkstyle:check" 作为 Maven 构建的一部分执行的属性 -->
                <configuration>
                    <configLocation>checkstyle.xml</configLocation>
                    <logViolationsToConsole>true</logViolationsToConsole>
                    <consoleOutput>true</consoleOutput>
                    <failOnViolation>true</failOnViolation>
                    <failsOnError>true</failsOnError>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <reporting>
        <plugins>
            <!-- 在 "mvn site" 执行期间将在此处指定的配置生效 -->
            <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>

希望这对您有所帮助!

英文:

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

Node JS Packaged

package.json file:

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

Lint Staged Config

lint-staged.config.js file:

const path = require(&#39;path&#39;);

module.exports = {
    &#39;**/*.java&#39;: (javaFiles) =&gt; {
        const linters = [];
        for (let i = 0; i &lt; 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 如何仅对修改过的文件运行Maven Checkstyle插件

pre-commit-run file:

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

Pre-Commit Git Hook

.husky/pre-commit file:

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

Checkstyle Maven Plugin

pom.xml file (the part that matters):


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

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

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


huangapple
  • 本文由 发表于 2020年8月15日 01:00:55
  • 转载请务必保留本文链接:https://java.coder-hub.com/63417074.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定