【问题标题】:Spotbugs with Gradle show the both reportsGradle 的 Spotbugs 显示了这两个报告
【发布时间】:2021-09-08 14:42:35
【问题描述】:

我正在使用带有 Gradle 的 Spotbugs。

我最近从 Maven 迁移到 Gradle,然后我意识到两个构建器工具中的同一个插件之间存在许多差异...

例如,我想查看带有错误结果的 Spotbugs GUI...在 Maven 中它非常简单。在 Gradle 中,Spotbugs 生成两个任务,每个文件夹(主要和测试)一个。这就是为什么有两个报告(main.xmltest.xml),完全阻碍了GUI的展示。

我正在做一些变通方法来显示两个带有两个报告结果的 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'
  }
}

但它不起作用。有人可以帮我吗?

为了展示解决 spotbugs 与 Maven 插件不相等的完整和详尽的工作,下面是完整的解决方法:

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'
}

【问题讨论】:

    标签: java maven gradle groovy spotbugs


    【解决方案1】:

    (请避免在 Slack 和 StackOverflow 上交叉发布问题,这会适得其反)

    你不应该这样创建你的任务。相反,您的插件应该对 Spotbugs 任务的存在做出反应,这些任务可以为任何源集(主要、测试、功能测试,您命名)创建,并为其创建适当的视图任务。正确声明输入/输出并避免硬编码值很重要。

    你可以用这样的方式让它工作:

    // 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)
            }
        }
    }
    

    【讨论】:

    • 这是一个很好的答案......但它不会同时调用两个报告。它只会以更具可读性且不会过度设计的方式创建任务。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多