【问题标题】:Can I use property file in maven pom.xml for flyway configuration我可以使用 maven pom.xml 中的属性文件进行飞行路径配置吗
【发布时间】:2012-09-19 02:59:42
【问题描述】:
<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>

我不想在这里提及驱动程序、url 和用户。我已经在src/main/resources 上有一个abc.property。如何在这里使用该文件?

【问题讨论】:

    标签: maven pom.xml flyway


    【解决方案1】:

    看看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>
    

    【讨论】:

    • 供将来参考,这很好用,但是您需要在调用 flay 插件之前显式调用 mvn 初始化,例如mvn initialize flyway:migrate
    • 如果我根据配置文件有不同的属性文件怎么办?比如对于 dev,abc-dev.properties,对于 prod abc-prod.properties
    • 嘿,我们应该在 pom.xml 中哪里定义属性(你提到它是“ ${jdbc.driver}${jdbc.url} ${jdbc.user}"}
    【解决方案2】:

    在 3.0 版中,您必须像这样使用 configFile:

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

    【讨论】:

    【解决方案3】:

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

    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>
            ...
    

    在这里你可以看到 development 配置文件默认被激活。如果你想使用另一个配置文件设置它

    -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 的 pluginManagement 中管理,因此这里看不到版本。我还使用带有位置配置的显式 sql 脚本。

    【讨论】:

    • 如果在 configFile 中,我指定了 src/main/resources/flyway.properties 的路径,即使我在迁移前编译它也不起作用。有什么想法吗?
    • 当您尝试迁移时会发生什么?您是否尝试过在 maven 中使用 -X 选项,它说明了什么?
    • 它说“configFiles = []”,在很多输出中
    【解决方案4】:

    有几种方法可以解决这个问题。一种方法是反其道而行之。

    这意味着要使用的属性保存为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
    

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

    【讨论】:

    • 感谢您的快速回复但主要问题是,我不想在 POM.XML 中声明属性。
    【解决方案5】:

    加里,

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

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

    <?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>
    

    之后你可以根据下面的sn-p修改你的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
    

    【讨论】:

    • 好的,你的例子有效,但是我应该怎么做才能从我的 application-local.yml 或 application-dev.yml 文件中加载值而不是在 pom 中设置值(硬编码)?
    【解决方案6】:

    在 3.x 版本中,您有 configFile 选项

    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>
    

    【讨论】:

      猜你喜欢
      • 2020-05-06
      • 2018-11-30
      • 1970-01-01
      • 2011-01-22
      • 2012-06-30
      • 1970-01-01
      • 2016-01-27
      • 1970-01-01
      • 2013-09-05
      相关资源
      最近更新 更多