如何从外部属性文件填充Liquibase参数值? [英] How can I populate Liquibase parameter values from an external properties file?

查看:129
本文介绍了如何从外部属性文件填充Liquibase参数值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以在基于以下内容的Liquibase更改日志文件中填充参数外部属性文件的内容?

Is there any way that I can populate parameters in a Liquibase changelog file based on the contents of an external property file?

我想这样说:

<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中,并可以从changelog或Liquibase命令行中引用此文件,也可以作为运行的Maven插件的一个选项来引用该文件. liquibase.

And keep the value of table.name and the other parameters in an external file db.properties, and reference this file either from within the changelog, or from the Liquibase command line, or as an option of the Maven plugin that runs liquibase.

我似乎找不到任何方法可以这样做吗?

I can't seem to find any way to do this, is it possible?

推荐答案

在编译时执行以下操作: 听起来像是maven的工作过滤器和/或配置文件

Do that at compile time: sounds like job for maven filters and/or profiles

注意:请谨慎使用liquibase和任何标记"替换物... liquibase存储已应用更改集的CRC

NOTE: be carefull with liquibase and any "marker" replacements... liquibase stores CRC of applied changessets

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

/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

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

典型调用(使用

A typical invocation (using filtered resources) would look like this: mvn resources:resources liquibase:update or more preferably use profiles... mvn resources:resources liquibase:update -P<profile_name>

这种定义列的方式有一个很大的优势.您可以使用此属性的(例如:column1.length)值(例如:10)来验证层:Hibernate,DAO,WEB,faces,JavaScript.只需在需要对其进行验证的每个位置使用此属性.甚至在i18n/messages.properties中(如果需要)(例如:input1.validation =不超过$ {column1.length}个字母).

There is one big advantage of this way of defining columns. You could use this property's (e.g.: column1.length) value (e.g.: 10) for validation of every layer: Hibernate, DAO, WEB, faces, JavaScript. Just use this property at each place where you need to validate against it. Even in i18n/messages.properties if needed (e.g.: input1.validation=No more than ${column1.length} letters.).

唯一的麻烦是,如果您需要更改此值,则需要提供适当的liquibase更新/回滚脚本.有时可以更改值并设置新的liquibase校验和(安全操作,如增加varchar长度),但是有时您需要使用新的属性/值创建 safe 更新changescript.

The only complication is that if you need to change this value you need to provide proper liquibase update/rollback script. Sometimes it is possible to change value and set new liquibase checksum (safe operation like increase varchar length), but other times you need to create a safe update changescript using new property/value.

这篇关于如何从外部属性文件填充Liquibase参数值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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