Liquibase – 如何自定义执行的 expressionVars 属性

huangapple go评论95阅读模式
英文:

Liquibase - how to customize an expressionVars property for an execution

问题

我有一个 pom.xml 文件,其中使用了 liquibase 插件版本 4.7.1,并定义了两个 expressionVars,分别是 prop1prop2,用于替换另一个 changelog 文件中的占位符(如果有必要的话)。我定义了多个执行操作来创建不同的 SQL 脚本文件。如果我只在插件全局定义 expressionVars 并在那里设置属性值,一切都正常。

然而,我需要在其中一个执行操作中为 prop1 设置不同的值,而其他执行操作使用全局值。因此,我在 expressionVars 中仅为 prop1 属性值设置了新值,而我期望 prop2 仍然保留全局定义的值。但在这个执行操作中,实际发生的情况是,输出的 SQL 文件中替换了 prop1 的新值,但 prop2 的占位符不再被替换。

Pom 文件如下:

<plugin>
    <groupId>org.liquibase</groupId>
    <artifactId>liquibase-maven-plugin</artifactId>
    <version>4.7.1</version>
    <configuration>
        <outputFileEncoding>UTF-8</outputFileEncoding>
        <expressionVars>
            <property>
                <name>prop1</name>
                <value>some-common-value-1</value>
            </property>
            <property>
                <name>prop2</name>
                <value>some-common-value-2</value>
            </property>
        </expressionVars>
    </configuration>
    <executions>
        ...
        <execution>
            <id>one</id>
            <phase>generate-resources</phase>
            <goals>
                <goal>updateSQL</goal>
            </goals>
            <configuration>
                <changeLogFile>some-changelog.xml</changeLogFile>
                <url>...</url>
                <migrationSqlOutputFile>file.sql</migrationSqlOutputFile>
                <expressionVars>
                    <property>
                        <name>prop1</name>
                        <value>some-specific-value-1</value>
                    </property>
                </expressionVars>
            </configuration>
        </execution>
        ....
    </executions>
</plugin>

期望 prop1 被替换为 some-specific-value-1,而 prop2 被替换为 some-common-value-2。但 ${prop2} 的占位符未被替换。

我执行了 mvn help:effective-pom 命令,输出的 pom 包含以下部分:

<configuration>
    <changeLogFile>some-changelog.xml</changeLogFile>
    <url>...</url>
    <migrationSqlOutputFile>file.sql</migrationSqlOutputFile>
    <expressionVars>
        <property>
            <name>prop1</name>
            <value>some-specific-value-1</value>
        </property>
        <property>
            <name>prop2</name>
            <value>some-common-value-2</value>
        </property>
    </expressionVars>
</configuration>

这看起来与我的预期相符,因此我不明白为什么 ${prop2} 占位符没有被替换。

英文:

I have a pom.xml with a liquibase plugin v. 4.7.1 with two expressionVars, prop1 and prop2 that should replace the placeholders in a changelog (that is included in another changelog, if this matters). I have several executions defined to create different sql script files. All works fine if I only define the expressionVars globally for the plugin and set the property values there.
However, I need to have a different value for prop1 in one of the executions while the others use the global value. So I have an expressionVars with only that property value for prop1 and I expect prop2 would keep the globally defined value. What happens instead for this execution is I get the new value of prop1 in the output sql file but the prop2 placeholder is not replaced anymore.

The pom:

&lt;plugin&gt;
    &lt;groupId&gt;org.liquibase&lt;/groupId&gt;
    &lt;artifactId&gt;liquibase-maven-plugin&lt;/artifactId&gt;
    &lt;version&gt;4.7.1&lt;/version&gt;
    &lt;configuration&gt;
        &lt;outputFileEncoding&gt;UTF-8&lt;/outputFileEncoding&gt;
        &lt;expressionVars&gt;
            &lt;property&gt;
                &lt;name&gt;prop1&lt;/name&gt;
                &lt;value&gt;some-common-value-1&lt;/value&gt;
            &lt;/property&gt;
            &lt;property&gt;
                &lt;name&gt;prop2&lt;/name&gt;
                &lt;value&gt;some-common-value-2&lt;/value&gt;
            &lt;/property&gt;
        &lt;/expressionVars&gt;
    &lt;/configuration&gt;
    &lt;executions&gt;
        ...
        &lt;execution&gt;
            &lt;id&gt;one&lt;/id&gt;
            &lt;phase&gt;generate-resources&lt;/phase&gt;
            &lt;goals&gt;
                &lt;goal&gt;updateSQL&lt;/goal&gt;
            &lt;/goals&gt;
            &lt;configuration&gt;
                &lt;changeLogFile&gt;some-changelog.xml&lt;/changeLogFile&gt;
                &lt;url&gt;...&lt;/url&gt;
                &lt;migrationSqlOutputFile&gt;file.sql&lt;/migrationSqlOutputFile&gt;
                &lt;expressionVars&gt;
                    &lt;property&gt;
                        &lt;name&gt;prop1&lt;/name&gt;
                        &lt;value&gt;some-specific-value-1&lt;/value&gt;
                    &lt;/property&gt;
                &lt;/expressionVars&gt;
            &lt;/configuration&gt;
        &lt;/execution&gt;
        ....

Expected prop1 to be replaced by some-specific-value-1 and prop2 to be replaced by some-common-value-2. But the ${prop2} placeholder is not being replaced.

I executed mvn help:effective-pom and the output pom contains:

&lt;configuration&gt;
                &lt;changeLogFile&gt;some-changelog.xml&lt;/changeLogFile&gt;
                &lt;url&gt;...&lt;/url&gt;
                &lt;migrationSqlOutputFile&gt;file.sql&lt;/migrationSqlOutputFile&gt;
                &lt;expressionVars&gt;
                    &lt;property&gt;
                        &lt;name&gt;prop1&lt;/name&gt;
                        &lt;value&gt;some-specific-value-1&lt;/value&gt;
                    &lt;/property&gt;
                    &lt;property&gt;
                        &lt;name&gt;prop2&lt;/name&gt;
                        &lt;value&gt;some-common-value-2&lt;/value&gt;
                    &lt;/property&gt;
                &lt;/expressionVars&gt;
            &lt;/configuration&gt;
        &lt;/execution&gt;

That looks how I was expecting so I don't understand why is the ${prop2} placeholder not replaced?

答案1

得分: 0

首先,值得阅读Maven How-To: Merging Plugin Configuration in Complex Projects文章,描述了Maven为合并配置提供的选项。

其次,在阅读上述提到的文章之后,就变得清楚了如何合并像你的这种“基于属性”的配置并不清晰:

<expressionVars>
    <property>
        <name>prop1</name>
        <value>some-common-value-1</value>
    </property>
    <property>
        <name>prop2</name>
        <value>some-common-value-2</value>
    </property>
</expressionVars>  

仅仅因为所有元素都具有相同的名称(property/name/value),幸运的是,看起来Liquibase开发人员已经预见到了这个挑战(或类似的挑战),并提供了使用“基于映射”的配置的能力:expressionVariables,因此,在你的情况下,以下内容应该有效:

<configuration>
    <expressionVariables>
        <prop1>some-common-value-1</prop1>
        <prop2>some-common-value-2</prop2>
    </expressionVariables>
    <skip>true</skip>
</configuration>
<executions>
    <execution>
        <id>one</id>
        <configuration>
            <expressionVariables combine.self="append">
                <prop1 combine.self="override">some-special-value-1</prop1>
            </expressionVariables>
        </configuration>
    </execution>
</executions>
英文:

First of all, it is worth to read Maven How-To: Merging Plugin Configuration in Complex Projects article, describing options provided by maven for merging configuration.

At second, after reading the article, mentioned above, it becomes clear that it is not clear how to merge "property-based" configurations like yours:

&lt;expressionVars&gt;
    &lt;property&gt;
        &lt;name&gt;prop1&lt;/name&gt;
        &lt;value&gt;some-common-value-1&lt;/value&gt;
    &lt;/property&gt;
    &lt;property&gt;
        &lt;name&gt;prop2&lt;/name&gt;
        &lt;value&gt;some-common-value-2&lt;/value&gt;
    &lt;/property&gt;
&lt;/expressionVars&gt;  

just because all elements have the same name (property/name/value), fortunately, it seems that liquibase developers anticipated that challenge (or something similar) and have provided the ability to use "map-based" configurations: expressionVariables, so, in your case the following should work:

&lt;configuration&gt;
    &lt;expressionVariables&gt;
        &lt;prop1&gt;some-common-value-1&lt;/prop1&gt;
        &lt;prop2&gt;some-common-value-2&lt;/prop2&gt;
    &lt;/expressionVariables&gt;
    &lt;skip&gt;true&lt;/skip&gt;
&lt;/configuration&gt;
&lt;executions&gt;
    &lt;execution&gt;
        &lt;id&gt;one&lt;/id&gt;
        &lt;configuration&gt;
            &lt;expressionVariables combine.self=&quot;append&quot;&gt;
                &lt;prop1 combine.self=&quot;override&quot;&gt;some-special-value-1&lt;/prop1&gt;
            &lt;/expressionVariables&gt;
    &lt;/execution&gt;
&lt;/executions&gt;

huangapple
  • 本文由 发表于 2023年7月13日 21:23:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76679859.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定