通过 maven 和 spring 使用 liquibase 文件路径

2022-09-03 05:56:37

我使用以下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>

该文件包括子语言库更改日志文件。问题,如何从主节点引用它们。当我使用Spring时,我必须通过classpath使用以下路径:db.changelog-master.xml

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

使用 Maven 时,路径为:

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

我希望两种情况都具有相同的配置。如何存档?


答案 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 目录的路径,它将解决问题。

我通过使用相同的路径来改变Spring中的日志文件,maven和集成测试(称为Liquibase)来解决这个问题。我所有的更改日志文件都位于项目中一个 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 文件包含其他更改日志文件(更新.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>

答案 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


推荐