【问题标题】:Running flyway commands from command line using spring boot properties for maven使用 Maven 的 Spring Boot 属性从命令行运行 flyway 命令
【发布时间】:2020-10-29 10:24:42
【问题描述】:

我有一个使用 maven 的 spring boot 项目,我已将 flyway 包含在其中:

pom.xml:

    <dependency>
        <groupId>org.flywaydb</groupId>
        <artifactId>flyway-core</artifactId>
        <version>6.5.0</version>
    </dependency>

和application.properties:

#LOCAL
spring.datasource.url=jdbc:postgresql://localhost:5432/theDatabase
spring.datasource.username=theRightUser
spring.datasource.password=theRightPassword

当我运行应用程序时它会按预期工作。

但是,我尝试从命令行运行mvn flyway:clean,但它似乎无法正确识别配置:

[ERROR] Failed to execute goal org.flywaydb:flyway-maven-plugin:6.4.4:clean (default-cli) on project my-service: org.flywaydb.core.api.FlywayException: Unable to connect to the database. Configure the url, user and password! -> [Help 1]

我尝试在 application.properties 文件中添加 spring.flyway 属性 (user/pass/url),但它给了我同样的错误。我需要做什么才能让 flyway 从 application.properies 中读取,就像应用程序正常运行时一样?


编辑:我取得了一些进展:我可以通过将其添加到 pom.xml 中来引用我的 application.properties 作为 flyway 配置文件:

        <plugin>
            <groupId>org.flywaydb</groupId>
            <artifactId>flyway-maven-plugin</artifactId>
            <version>6.5.0</version>
            <configuration>
                <configFiles>${project.basedir}/src/main/resources/application.properties</configFiles>
            </configuration>
        </plugin>

所以现在在那个文件中,我有 flyway.urlflyway.userflyway.password。这使我可以从命令行运行 flyway 目标,但并不完全是我想要的解决方案。我正在研究使用此插件尝试将属性读入 pom.xml 文件,然后在flyway-maven-plugin&lt;configuration&gt; 区域中使用这些值。

        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>properties-maven-plugin</artifactId>
            <version>1.0.0</version>
            <executions>
                <execution>
                    <phase>initialize</phase>
                    <goals>
                        <goal>read-project-properties</goal>
                    </goals>
                    <configuration>
                        <files>
                            <file>${project.basedir}/src/main/resources/application.properties</file>
                        </files>
                    </configuration>
                </execution>
            </executions>
        </plugin>

这将允许我这样做:

        <plugin>
            <groupId>org.flywaydb</groupId>
            <artifactId>flyway-maven-plugin</artifactId>
            <version>6.5.0</version>
            <configuration>
                <url>${spring.datasource.url}</url>
                <user>${spring.datasource.username}</user>
                <password>${spring.datasource.password}</password>
            </configuration>
        </plugin>

【问题讨论】:

    标签: spring-boot maven pom.xml flyway


    【解决方案1】:

    当您将flyway作为maven目标运行时,它不会从application.properties中获取属性,而是使用flyway-maven-plugin提供的configuration,您可以在以下配置flyway-maven-plugin方式-

    将以下插件添加到pom.xml -

    <plugin>
        <groupId>org.flywaydb</groupId>
        <artifactId>flyway-maven-plugin</artifactId>
        <version>6.5.0</version>
    </plugin>
    

    接下来我们配置flyway-maven-plugin,Flyway Maven插件可以通过以下多种方式进行配置(最方便),

    插件的配置部分

    最简单的方法是在你的 pom.xml 中使用插件的配置部分:

    <plugin>
        <groupId>org.flywaydb</groupId>
        <artifactId>flyway-maven-plugin</artifactId>
        <version>6.5.0</version>
        <configuration>
            <driver>org.hsqldb.jdbcDriver</driver>
            <url>jdbc:hsqldb:file:${project.build.directory}/db/flyway_sample;shutdown=true</url>
            <user>SA</user>
            <password>mySecretPwd</password>
            <connectRetries>10</connectRetries>
            <initSql>SET ROLE 'myuser'</initSql>
            <schemas>
                <schema>schema1</schema>
                <schema>schema2</schema>
                <schema>schema3</schema>
            </schemas>
            <callbacks>
                <callback>com.mycompany.project.CustomCallback</callback>
                <callback>com.mycompany.project.AnotherCallback</callback>
            </callbacks>
            <skipDefaultCallbacks>false</skipDefaultCallbacks>
            <cleanDisabled>false</cleanDisabled>
            <skip>false</skip>
            <configFiles>
                <configFile>myConfig.conf</configFile>
                <configFile>other.conf</configFile>
            </configFiles>
            <workingDirectory>/my/working/dir</workingDirectory>
        </configuration>
    </plugin>
    

    Maven 属性

    为了方便使用 Maven 配置文件和对配置进行逻辑分组,Flyway Maven 插件还支持 Maven 属性,更新 pom.xml 中的属性部分如下:

    <project>
        ...
        <properties>
            <!-- Properties are prefixed with flyway. -->
            <flyway.user>myUser</flyway.user>
            <flyway.password>mySecretPwd</flyway.password>
    
            <!-- List are defined as comma-separated values -->
            <flyway.schemas>schema1,schema2,schema3</flyway.schemas>
    
            <!-- Individual placeholders are prefixed by flyway.placeholders. -->
            <flyway.placeholders.keyABC>valueXYZ</flyway.placeholders.keyABC>
            <flyway.placeholders.otherplaceholder>value123</flyway.placeholders.otherplaceholder>
        </properties>
        ...
    </project>
    

    外部配置文件

    另一种方法是创建一个单独的.properties 文件,默认配置文件名为flyway.properties,它应该与pom.xml 文件位于同一目录中。编码由 flyway.encoding 指定(默认为 UTF-8):

    flyway.user=databaseUser
    flyway.password=databasePassword
    flyway.schemas=schemaName
    ...
    

    如果您使用任何其他名称(例如 customConfig.properties)作为配置文件,则应在调用 Maven 命令时明确指定:

    $ mvn <goals> -Dflyway.configFile=customConfig.properties
    

    配置您的flyway-maven-plugin 后,使用所需的配置,我们将能够从命令行执行flyway maven 目标。

    您可以进一步阅读here

    希望这会有所帮助!

    【讨论】:

    • 嗨!我很欣赏这种努力,但我试图让 pom 动态读取属性,然后使用这些值来配置 flyway。我已经编辑了我的问题,我正在尝试使用properties-maven-plugin 来尝试实现这个目标。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多