java - 如何从外部属性文件填充 Liquibase 参数值?

标签 java maven liquibase

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

例如,我希望能够说:

<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 的工作 filters和/或 profiles

注意:小心使用 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>

编辑:A typical调用(使用 filtered 资源)将如下所示: 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=No more than ${column1.length} letters.)。

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

关于java - 如何从外部属性文件填充 Liquibase 参数值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20610948/

相关文章:

java - 在 Android 所有 Activity 上设置标签栏底部

JAVA-主方法中的逻辑错误

java - 用Java设计高性能状态机

apache - 如何解决缺少的需求[308.0] osgi.wiring.package; (osgi.wiring.package=org.apache.cxf.binding.corba) 在 FUSE 6.3.0 中

java - cassandra datastax 写入生成的代码时出现意外错误 : java. lang.NullPointerException

java - Jython 打印所有终端输出/将输出分配为字符串

maven - 如果 child 的 pom 版本与 parent 的聚合器 pom 及其子模块的版本不同,请避免错误的版本插值

spring-boot - 使用 H2 数据库和 Liquibase 配置 Spring Boot

mysql - 这个xslt有什么问题吗?

database - Liquibase 命令行在 sql 中创建差异变更日志