Flyway + gradle + Spring Boot 配置

Lus*_*usi 3 gradle flyway spring-boot

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

而不是这个:

flyway {
    url = 'jdbc:postgresql://localhost:5432/db'
    user = 'a'
    password = 'a'
    locations = ['filesystem:db/migration']
}
Run Code Online (Sandbox Code Playgroud)

像这样的东西:

flyway {
    path = ['filesystem:src/main/resources/data-access.properties']
    locations = ['filesystem:db/migration']
}
Run Code Online (Sandbox Code Playgroud)

Sta*_*lav 6

你可以这样做:

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

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

flyway {
    url = 'jdbc:postgresql://flywayProps['dbIp']:flywayProps['dbPort']/db'
    user = flywayProps['dbUsername']
    password = flywayProps['dbPassword']
    locations = ['filesystem:db/migration']
}
Run Code Online (Sandbox Code Playgroud)

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

dbIp=localhost
dbPort=5432
dbUsername=a
dbPassword=a
Run Code Online (Sandbox Code Playgroud)