【问题标题】:Using liquibase file paths via both maven and spring通过 maven 和 spring 使用 liquibase 文件路径
【发布时间】:2013-05-12 09:20:13
【问题描述】:

我在 spring 上下文中使用以下 beean 更新方案和初始数据:

<bean id="liquibase" class="liquibase.integration.spring.SpringLiquibase">
    <property name="dataSource" ref="dataSource" />
    <property name="changeLog" value="classpath:db/changelog/db.changelog-master.xml" />
    <property name="dropFirst" value="true" />
</bean>

我还使用 Maven liquibase 插件生成 sql 脚本,以查看创建了哪些表等。

 <plugin>
                <groupId>org.liquibase</groupId>
                <artifactId>liquibase-maven-plugin</artifactId>
                <version>2.0.5</version>
                <configuration>
                    <!--mvn initialize liquibase:updateSQL-->
                    <propertyFile>src/main/resources/db/config/liquibase-gensql-data-access.properties</propertyFile>
                    <changeLogFile>src/main/resources/db/changelog/db.changelog-master.xml</changeLogFile>

                </configuration>
           </plugin>

db.changelog-master.xml 文件包括子 liquibase 更改日志文件。问题,如何从大师那里引用它们。当我使用 Spring 时,我必须通过类路径使用以下路径:

<include file="classpath:/db/changelog/db.changelog-1.0.xml"/>

使用Maven时,路径为:

<include file="src/main/resources/db/changelog/db.changelog-1.0.xml"/>

我希望这两种情况都使用相同的配置。如何存档?

【问题讨论】:

    标签: java spring maven liquibase


    【解决方案1】:

    我想如果你改变你的 Maven 路径

    <changeLogFile>src/main/resources/db/changelog/db.changelog-master.xml</changeLogFile>
    

    到

    <changeLogFile>db/changelog/db.changelog-master.xml</changeLogFile>
    

    并更新所有包含文件的 db.changelog-master.xml 文件以使用相对于 src/main/resources 目录的路径,它将解决问题。

    我通过在调用 Liquibase 的 Spring、maven 和集成测试中使用相同的路径来更改日志文件解决了这个问题。我所有的变更日志文件都位于项目中某个 Maven 模块的 /src/main/resources/db 目录下。

    运行 Liquibase 的 Maven 配置文件,注意路径:db/masterChangeLog.xml

    <plugin>
                        <groupId>org.liquibase</groupId>
                        <artifactId>liquibase-maven-plugin</artifactId>
                        <version>3.0.2</version>
    
                        <executions>
                            <execution>
                                <id>*** Install a last major release version of db ***</id>
                                <phase>process-resources</phase>
                                <goals>
                                    <goal>update</goal>
                                </goals>
                                <configuration>
                                    <changeLogFile>db/masterChangeLog.xml</changeLogFile>
                                    <contexts>dbBuildContext, dmlDevContext</contexts>
                                    <propertyFile>db/liquibase-${user.name}.properties</propertyFile>
                                    <promptOnNonLocalDatabase>false</promptOnNonLocalDatabase>
                                    <logging>debug</logging>
                                </configuration>
                            </execution>
    

    db/masterChangeLog.xml 文件包括以下文件:

    <include file="db/install.xml"/>
    <include file="db/update.xml"/>
    

    db/install.xml 文件包括其他变更日志文件(update.xml 也是如此):

    <includeAll path="db/install/seq"/>
    <includeAll path="db/install/tab"/>
    <includeAll path="db/install/cst"/>
    <includeAll path="db/latest/vw"  />
    

    Spring 上下文在应用启动时执行相同的一组 db 脚本,如下所示:

    <bean id="liquibase" class="liquibase.integration.spring.SpringLiquibase">
        <property name="dataSource" ref="baseCostManagementDataSource" />
        <property name="changeLog" value="classpath:db/masterChangelog.xml" />
        <property name="contexts" value="dbBuildContext, dmlDevContext" />
    </bean>
    

    【讨论】:

    • 我用 Liquibase 3.0.5 测试了一个类似的配置,但这不起作用。如果我使用 SpringLiquibase 运行迁移,我的所有变更集 FILENAME 列值都将以“classpath:”为前缀,而通过 Maven 运行迁移时不会添加这样的前缀。因此,使用两种方式时,迁移会发生冲突。
    • 我对 有点困惑,这不是 吗?请澄清。
    • 谢谢你。完美的!!!!!我唯一的区别是:
    • 我正在使用 Liquibase 3.5.3 及其 Maven 插件,版本相同,并且在 Spring 中使用相同的路径,我的 pom.xml 解决了问题!
    • @Rolf 这个答案现在有效,因为this PR (我认为这是你的:-))。它基本上在文件名比较之前去除 classpath: 前缀,因此 FILENAME 列值以 classpath: 为前缀这一事实并不能阻止它工作。
    【解决方案2】:

    我评论了伊戈尔的回答,他的解决方案似乎不起作用。

    为了解决这个问题,我刚刚向 Liquibase 推送了一个补丁:https://github.com/liquibase/liquibase/pull/187。这应该在 3.0.6-SNAPSHOT 中合并,因此很快在 3.0.6 中可用。

    通过此更改,您现在可以使用以下附加行配置 SpringLiquibase:

    <property name="ignoringClasspathPrefix" value="true" />
    

    可以在此处找到需要此更改的另一个示例/用例:https://github.com/LateralThoughts/spring-liquibase-extensions。

    【讨论】:

    • 只是一个更新:从 liquibase-3.1 开始,该属性现在称为 ignoreClasspathPrefix,并且默认为 true。
    • 使它对我有用的小细节:在 Maven 中,不要指定 src/main/resources 部分,否则它将与 Spring 不兼容。我在 Maven 中配置了db/changelog/...,在 Spring (application.properties) 中配置了classpath:db/changelog/...,然后它就可以工作了。
    【解决方案3】:

    Maven 插件在最近的版本中具有配置属性 changeLogDirectory。

    因此设置&lt;changeLogDirectory&gt;src/main/resources&lt;/changeLogDirectory&gt; 应该可以实现您想要的。

    【讨论】:

      【解决方案4】:

      在每个 databaseChangeLog 文件中指定logicalFilePath 属性。见http://www.liquibase.org/documentation/databasechangelog.html

      【讨论】:

        猜你喜欢
        • 2017-09-01
        • 1970-01-01
        • 2020-01-28
        • 1970-01-01
        • 2015-09-27
        • 2021-10-22
        • 2017-10-14
        • 2021-04-02
        • 1970-01-01
        相关资源
        最近更新 更多