我可以在 maven pom 中使用属性文件.xml进行飞行路线配置吗?


<plugin>
<groupId>com.googlecode.flyway</groupId>
<artifactId>flyway-maven-plugin</artifactId>
<version>1.7</version>
<configuration>
<driver>com.mysql.jdbc.Driver</driver>
<url>jdbc:mysql://127.0.0.1:3306/db_abc</url>
<user>db_user</user>
<sqlMigrationPrefix>V</sqlMigrationPrefix>
</configuration>
</plugin>

我不想在这里提及驱动程序,网址和用户。我已经对src/main/resourcesabc.property.如何在此处使用该文件?

看看properties-maven-plugin. 它允许您从文件中读取属性,然后在您的 pom 中使用它们。

添加以下插件定义:

<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>src/main/resources/abc.properties</file>
</files>
</configuration>
</execution>
</executions>
</plugin>

如果abc.properties包含:

jdbc.driver = com.mysql.jdbc.Driver
jdbc.url = jdbc:mysql://127.0.0.1:3306/db_ab
jdbc.user = db_user

然后,可以按如下方式使用这些属性:

<!-- language: xml -->
<driver>${jdbc.driver}</driver>
<url>${jdbc.url}</url>
<user>${jdbc.user}</user>

在 3.0 版本中,您必须使用配置文件,例如:

<configFile>src/main/resources/db/config/flyway.properties</configFile>

在我看来,最好和最灵活的方法是:

a)使用配置文件和过滤- 保留特定配置文件(开发、测试等)的所有配置属性,例如在 development.properties 中:

jdbc.url=jdbc:mysql://127.0.0.1:3306/testdb?useSSL=false
jdbc.user=testuser
jdbc.password=testpass
jdbc.driver=com.mysql.jdbc.Driver

然后,在您的 pom 文件中(可能在根 pom 中)定义一个配置文件,例如:

...
<profiles>
<profile>
<id>development</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<build>
<filters>
<filter>../filters/development.properties</filter>
</filters>
</build>
...

在这里,您可以看到开发配置文件默认处于激活状态。如果要使用其他配置文件,请将其设置为

-p [profile-id]


b) 使用过滤值设置 Flyway.Properties - 您的Flyway.Properties应位于 SRC/Main/Resources 中,并且应使用配置文件属性文件中定义的参数中的值:

flyway.driver = ${jdbc.driver}
flyway.url = ${jdbc.url}
flyway.user = ${jdbc.user}
flyway.password = ${jdbc.password}

c)从构建目录中引用 flyway.properties- 使用简单的插件配置(我真的很喜欢干净的poms):

...
<build>
<resources>
<!-- This way we instruct maven to inject values from filters into the resources -->
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-maven-plugin</artifactId>
<configuration>
<configFile>${project.build.directory}/classes/flyway.properties</configFile>
<locations>
<location>classpath:migration/mysql</location>
</locations>
</configuration>
</plugin>
</plugins>
</build>
...

不要忘记在资源中启用筛选,如此处的许多示例所示。我的flyway-maven-plugin 版本是 3.2.1,它在父 pom 的插件管理中管理,因此版本在这里不可见。我还使用带有位置配置的显式 sql 脚本。

有几种方法可以解决这个问题。一种方法是反过来做。

这意味着要使用的属性将另存为pom.xml中的属性,并且文件abc.properties只有将在生成时填充的占位符。

我将向您展示如何配置它。

这是pom.xml的样子:

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.stackoverflow</groupId>
<artifactId>Q12619446</artifactId>
<version>1.0-SNAPSHOT</version>
<name>${project.artifactId}-${project.version}</name>
<properties>
<jdbc.driver>com.mysql.jdbc.Driver</jdbc.driver>
<jdbc.url>jdbc:mysql://127.0.0.1:3306/db_abc</jdbc.url>
<jdbc.user>db_user</jdbc.user>
</properties>
<build>
<plugins>
<plugin>
<groupId>com.googlecode.flyway</groupId>
<artifactId>flyway-maven-plugin</artifactId>
<version>1.7</version>
<configuration>
<driver>${jdbc.driver}</driver>
<url>${jdbc.url}</url>
<user>${jdbc.user}</user>
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
</project>

这将是您的src/main/resources/abc.properties(使用您选择的键名称):

jdbcDriver = ${jdbc.driver}
jdbcUrl = ${jdbc.url}
jdbcUser = ${jdbc.user}

构建后,target/classes/abc.properties将如下所示:

jdbcDriver = com.mysql.jdbc.Driver
jdbcUrl = jdbc:mysql://127.0.0.1:3306/db_abc
jdbcUser = db_user

如前所述,这只是几种方法之一。它可能不适合您的确切需求,但可以。

Garry,

还有一种方法可以不将数据库连接参数放置到 POM 文件中。特别是,可以将它们添加到设置.xml用户文件夹 [1] 的 .m2 子文件夹中的文件。好处是数据库参数不存在于项目文件夹中,也不会传输到存储库,因此每个开发人员都可以使用自己的设置。

设置文件的示例可能类似于下面的示例。

<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<profiles>
<profile>
<id>fw</id>
<properties>
<flyway.url>jdbc:oracle:thin:@//localhost:1521/xe</flyway.url>
<flyway.user>Your login</flyway.user>
<flyway.password>Your password</flyway.password>
</properties>
</profile>
</profiles>
<activeProfiles>
<activeProfile>fw</activeProfile>
</activeProfiles>
</settings>

之后,您可以根据以下代码片段修改您的pom文件。

<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
...
<dependencies>
...
</dependencies>
<profiles>
<profile>
<id>fw</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-maven-plugin</artifactId>
<version>3.2.1</version>
<configuration>
<url>${flyway.url}</url>
<user>${flyway.user}</user>
<password>${flyway.password}</password>
</configuration>
<dependencies>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc7</artifactId>
<version>12.1.0.1.0</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>

在上面的示例中,使用了 Oracle 的驱动程序,但您可以将其替换为所需的驱动程序。

要打印当前设置,请执行以下操作。

mvn help:effective-settings

在 3.x 版中,你有配置文件选项

By default- flyway.properties in the same directory as the project POM.

您可以在pom的配置部分添加外部属性文件,也可以在运行maven目标示例时传递- 命令行-

mvn <goal> -Dflyway.configFile=myConfig.properties 

Pom 文件-

<plugin>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-maven-plugin</artifactId>
<version>3.2.1</version>
<configuration>
<driver/>
<url/>
<user/>
<password/>
<baselineVersion>1.0</baselineVersion>
<baselineDescription>Base Migration</baselineDescription>
<skip>false</skip>
<configFile>myConfig.properties</configFile>
</configuration>
</plugin>

最新更新