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

Spring Boot多模块项目中让Flyway读取Web模块PostgreSQL配置

解决方案

要让Flyway任务复用Web模块中的数据库配置,无需重复编写连接字符串,可在External模块的build.gradle.kts中添加读取并解析Web模块application.properties的逻辑,再将配置绑定到Flyway插件。

修改External模块的build.gradle.kts如下:

plugins {
    id("java-library")
    id("org.flywaydb.flyway") version "11.0.1"
}

repositories {
    mavenCentral()
}

dependencies {
    implementation("org.springframework.boot:spring-boot-starter-data-jpa:3.4.0")
    implementation("org.postgresql:postgresql:42.7.4")
    implementation("org.flywaydb:flyway-core:11.0.1")
    implementation("org.flywaydb:flyway-database-postgresql:11.0.1")
    implementation("org.flywaydb:flyway-gradle-plugin:11.0.1")
}

// 读取Web模块的application.properties配置文件
val webPropertiesFile = file("../Web/src/main/resources/application.properties")
val dbProps = java.util.Properties().apply {
    load(webPropertiesFile.inputStream())
}

// 配置Flyway插件参数
flyway {
    url = dbProps.getProperty("spring.datasource.url").trim()
    user = dbProps.getProperty("spring.datasource.username").trim()
    password = dbProps.getProperty("spring.datasource.password").trim()
    driver = dbProps.getProperty("spring.datasource.driver-class-name").trim()
    // 你的迁移文件路径默认就是db/migration,和现有结构匹配,无需额外配置locations
}

说明:

  • 通过Java的Properties类读取Web模块的配置文件,路径为External模块根目录到Web模块配置文件的相对路径,确保项目结构不变时路径有效。
  • 将读取到的数据库连接参数直接赋值给Flyway插件的对应配置项,实现配置复用,避免重复编写连接信息。
  • 再次执行./gradlew flywayMigrate时,Flyway任务会自动使用Web模块中的数据库连接信息完成迁移操作。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.15 16:20:04