Liquibase变更集无法读取properties文件属性值问题求助
问题
我写了下面的Liquibase变更集代码,想通过liquibase.properties里的table.name=NEW_TABLE配置来指定要创建的表名,但实际执行后创建的表名却是${table.name},哪里错了?
变更集代码
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext" xmlns:pro="http://www.liquibase.org/xml/ns/pro" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd http://www.liquibase.org/xml/ns/pro http://www.liquibase.org/xml/ns/pro/liquibase-pro-latest.xsd http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-latest.xsd"> <changeSet id="1" author="fabio"> <createTable tableName="${table.name}"> <column name="NEW_2" type="int"> </column> </createTable> </changeSet> </databaseChangeLog>
liquibase.properties配置
table.name=NEW_TABLE
原因与解决方法
问题出在Liquibase默认不会自动解析XML变更集中的属性占位符,必须显式开启属性替换功能,否则${table.name}会被当作普通表名处理。
下面是几种可行的解决方式:
方式1:执行命令时开启属性替换
运行Liquibase命令时添加参数,启用默认属性解析器:liquibase update --changeLogPropertyProvider=liquibase.property.DefaultPropertyProvider或者用JVM参数传递:
liquibase update -Dliquibase.changeLogPropertyProvider=liquibase.property.DefaultPropertyProvider方式2:在配置文件中全局开启
在liquibase.properties里添加一行配置,永久开启属性替换:changeLogPropertyProvider=liquibase.property.DefaultPropertyProvider方式3:结合
<property>标签增强属性管理(可选)
如果需要更清晰的属性声明,可以在databaseChangeLog节点下添加<property>标签,显式引入配置文件中的属性:<databaseChangeLog ...> <!-- 显式引入配置文件中的table.name属性 --> <property name="table.name" value="${table.name}" /> <changeSet id="1" author="fabio"> <createTable tableName="${table.name}"> <column name="NEW_2" type="int"> </column> </createTable> </changeSet> </databaseChangeLog>注意这种方式依然需要配合方式1或方式2开启属性替换功能。
内容的提问来源于stack exchange,提问作者Fábio Pires
相关产品推荐
相关产品推荐

