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

构建Kotlin Spring WebFlux项目遇Gradle弃用警告及TestContainers构建失败

解决方案

一、消除org.gradle.util.GFileUtils弃用警告

Gradle 8+已移除GFileUtils类,项目出现该警告是因为依赖的插件或自定义代码仍在调用这个废弃API,解决方式:

  • 升级测试容器插件版本:如果项目用到testcontainers-gradle-plugin,将其升级到1.19.3及以上,该版本已适配Gradle 8的API变更。
  • 替换自定义代码中的GFileUtils:如果是自己写的任务用到了该类,改用java.nio.file.Files或org.apache.commons.io.FileUtils替代,比如把GFileUtils.copyDirectory替换为FileUtils.copyDirectory(需引入commons-io依赖)。

二、解决DockerClientProviderStrategy的IllegalStateException

这个异常是测试容器框架无法正确连接Docker环境导致的,即使手动启动PostgreSQL容器也无效——因为测试容器需要自主管理容器实例,和手动启动的容器无关,按以下步骤排查:

  1. 统一测试容器依赖版本
    确保testcontainers核心依赖与PostgreSQL模块版本一致,且适配Java 17和Gradle 8。在build.gradle.kts中通过BOM管理版本:

    dependencyManagement {
        imports {
            mavenBom("org.testcontainers:testcontainers-bom:1.19.3")
        }
    }
    
    dependencies {
        testImplementation("org.testcontainers:junit-jupiter")
        testImplementation("org.testcontainers:postgresql")
    }
    
  2. 显式指定Docker Socket路径(macOS专属)
    macOS上Docker Desktop的Socket路径可能与测试容器默认查找路径不匹配,在测试任务中添加系统属性:

    tasks.withType<Test> {
        // 根据Docker Desktop版本选择其一
        systemProperty("docker.host", "unix:///var/run/docker.sock")
        // 或新版本路径:unix:///Users/你的用户名/.docker/run/docker.sock
    }
    
  3. 检查Docker运行状态与权限

    • 执行docker ps确认当前用户无需sudo即可访问Docker,若需要则重启Docker Desktop(macOS默认已配置用户权限)。
    • 执行docker info验证Docker daemon正常运行,无报错信息。
  4. 临时跳过测试(快速验证构建)
    如果只是想先完成项目构建,可临时禁用测试任务:

    ./gradlew build -x test
    
  5. 修正测试类容器配置
    检查KotlinApplicationTests的容器注解配置是否正确,示例正确配置:

    @SpringBootTest
    @Testcontainers
    class KotlinApplicationTests {
    
        @Container
        val postgresContainer = PostgreSQLContainer("postgres:15-alpine")
            .withDatabaseName("test_db")
            .withUsername("test_user")
            .withPassword("test_pass")
    
        @DynamicPropertySource
        fun setDbProperties(registry: DynamicPropertyRegistry) {
            registry.add("spring.r2dbc.url") {
                "r2dbc:postgresql://${postgresContainer.host}:${postgresContainer.firstMappedPort}/${postgresContainer.databaseName}"
            }
            registry.add("spring.r2dbc.username") { postgresContainer.username }
            registry.add("spring.r2dbc.password") { postgresContainer.password }
        }
    
        @Test
        fun contextLoads() {}
    }
    

三、验证修复效果

执行以下命令重新构建项目:

./gradlew clean build

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.22 00:35:20