簡體   English   中英

如何從外部屬性文件中填充Liquibase參數值?

[英]How can I populate Liquibase parameter values from an external properties file?

有沒有辦法可以根據外部屬性文件的內容在Liquibase changelog文件中填充參數

如我所願,我想說:

<createTable tableName="${table.name}">
     <column name="id" type="int"/>
     <column name="${column1.name}" type="varchar(${column1.length})"/>
     <column name="${column2.name}" type="int"/>
</createTable>

並將table.name和其他參數的值保存在外部文件db.properties ,並在更改日志或Liquibase命令行中引用此文件,或者作為運行liquibase的Maven插件的選項引用此文件。

我似乎無法找到任何辦法,這可能嗎?

在編譯時這樣做:聽起來像maven 過濾器和/或配置文件的工作

注意:小心使用liquibase和任何“標記”替換... liquibase存儲應用的更改集的CRC

的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>test</groupId>
    <artifactId>test</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <build>
        <filters>
            <filter>src/main/filters/liquibase.properties</filter>
        </filters>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
                <includes>
                    <include>liquibase.xml</include>
                </includes>
            </resource>
        </resources>
    </build>
</project>

/src/main/filters/liquibase.properties

table.name=TABLE_NAME
column1.name=COLUMN1_NAME
column1.length=10
column2.name=COLUMN2_NAME

/src/main/resources/liquibase.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<databaseChangeLog logicalFilePath="liquibase.xml" xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-2.0.xsd">

    <changeSet author="me" id="changeSetId1">
        <comment>Test</comment>

        <createTable tableName="${table.name}">
            <column name="id" type="int" />
            <column name="${column1.name}" type="varchar(${column1.length})" />
            <column name="${column2.name}" type="int" />
        </createTable>
    </changeSet>

</databaseChangeLog>

編輯: 典型的調用(使用過濾的資源)將如下所示: mvn resources:resources liquibase:update或更優選使用配置文件... mvn resources:resources liquibase:update -P<profile_name>

EDIT2:這種定義列的方法有一個很大的優點。 您可以使用此屬性(例如:column1.length)值(例如:10)來驗證每個層:Hibernate,DAO,WEB,faces,JavaScript。 只需在需要驗證它的每個地方使用此屬性。 即使在i18n / messages.properties中,如果需要(例如:input1.validation =不超過$ {column1.length}個字母。)。

唯一的復雜因素是,如果您需要更改此值,則需要提供正確的liquibase更新/回滾腳本。 update changescript using new property/value. 有時可以更改值並設置新的liquibase校驗和(安全操作,如增加varchar長度),但有時您需要使用新屬性/值創建更新更改腳本。

看看這里 您可以使用命令行參數( -D[arg name]=[arg value] )或環境變量。 如果不使用任何構建管理器工具(如Maven或Ant),則需要編寫用於從文件讀取參數並將其傳遞給命令的腳本。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM