Java 有没有办法只在VCS中有更改的文件上运行checkstyle?
我最近开始帮助一个年轻的团队改进他们的开发实践,并希望他们在每次提交之前运行checkstyle。不幸的是,他们的代码到处都是错误,实现它的唯一可伸缩方法是每次对一小部分文件运行checkstyle 从战略上讲,我只想在VCS中修改过的文件上运行checkstyle。我写了一个gradle脚本来实现这一点,但它似乎不起作用。有人知道如何做到这一点吗Java 有没有办法只在VCS中有更改的文件上运行checkstyle?,java,git,gradle,android-gradle-plugin,checkstyle,Java,Git,Gradle,Android Gradle Plugin,Checkstyle,我最近开始帮助一个年轻的团队改进他们的开发实践,并希望他们在每次提交之前运行checkstyle。不幸的是,他们的代码到处都是错误,实现它的唯一可伸缩方法是每次对一小部分文件运行checkstyle 从战略上讲,我只想在VCS中修改过的文件上运行checkstyle。我写了一个gradle脚本来实现这一点,但它似乎不起作用。有人知道如何做到这一点吗 apply plugin: 'checkstyle' def getChangedFiles = { -> try {
apply plugin: 'checkstyle'
def getChangedFiles = { ->
try {
def stdout = new ByteArrayOutputStream()
exec {
commandLine 'git', 'diff', '--name-only'
standardOutput = stdout
}
return stdout.toString().trim().split("\n")
}
catch (ignored) {
return null;
}
}
task checkstyle(type: Checkstyle) {
configFile file("${project.rootDir}/quality/checkstyle/uncommon-checkstyle.xml")
configProperties = [
'checkstyle.cache.file': rootProject.file('build/checkstyle.cache'),
'checkstyleSuppressionsPath': file("${project.rootDir}/quality/checkstyle/suppressions.xml").absolutePath
]
source 'src'
String[] split = getChangedFiles()
for (int i=0; i<split.length; i++){
include split[i]
println split[i]
}
exclude '**/build/**' // Exclude everything inside build folders.
exclude '**/test/**' // Exclude tests
exclude '**/androidTest/**' // Exclude android test files.
ignoreFailures = false // Don't allow the build to continue if there are warnings.
classpath = files()
}
checkstyle {
toolVersion '6.19' // set Checkstyle version here
}
应用插件:“checkstyle”
def getChangedFiles={->
试一试{
def stdout=newbytearrayoutputstream()
执行官{
命令行'git','diff','--仅限名称'
标准输出=标准输出
}
返回stdout.toString().trim().split(“\n”)
}
捕获(忽略){
返回null;
}
}
任务检查样式(类型:检查样式){
configFile文件(“${project.rootDir}/quality/checkstyle/unformal checkstyle.xml”)
配置属性=[
'checkstyle.cache.file':rootProject.file('build/checkstyle.cache'),
“checkstyleSuppressionsPath”:文件(${project.rootDir}/quality/checkstyle/suppressions.xml”).absolutePath
]
来源“src”
String[]split=getChangedFiles()
对于(inti=0;i我认为,您实际上想要的是过滤掉在更改的文件中发现的冲突
签出和自述文件中提到的插件。有一些插件可以脱机执行此操作,或者将其与pull请求集成。最近,我基于checkstyle编写了一个工具,仅支持对代码行的增量更改进行样式检查,您可以将此工具添加到Git pre-commit钩子中,以在每次提交之前触发检查
详情请参阅:
虽然现在提你的问题已经太晚了,但我还是希望这能对你将来有所帮助