从属性文件加载数据,以供 Maven 构建中的 Liquibase 使用

2022-09-03 13:11:20

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

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

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

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

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

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

<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. 我尝试使用属性 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:update maven 目标时,我收到以下错误:

[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 文件中引用的数据库属性。

任何想法如何实现这一点?


答案 1

我设法让它工作。关键是将 maven 过滤器元件与资源元素结合使用,如 Liquibase 文档中所述。

同样重要的是,在 maven 命令中包含资源目标:

mvn resources:resources liquibase:update -Plocal

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

|-- 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 文件将如下所示:

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

liquibase.properties 文件将如下所示:

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

POM 文件将如下所示:

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

答案 2

推荐