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

Spring项目中使用Flyway迁移PostgreSQL SQL脚本失败排查

Flyway迁移任务失败问题排查与解决

问题描述

基于Spring Boot 2.6.4构建应用,引入Flyway 9.1.6实现数据库迁移,SQL脚本放置在src/main/resources/db/migration目录,配置完build.gradle后执行gradle flywayMigrate -i,日志显示成功验证3个迁移脚本,但任务被标记为失败。

原build.gradle配置

plugins {
    id 'org.springframework.boot' version '2.6.4'
    id 'io.spring.dependency-management' version '1.0.11.RELEASE'
    id 'java'
    id "org.flywaydb.flyway" version "9.1.6"
}

group = 'com.test'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '17'

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'org.postgresql:postgresql'
    compileOnly 'org.projectlombok:lombok'
    compileOnly 'org.flywaydb:flyway-core'
    annotationProcessor 'org.projectlombok:lombok'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

flyway {
    url = 'jdbc:postgresql://localhost:5432/test'
    user = 'postgres'
    password = 'postgres'
    driver = 'org.postgresql.Driver'
    schemas = ['public']
    locations = ['filesystem:src/main/resources/db/migration/']
}

tasks.named('test') {
    useJUnitPlatform()
}

执行命令后的报错日志

> Task :flywayMigrate FAILED
Caching disabled for task ':flywayMigrate' because:
  Build cache is disabled
Task ':flywayMigrate' is not up-to-date because:
  Task has not declared any outputs despite executing actions.
Resolving global dependency management for project 'myapp.main.module'
Excluding []
Excluding []
Excluding []
Excluding []
Database: jdbc:postgresql://localhost:5432/test (PostgreSQL 14.4)
Successfully validated 3 migrations (execution time 00:00.010s)
:flywayMigrate (Thread[Execution worker Thread 3,5,main]) completed. Took 0.396 secs.

问题分析与解决

问题核心出在依赖配置和版本兼容性,具体修复步骤如下:

  1. 修正Flyway Core依赖作用域

    • 原配置中flyway-core使用compileOnly,仅编译阶段可用,而Flyway插件执行迁移任务需要运行时依赖,导致插件无法加载必要类。
    • 修复:将compileOnly 'org.flywaydb:flyway-core'改为implementation 'org.flywaydb:flyway-core',也可直接移除该依赖(Flyway插件会自动引入核心依赖)。
  2. 适配Spring Boot与Flyway版本

    • Spring Boot 2.6.4默认适配Flyway 8.5.13,使用更高版本的Flyway插件(9.1.6)会存在API兼容问题。
    • 修复二选一:
      • 调整Flyway插件版本为Spring Boot兼容版:
        id "org.flywaydb.flyway" version "8.5.13"
        
      • 显式指定Flyway版本覆盖Spring Boot默认配置:
        dependencyManagement {
            dependencies {
                dependency 'org.flywaydb:flyway-core:9.1.6'
            }
        }
        
  3. 移除冗余配置项

    • locations默认值就是filesystem:src/main/resources/db/migration,可删除;
    • driver无需手动指定,Flyway会根据JDBC URL自动识别驱动类,可删除。

修改后的build.gradle示例

plugins {
    id 'org.springframework.boot' version '2.6.4'
    id 'io.spring.dependency-management' version '1.0.11.RELEASE'
    id 'java'
    id "org.flywaydb.flyway" version "8.5.13" // 使用Spring Boot兼容版本
}

group = 'com.test'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '17'

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'org.postgresql:postgresql'
    compileOnly 'org.projectlombok:lombok'
    implementation 'org.flywaydb:flyway-core' // 修正依赖作用域
    annotationProcessor 'org.projectlombok:lombok'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

flyway {
    url = 'jdbc:postgresql://localhost:5432/test'
    user = 'postgres'
    password = 'postgres'
    schemas = ['public']
    // 移除冗余的locations和driver配置
}

tasks.named('test') {
    useJUnitPlatform()
}

验证

修改配置后重新执行gradle flywayMigrate -i,任务应能正常完成,数据库迁移脚本会被正确执行。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.21 10:27:27