You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Gradle脚本自动实现Allure报告历史趋势展示?

解决Allure Gradle插件历史趋势面板为空的自动化方案

方法一:自定义Gradle复制任务自动同步历史数据

添加以下任务到你的build.gradle(或build.gradle.kts)中,实现构建时自动将allure-report下的history目录复制到allure-results:

Groovy DSL

// 定义复制Allure历史数据的任务
task copyAllureHistory(type: Copy) {
    // 源目录:上次生成报告的历史数据位置
    from "${buildDir}/allure-report/history"
    // 目标目录:Allure结果目录,用于生成带趋势的新报告
    into "${buildDir}/allure-results/history"
    // 仅当源目录存在时执行,避免首次构建(无历史数据)报错
    onlyIf { file("${buildDir}/allure-report/history").exists() }
}

// 确保在生成Allure报告前完成历史数据复制
allureReport.dependsOn copyAllureHistory

// 可选:测试任务执行完成后自动触发复制,保证本地开发流程连贯
test.finalizedBy copyAllureHistory

Kotlin DSL

// 定义复制Allure历史数据的任务
tasks.register<Copy>("copyAllureHistory") {
    from("${buildDir}/allure-report/history")
    into("${buildDir}/allure-results/history")
    onlyIf { file("${buildDir}/allure-report/history").exists() }
}

// 关联任务依赖,确保报告生成前同步历史数据
tasks.allureReport {
    dependsOn(tasks.named("copyAllureHistory"))
}

// 可选:测试完成后自动复制历史数据
tasks.test {
    finalizedBy(tasks.named("copyAllureHistory"))
}

方法二:配置Allure Gradle插件自动加载历史数据

如果使用最新版allure-gradle插件,可直接通过插件配置指定历史数据路径,让插件自动处理历史同步:

Groovy DSL

allure {
    version = "2.21.0"
    useJUnit5()
    useJUnit4()
    configuration {
        // 指定历史数据目录,插件会自动将该目录下的历史注入到allure-results
        historyPath = file("${buildDir}/allure-report/history")
    }
}

Kotlin DSL

allure {
    version.set("2.21.0")
    useJUnit5()
    useJUnit4()
    configuration {
        historyPath.set(file("${buildDir}/allure-report/history"))
    }
}

GitLab CI环境适配(团队协作场景)

为了让团队成员拉取代码后能自动获取历史趋势,需在GitLab CI配置中缓存历史数据,避免每次构建从零开始:

在.gitlab-ci.yml中添加缓存配置:

cache:
  paths:
    - build/allure-report/history/
    - build/allure-results/history/

这样每次CI构建都会保留历史数据,后续构建时可直接复用,生成包含趋势的报告。

为什么之前的尝试无效?

  • 修改allure.properties的allure.history.directory是Allure命令行工具的配置,但allure-gradle插件有独立的配置逻辑,不会直接读取该属性。
  • --report-dir参数仅用于指定Allure报告的输出目录,与历史数据的加载无关。

内容的提问来源于stack exchange,提问作者Raza Nasir

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 07:35:27