使用Spring Boot属性和Maven从命令行运行Flyway命令

3

我有一个使用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<configuration>区域中使用这些值。

        <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>
1个回答

3
当您将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默认的配置文件名称为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 goals。

您可以在此处进行进一步阅读。

希望这有所帮助!


你好!我很感激你的努力,但是我正在尝试让pom动态读取属性,然后使用这些值来配置flyway。我已经编辑了我的问题,我正在尝试使用properties-maven-plugin来实现这个目标。 - pennstatephil

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接