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

fnvucqvd  于 2023-01-07  发布在  Java
关注(0)|答案(1)|浏览(137)

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

|-- 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文件中定义这些属性(如liquibase using maven with two databases does not work问题的公认答案中所述),而是希望从外部属性文件中加载它们。

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

<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文件中引用的数据库属性无法解析。
你知道怎么才能做到吗?

gorkyyrv

gorkyyrv1#

我设法让它工作起来,关键是结合使用maven filter 元素和 resource 元素,如Liquibase Documentation中所述。
在maven命令中包含 resources 目标也很重要:

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>

相关问题