Gradle 3.3中如何设置自定义报告目录?
修改Gradle测试报告默认输出路径的正确方式
这个问题我之前也碰到过!你报错的核心原因是:destinationDir并不是test任务的直接属性,Gradle中测试相关的报告是通过任务内部的reports块来配置的,不同类型的报告(HTML、XML等)有各自独立的输出路径设置项。
下面分两种常用的DSL语法给出具体配置方案:
Groovy DSL(build.gradle)
针对JUnit测试报告,直接在test任务的reports块中指定各格式报告的输出路径:
test { reports { // 配置HTML测试报告路径 html.destination = file("$projectDir/custom-reports/test/html") // 配置XML测试报告路径 xml.destination = file("$projectDir/custom-reports/test/xml") // 部分Gradle版本中JUnit XML报告用这个属性 junitXml.destination = file("$projectDir/custom-reports/test/junit-xml") } }
如果需要修改Jacoco覆盖率报告的路径,要单独配置jacocoTestReport任务:
jacocoTestReport { reports { html.destination = file("$projectDir/custom-reports/jacoco/html") xml.destination = file("$projectDir/custom-reports/jacoco/xml") } }
Kotlin DSL(build.gradle.kts)
Kotlin DSL的语法略有不同,需要用outputLocation结合layout来配置:
tasks.test { reports { // 确保HTML报告启用 html.required.set(true) html.outputLocation.set(layout.projectDirectory.dir("custom-reports/test/html")) // 确保XML报告启用 xml.required.set(true) xml.outputLocation.set(layout.projectDirectory.dir("custom-reports/test/xml")) // JUnit XML报告配置 junitXml.outputLocation.set(layout.projectDirectory.dir("custom-reports/test/junit-xml")) } }
Jacoco报告的Kotlin DSL配置:
tasks.jacocoTestReport { reports { html.outputLocation.set(layout.projectDirectory.dir("custom-reports/jacoco/html")) xml.outputLocation.set(layout.projectDirectory.dir("custom-reports/jacoco/xml")) } }
配置完成后,执行./gradlew test或对应的报告任务(比如jacocoTestReport),报告就会生成到你指定的custom-reports目录下了。
内容的提问来源于stack exchange,提问作者Chandrashekhar Swami
相关产品推荐
相关产品推荐

