英文:
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
我尝试了许多其他组合,但都无效。我发现了这个问题,与我的情况相同,所以我想知道是否有人找到了解决办法。
我还阅读了:
但它们都没有解决我的问题。
英文:
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:
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.
<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"
答案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 使其工作。如果您找到方法,请分享
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:
{
"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>
专注分享java语言的经验与见解,让所有开发者获益!
评论