从属性文件加载数据,以供Liquibase在Maven构建中使用 [英] Loading data from properties file to be used by Liquibase in Maven build

查看:196
本文介绍了从属性文件加载数据,以供Liquibase在Maven构建中使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以通过maven build( liquibase:update 目标)运行Liquibase changelog而不会出现任何问题。现在,我希望Liquibase使用数据库凭据和从属性文件( db.properties )加载的URL,具体取决于所选的Maven配置文件:

I can run Liquibase changelog through maven build (liquibase:update goal) without any problems. Now I'd like Liquibase to use database credentials and URL loaded from a properties file (db.properties) depending on the selected Maven profile:

|-- pom.xml
`-- src
    `-- main
        `-- resources
            |-- local
            |   `-- db.properties
            |-- dev
            |   `-- db.properties
            |-- prod
            |   `-- db.properties
            `-- db-changelog-master.xml
            `-- db-changelog-1.0.xml

3个属性文件中的每一个都如下所示:

Each of the 3 properties files would look like the following:

database.driver = oracle.jdbc.driver.OracleDriver
database.url = jdbc:oracle:thin:@<host_name>:<port_number>/instance
database.username = user
database.password = password123

现在,而不是在POM文件本身中定义这些属性(如这个问题的公认答案 liquibase使用带有两个数据库的maven不工作),我希望他们从外部属性文件加载。我尝试了不同的方法无济于事:

Now, instead of these properties being defined in the POM file itself (as explained in the accepted answer of this question liquibase using maven with two databases does not work), I'd like them to be loaded from the external properties file. I have tried different approaches to no avail:

1。我在POM文件中使用了Maven的资源元素:

1. I used Maven's resource element in the POM file:

<build>
      <pluginManagement>
         <plugins>
            <plugin>
               <groupId>org.liquibase</groupId>
               <artifactId>liquibase-maven-plugin</artifactId>
               <version>3.1.0</version>
               <configuration>
                  <changeLogFile>db.changelog-master.xml</changeLogFile>
                  <verbose>true</verbose>
               </configuration>
            </plugin>
         </plugins>
      </pluginManagement>
   </build>


<profiles>
    <profile>
        <id>local</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <build>
            <resources>
                <resource>
                    <directory>src/main/resources/local</directory>
                </resource>
            </resources>
        </build>
        <properties>
            <liquibase.url>${database.url}</liquibase.url>
            <liquibase.driver>${database.driver}</liquibase.driver>
            <liquibase.username>${database.username}</liquibase.username>
            <liquibase.password>${database.password}</liquibase.password>
        </properties>
    </profile>
    <profile>
        <id>dev</id>
        <build>
            <resources>
                <resource>
                    <directory>src/main/resources/dev</directory>
                </resource>
            </resources>
        </build>
        <properties>
            <liquibase.url>${database.url}</liquibase.url>
            <liquibase.driver>${database.driver}</liquibase.driver>
            <liquibase.username>${database.username}</liquibase.username>
            <liquibase.password>${database.password}</liquibase.password>
        </properties>
    </profile>
</profiles>

2。我尝试使用Properties Maven插件:

   <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>properties-maven-plugin</artifactId>
      <version>1.0-alpha-2</version>
      <executions>
         <execution>
            <phase>initialize</phase>
            <goals>
               <goal>read-project-properties</goal>
            </goals>
            <configuration>
               <files>
                  <file>src/main/resources/local/db.properties</file>
               </files>
            </configuration>
         </execution>
      </executions>
   </plugin>

当我运行liquibase:更新maven目标时,我收到此错误:

When I run the liquibase:update maven goal, I get this this error:

[ERROR] Failed to execute goal org.liquibase:liquibase-maven-plugin:3.1.0:update (default-cli) on project my-project: The driver has not been specified either as a parameter or in a properties file

似乎无法解析POM文件中引用的数据库属性。

It seems that the database properties referred to in the POM file couldn't be resolved.

知道如何实现这一目标吗?

Any idea how this can be achieved ?

推荐答案

我设法让这个工作。关键是将maven filter 元素与 resource 元素结合使用,如 Liquibase文档

I managed to get this working. The key was to use the maven filter element in conjunction with the resource element as explained in Liquibase Documentation.

在maven命令中包含 resources 目标也很重要:

Also it's important to include the resources goal in the maven command:

mvn resources:resources liquibase:update -Plocal

这是我使用的文件层次结构:

This is the file hierarchy I used:

|-- pom.xml
`-- src
    `-- main
       |-- resources
       |   `-- liquibase.properties
       |   |-- changelog
       |       `-- db-changelog-master.xml
       |       `-- db-changelog-1.0.xml
       |-- filters
           |-- local
           |   `-- db.properties
           |-- dev
           |   `-- db.properties

db.properties文件如下所示:

The db.properties file would look like the following:

database.driver = oracle.jdbc.driver.OracleDriver
database.url = jdbc:oracle:thin:@<host_name>:<port_number>/instance
database.username = user
database.password = password123

liquibase.properties文件如下所示:

The liquibase.properties file would look like the following:

changeLogFile: changelog/db.changelog-master.xml
driver: ${database.driver}
url: ${database.url}
username: ${database.username}
password: ${database.password}
verbose: true

POM文件如下所示:

The POM file would look like the following:

<build>
      <pluginManagement>
         <plugins>
            <plugin>
               <groupId>org.liquibase</groupId>
               <artifactId>liquibase-maven-plugin</artifactId>
               <version>3.1.0</version>
               <configuration>
                  <propertyFile>target/classes/liquibase.properties</propertyFile>
               </configuration>
            </plugin>
         </plugins>
      </pluginManagement>
   </build>


<profiles>
    <profile>
        <id>local</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <build>
            <filters>
                <filter>src/main/filters/local/db.properties</filter>
            </filters>
            <resources>
                <resource>
                    <directory>src/main/resources</directory>
                    <filtering>true</filtering>
                </resource>
            </resources>
        </build>
    </profile>
    <profile>
        <id>dev</id>
        <build>
            <filters>
                <filter>src/main/filters/dev/db.properties</filter>
            </filters>
            <resources>
                <resource>
                    <directory>src/main/resources</directory>
                    <filtering>true</filtering>
                </resource>
            </resources>
        </build>
    </profile>
</profiles>

这篇关于从属性文件加载数据,以供Liquibase在Maven构建中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆