我有以下Gradle构建文件:https://github.com/markuswustenberg/jsense/blob/a796055f984ec309db3cc0f3e8340cbccac36e4e/jsense-protobuf/build.gradle其中包括:
checkstyle {
// TODO The excludes are not working, ignore failures for now
//excludes '**/org/jsense/serialize/protobuf/gen/**'
ignoreFailures = true
showViolations = false
}
findbugs {
// TODO The excludes are not working, ignore failures for now
//excludes '**/org/jsense/serialize/protobuf/gen/**'
ignoreFailures = true
}
正如你所看到的,我正在尝试排除包org.jsense.serialize.protobuf.gen中自动生成的代码。我无法弄清楚excludes参数的字符串格式,文档也没有多大帮助:http://www.gradle.org/docs/1.10/dsl/org.gradle.api.plugins.quality.FindBugs.html#org.gradle.api.plugins.quality.FindBugs:excludes(它只是说"排除模式的集合。")。
所以我的问题是:对于Findbugs和Checkstyle插件,excludes模式字符串应该如何格式化?
我在考1.10级。
谢谢!
编辑1:我得到了Checkstyle排除处理以下内容:
tasks.withType(Checkstyle) {
exclude '**/org/jsense/serialize/protobuf/gen/**'
}
然而,在Findbugs插件上使用完全相同的排除是不起作用的:
tasks.withType(FindBugs) {
exclude '**/org/jsense/serialize/protobuf/gen/*'
}
EDIT2:接受的答案有效,使用XML文件并对其进行过滤也是如此,比如:
findbugs {
excludeFilter = file("$projectDir/config/findbugs/excludeFilter.xml")
}
和
<?xml version="1.0" encoding="UTF-8"?>
<FindBugsFilter>
<Match>
<Package name="org.jsense.serialize.protobuf.gen"/>
</Match>
</FindBugsFilter>
EDIT 3:这非常有效,不需要XML文件:
def excludePattern = 'org/jsense/serialize/protobuf/gen/'
def excludePatternAntStyle = '**/' + excludePattern + '*'
tasks.withType(FindBugs) {
classes = classes.filter {
!it.path.contains(excludePattern)
}
}
tasks.withType(Checkstyle) {
exclude excludePatternAntStyle
}
tasks.withType(Pmd) {
exclude excludePatternAntStyle
}
SourceTask#exclude
过滤源文件。然而,FindBugs主要对类文件进行操作,您还必须对其进行筛选。试试类似的东西:
tasks.withType(FindBugs) {
exclude '**/org/jsense/serialize/protobuf/gen/*'
classes = classes.filter {
!it.path.contains(new File("org/jsense/serialize/protobuf/gen/").path)
}
}
PS:在FindBug的情况下,过滤源文件可能没有什么区别(因此没有必要)。(不过我还没试过。)
如果有人在寻找现代解决方案:
对于checkstyle,你可以在build.gradle:中使用这样的东西
checkstyleMain.exclude '**/org/jsense/serialize/protobuf/gen/**'
如果要排除多个路径
解决方案1:
checkstyleMain.exclude '**/org/jsense/serialize/protobuf/gen/**'
checkstyleMain.exclude '**/org/example/some/random/path/**'
解决方案2:
checkstyleMain {
setExcludes(new HashSet(['**/org/jsense/serialize/protobuf/gen/**', '**/org/example/some/random/path/**']))
}