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

Gradle从5.x升级至7.x时出现“Configuration with name 'integrationTestCompile' not found”错误的解决方法(Kotlin DSL)

修复Gradle 7.x升级中的integrationTestCompile找不到及兼容性问题

这个问题我升级Gradle的时候也踩过坑,核心原因是Gradle 6.0+彻底废弃了compile/runtime系列的配置命名,转而使用implementation/runtimeOnly这类更清晰的命名。你的代码还在引用已经被移除的integrationTestCompile配置,自然会报错;同时旧的配置用法也是Gradle 8.0会淘汰的废弃特性,所以需要同步调整代码。

具体修复步骤:

  • 移除旧的配置引用:删掉手动获取integrationTestCompile和integrationTestRuntimeOnly的代码,Gradle在你创建integrationTest sourceSet时,会自动生成integrationTestImplementation、integrationTestRuntimeOnly等对应新配置。
  • 扩展正确的配置:让integrationTest的实现配置继承testImplementation,运行时配置继承testRuntimeOnly,直接通过sourceSet关联即可。
  • 适配Gradle 7.x的API规范:确保所有sourceSet和配置的获取使用get()或者named(),避免隐式调用带来的兼容性问题。

修复后的完整Kotlin DSL代码:

sourceSets {
    create("integrationTest") {
        // 关联主代码和测试代码的输出
        compileClasspath += sourceSets.main.get().output
        runtimeClasspath += sourceSets.main.get().output
        runtimeClasspath += sourceSets.test.get().output
        
        // 让integrationTest的配置继承test的配置
        configurations.named(this.implementationConfigurationName) {
            extendsFrom(configurations.testImplementation.get())
        }
        configurations.named(this.runtimeOnlyConfigurationName) {
            extendsFrom(configurations.testRuntimeOnly.get())
        }
    }
}

tasks {
    val integrationTest by creating(Test::class) {
        description = "Running integration tests."
        group = "verification"
        testClassesDirs = sourceSets["integrationTest"].output.classesDirs
        classpath = sourceSets["integrationTest"].runtimeClasspath
        shouldRunAfter("test")
    }
}

// 可选:将integrationTest加入check任务的依赖,执行check时自动跑集成测试
tasks.check {
    dependsOn(tasks.named("integrationTest"))
}

额外说明:

  • 用this.implementationConfigurationName和runtimeOnlyConfigurationName是为了和sourceSet的命名保持一致,避免硬编码配置名,更符合Gradle的最佳实践。
  • 这样修改后,不仅解决了Configuration not found的错误,还移除了废弃特性的使用,完全兼容Gradle 7.x,同时也能平滑过渡到Gradle 8.x。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.01 01:52:32