我是 gradle 的新手,正在尝试配置 Spotbugs。 我已将插件添加到 build.gradle 中,发现了 spotbugs 问题。 但是我想排除 Findbugs EI_EXPOSE_REP 和 EI_EXPOSE_REP2 规则,因为它们会出现在我所有的 getter 和 setter 中。 我在 build.gradle 中有以下片段:
apply plugin: 'java'
apply plugin: 'com.github.spotbugs'
apply plugin: 'findbugs'
spotbugs {
toolVersion = '5.0.0'
}
tasks.withType(SpotBugsTask) {
reports {
xml.enabled = false
html.enabled = true
}
}
findbugs {
excludeFilter = file("$rootProject.projectDir/config/findbugs/excludeFilter.xml")
toolVersion = "3.0.1"
effort = "max"
}
excludeFilter.xml 包含以下内容:
<?xml version="1.0" encoding="UTF-8"?>
<FindBugsFilter>
<Match>
<Bug pattern="EI_EXPOSE_REP"/>
</Match>
<Match>
<Bug pattern="EI_EXPOSE_REP2"/>
</Match>
</FindBugsFilter>
我也试过像这样添加排除:
tasks.withType(FindBugs) {
excludeFilter = file("$rootProject.projectDir/config/findbugs/excludeFilter.xml")
}
但是没有成功,所以我可能遗漏了什么。
尝试将排除过滤器添加到 spotbugs 配置而不是 findbugs: 所以你应该试试:
spotbugs {
toolVersion = '5.0.0'
excludeFilter.set(file("${spotbugsConfigDir}/excludeFilter.xml"))
}
以下行将 SpotBugs 5.0.14 插件添加到 Gradle 项目中:
buildscript {
repositories {
mavenCentral()
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "com.github.spotbugs.snom:spotbugs-gradle-plugin:5.0.14"
}
}
plugins {
id 'application'
id "com.github.spotbugs" version "5.0.14"
}
spotbugs {
excludeFilter.set(
file("${projectDir}/bug-filter.xml")
)
}
在项目的根目录中创建文件
bug-filter.xml
。有关可用选项的详细信息,请参阅过滤器文件文档(或PDF 版本)。将以下简短片段插入文件中以忽略所有类中的 EI 和 EI2 错误:
<?xml version="1.0" encoding="UTF-8"?>
<FindBugsFilter>
<Match>
<Or>
<Bug code="EI, EI2" />
</Or>
</Match>
</FindBugsFilter>
错误描述页面列出了错误代码和模式。
FindBugsFilter
根级元素名称是保留的。