使用maven的spring-boot属性从命令行运行flyway命令



我有一个使用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从应用程序中读取操作,就像应用程序正常运行时一样?


EDIT:我取得了一些进展:通过将application.properties添加到pom.xml:中,我可以将其引用为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>

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

将以下插件添加到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指定。编码(默认为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目标。

你可以在这里做进一步的阅读。

希望这能有所帮助!

最新更新