我在Gradle中使用Spotbugs。
我最近从Maven转到了Gradle,然后我意识到两个构建器工具中相同的插件之间有很多不同之处……
例如,我希望看到带有bug结果的Spotbugs GUI。在Maven中,这非常非常简单。在Gradle中,Spotbugs为每个文件夹(main和test)生成两个任务。这就是为什么有两个报告(main.xml和test.xml),完全困难的图形用户界面的显示。
我正在做一些变通方法来显示两个GUI和两个报告结果。但我在调用一个任务时遇到了一些麻烦,因为另一个任务中有参数...我正在做的事情似乎是这样的:
task spotbugsViewReports {
def reports = ['main', 'test']
for(String report : reports) {
def name = "spotbugsView_${report}"
task (name)(type: Exec) {
commandLine "java", "-jar", "${buildDir}/unpacked/dist/spotbugs-4.4.0/lib/spotbugs.jar"
args '-loadBugs', "${buildDir}/reports/spotbugs/${report}.xml"
}
}
doLast {
dependsOn 'spotbugsView_main'
dependsOn 'spotbugsView_test'
}
}但它不起作用。有人能帮帮我吗?
为了展示完整和详尽的工作,以解决与Maven插件不相等的spotbugs,下面是完成此操作的完整解决方案:
task spotbugsGui {
finalizedBy 'spotbugsUnzip'
}
task spotbugsUnzip(type: Copy) {
def zipFile = file(project.configurations.artifact.find {
it.name.startsWith("spotbugs")
})
def outputDir = file("${buildDir}/unpacked/dist")
from zipTree(zipFile)
into outputDir
finalizedBy 'spotbugsRegisterGui'
}
task spotbugsRegisterGui {
def reports = ['main', 'test']
for(String report : reports) {
tasks.create("spotbugsExecGui_${report}", Exec) {
commandLine "java", "-jar", "${buildDir}/unpacked/dist/spotbugs-4.4.0/lib/spotbugs.jar"
args '-loadBugs', "${buildDir}/reports/spotbugs/${report}.xml"
}
}
finalizedBy 'spotbugsExecGui'
}
task spotbugsExecGui {
dependsOn 'spotbugsExecGui_main'
dependsOn 'spotbugsExecGui_test'
}发布于 2021-09-08 17:25:39
(请避免在Slack和StackOverflow上交叉发布问题,这会适得其反)
你不应该这样创建你的任务。相反,您的插件应该对Spotbugs任务的存在做出反应,Spotbugs任务可以为任何源集(main、test、functional test等)创建,并为其创建适当的视图任务。正确声明输入/输出并避免硬编码值是很重要的。
你可以用下面这样的代码让它工作:
// For each SpotBugsTask, we want to create a GUI task
tasks.withType(SpotBugsTask) { spotbugs ->
tasks.register("${spotbugs.name}View", SpotBugsView) {
dependsOn spotbugs
description = "Opens the Spotbugs UI for ${spotbugs.name}"
spotBugsClasspath.from(spotbugs.spotbugsClasspath)
reportsDir = spotbugs.reportsDir
}
}
abstract class SpotBugsView extends DefaultTask {
@InputFiles
abstract ConfigurableFileCollection getSpotBugsClasspath()
@InputDirectory
abstract DirectoryProperty getReportsDir()
@javax.inject.Inject
abstract ExecOperations getExecOperations()
@TaskAction
void execute() {
String reportPaths = reportsDir.asFileTree
.files
.stream()
.map(File::getAbsolutePath)
.collect(Collectors.joining(","))
execOperations.javaexec {
it.classpath(spotBugsClasspath)
it.mainClass.set("edu.umd.cs.findbugs.LaunchAppropriateUI")
it.args("-loadBugs", reportPaths)
}
}
}https://stackoverflow.com/questions/69105163
复制相似问题