2014-03-12 23 views
6

我可以通過Maven構建(liquibase:update目標)運行Liquibase更新日誌,沒有任何問題。現在,我想Liquibase使用數據庫憑據和URL從屬性文件加載(db.properties)根據所選擇的Maven的配置文件:從Maven構建中的Liquibase使用的屬性文件加載數據

|-- 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.我使用Maven的resource元素在POM文件:

<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:更新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文件中引用的數據庫屬性。

任何想法如何實現?

回答

14

我設法讓這個工作。關鍵是如Liquibase Documentation中所解釋的,使用元素元素與元素元素一起使用。

mvn resources:resources liquibase:update -Plocal 

這是我使用的文件層次:

而且它包括資源在Maven命令目標是非常重要的

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

liqui base.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> 
相關問題