【问题标题】:Flyway-maven-plugin how to read the settings from spring boot application.ymlFlyway-maven-plugin 如何从 spring boot application.yml 中读取设置
【发布时间】:2015-03-18 03:27:32
【问题描述】:

在我的 Spring Boot 项目中,我想使用 flyway-maven-plugin。 我的pom:

        <plugin>
            <groupId>org.flywaydb</groupId>
            <artifactId>flyway-maven-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <url>jdbc:mysql://localhost:3306/my_database?autoReconnect=true&amp;useUnicode=true&amp;characterEncoding=UTF-8&amp;connectionCollation=utf8_unicode_ci&amp;characterSetResults=UTF-8</url>
                <user>root</user>
                <password>${spring.datasource.password}</password>
            </configuration>
        </plugin>

这是我的 application.yml

spring:
  profiles.active: default
---
spring:
  profiles: default
spring.datasource:
  password: root

据我所知,要使用mvn flyway:info,我需要一些插件来读取我的 application.yml。或者也许还有其他方法?

【问题讨论】:

  • 你为什么要使用这个插件? Spring Boot 会为您执行 flyway,无需在您的 maven 文件中执行此操作。

标签: java spring-boot database-migration flyway


【解决方案1】:

我的src/main/resources/application.properties中有以下内容

flyway.url=jdbc:sqlserver://localhost:1433
flyway.user=james_hetfield
flyway.password=MetaLLic@

spring.datasource.url=${flyway.url}
spring.datasource.user=${flyway.user}
spring.datasource.password=${flyway.password}

然后我从命令行运行迁移,如下所示

mvn -Dflyway.configFiles=src/main/resources/application.properties flyway:migrate

【讨论】:

    【解决方案2】:

    我的理解是,如果您在 Spring Boot 中使用 Flyway,则不要使用 flyway-maven 插件。

    编辑:当您需要直接使用 DB 来修改 Flyway 的模式表时,使用 Flyway CLI 很有用。例如init 现有数据库或clean 测试项目。

    这些是当前设置,可以通过在 application.properties 中为 Flyway 设置属性来开箱即用地执行。

    # FLYWAY (FlywayProperties)
    flyway.check-location=false # check that migration scripts location exists
    flyway.locations=classpath:db/migration # locations of migrations scripts
    flyway.schemas= # schemas to update
    flyway.init-version= 1 # version to start migration
    flyway.init-sqls= # SQL statements to execute to initialize a connection immediately after obtaining it
    flyway.sql-migration-prefix=V
    flyway.sql-migration-suffix=.sql
    flyway.enabled=true
    flyway.url= # JDBC url if you want Flyway to create its own DataSource
    flyway.user= # JDBC username if you want Flyway to create its own DataSource
    flyway.password= # JDBC password if you want Flyway to create its own DataSource
    

    YAML 版本:

    flyway:
        check-location: false
        locations: classpath:db/migration
        ....
    

    如果您需要进一步自定义,您可能需要编写@Bean 以进一步自定义 Flyway 与您的应用程序交互的方式。

    【讨论】:

    • 不使用 CLI 或 maven 插件无法运行“信息、验证、修复”和其他功能。另外我没有使用application.properties。我有 application.yml。
    • 你是对的,除非你修改了Spring Boot的配置。考虑init 现有数据库的工作流程。 Flyway CLI 会很棒。然后让 Spring Boot 处理迁移。
    • Spring Boot 中的 Flyway 支持 YAML。
    • 你有例子吗?
    • 假设你在谈论如何在yaml中使用Flyway
    【解决方案3】:

    要从 pom 中读取 *.yml 属性,您应该使用 https://github.com/aadnk/yaml-properties-plugin

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-22
      • 2019-10-27
      • 2019-05-19
      • 1970-01-01
      • 1970-01-01
      • 2022-01-09
      • 2020-06-09
      • 2022-10-04
      相关资源
      最近更新 更多