flyway + gradle + spring 引导配置

flyway + gradle + spring boot configuration

如何在 build.gradle 中配置 flyway 以从其他属性文件中获取 url、用户名、密码?

而不是这个:

flyway {
    url = 'jdbc:postgresql://localhost:5432/db'
    user = 'a'
    password = 'a'
    locations = ['filesystem:db/migration']
}

像这样:

flyway {
    path = ['filesystem:src/main/resources/data-access.properties']
    locations = ['filesystem:db/migration']
}

你可以这样做:

ext.flywayProps = new Properties()
flywayProps.load(new FileInputStream(this.projectDir.absolutePath + "/src/main/resources/data-access.properties"))

在构建脚本的根目录中,它会将属性文件加载到 Properties 类型的局部变量中。之后,您可以按照需要的方式使用此属性,例如:

flyway {
    url = 'jdbc:postgresql://flywayProps['dbIp']:flywayProps['dbPort']/db'
    user = flywayProps['dbUsername']
    password = flywayProps['dbPassword']
    locations = ['filesystem:db/migration']
}

并且在您的 data-access.properties 中您需要指定如下:

dbIp=localhost
dbPort=5432
dbUsername=a
dbPassword=a