java - 为什么liquibase无法解析db.changelog类路径?

标签 java maven spring-mvc spring-boot liquibase

这是Maven依赖项jar项目之一的结构,其中一个包含类路径中的 liquibase 更改日志,如下所示:

chorke─init─change-1.0.00.GA.jar!
 └─ META-INF/
    └─ migrations/
        ├─ db.changelog-master.xml
        ├─ config/
        │   ├─ db.changelog-config.xml
        │   ├─ db.changelog-property.xml
        │   └─ db.changelog-restrict.xml
        └─ change/
            ├─ db.changelog-1.xml
            ├─ db.changelog-2.xml
            ├─ V1/
            │   ├─ db.changelog-1.0.xml
            │   ├─ db.changelog-1.1.xml
            │   ├─ V1.0/
            │   │   ├─ db.changelog-1.0.00.xml
            │   │   ├─ db.changelog-1.0.01.xml
            │   │   ├─ V1.0.00/
            │   │   │   ├─ db.changelog-1.0.00.000.xml
            │   │   │   ├─ db.changelog-1.0.00.001.xml
            │   │   │   ├─ db.changelog-1.0.00.002.xml
            │   │   │   └─ db.changelog-1.0.00.999.xml
            │   │   └─ V1.0.01/
            │   │       ├─ db.changelog-1.0.01.000.xml
            │   │       ├─ db.changelog-1.0.01.001.xml
            │   │       ├─ db.changelog-1.0.01.002.xml
            │   │       └─ db.changelog-1.0.01.999.xml
            │   └─ V1.1/
            │       ├─ db.changelog-1.1.00.xml
            │       ├─ db.changelog-1.1.01.xml
            │       ├─ V1.1.00/db.changelog-1.1.00.###.xml
            │       └─ V1.1.01/db.changelog-1.1.01.###.xml
            └─ V2/
                ├─ db.changelog-2.#.xml
                └─ V2.#/V2.#.##/db.changelog-2.#.##.###.xml

这是 db.changelog-master.xml
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
    xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.5.xsd
    http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd">

    <includeAll path="config" relativeToChangelogFile="true"/>
    <include file="change/db.changelog-1.xml" relativeToChangelogFile="true"/>
    <include file="change/db.changelog-2.xml" relativeToChangelogFile="true"/>
</databaseChangeLog>

哪一个是由 spring-boot application.properties加载的,如下所示
liquibase.change-log=classpath:/META-INF/migrations/db.changelog-master.xml

在同一项目中时执行良好。在依赖项目上,它执行如下:
  • 数据库创建的
  • 创建的DATABASECHANGELOGLOCK
  • 但未执行更新迁移!

  • liquibase maven插件加载 db.changelog-master.xml 时,如下所示:
    <?xml version="1.0" encoding="UTF-8"?>
    <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/maven-v4_0_0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <!-- intentionally configuration skipped -->
        <dependencies>
            <dependency>
                <groupId>org.chorke.init</groupId>
                <artifactId>chorke-init-change</artifactId>
                <version>1.0.00.GA</version>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.liquibase</groupId>
                    <artifactId>liquibase-maven-plugin</artifactId>
                    <version>3.5.3</version>
                    <configuration>
                        <propertyFileWillOverride>true</propertyFileWillOverride>
                        <promptOnNonLocalDatabase>false</promptOnNonLocalDatabase>
                        <changeLogFile>classpath:/META-INF/migrations/db.changelog-master.xml</changeLogFile>
                        <propertyFile>${project.build.directory}/test-classes/liquibase-update.properties</propertyFile>
                    </configuration>
                    <executions>
                        <execution>
                            <phase>package</phase>
                            <goals>
                                <goal>update</goal>
                            </goals>
                        </execution>
                    </executions>
                    <dependencies>
                        <dependency>
                            <groupId>org.yaml</groupId>
                            <artifactId>snakeyaml</artifactId>
                            <version>1.14</version>
                        </dependency>
                    </dependencies>
                </plugin>
            </plugins>
        </build>
    </project>
    

    消息错误:
    Failed to execute goal
    org.liquibase:liquibase-maven-plugin:3.5.3:update (default) on project
    chorke-init-migrat: Error setting up or running Liquibase:
    classpath:/META-INF/migrations/db.changelog-master.xml does not exist
    

    在这种情况下,您的指南要求为使用 spring-boot liquibase-maven-plugin 的从属项目的无错误 liquibase迁移

    最佳答案

    针对chorke─init─change-1.0.00.GA.jar提到的结构包含类路径中的 liquibase更改日志足够好,并且 spring-boot application.properties也已正确配置。但是 liquibase-maven-plugin 配置中存在一些愚蠢的错误,应将其更正为:

    <configuration>
        <propertyFileWillOverride>true</propertyFileWillOverride>
        <promptOnNonLocalDatabase>false</promptOnNonLocalDatabase>
        <changeLogFile>META-INF/migrations/db.changelog-master.xml</changeLogFile>
        <propertyFile>liquibase-update.properties</propertyFile>
    </configuration>
    

    liquibase-maven-plugin 配置中有两个错误,它们是:
  • changeLogFile的第一个
  • propertyFile的第二个

  • 无需为classpath:使用诸如classpath:/changeLogFile之类的前缀,也无需为${project.build.directory}/test-classes/使用诸如propertyFile之类的绝对或相对路径。保持简单。 liquibase-maven-plugin 是自己的公司,如何从classpath解析它。

    除此之外,还有一些其他技巧可能对无忧的便携式移植有所帮助,如下所示
  • 始终对每个databaseChangeLog使用相对路径。 db.changelog-master.xml中提到的示例
  • 为每个changeSet使用逻辑文件路径

  • 这是逻辑文件路径的示例:
    <changeSet author="chorkeorg" id="1508234245316-1" logicalFilePath="V0/V0.0/V0.0.00/db.changelog-0.0.00.000.xml">
        <createSequence cacheSize="20" cycle="false"
            incrementBy="1" maxValue="999999999999999999999999999" minValue="10001"
            ordered="false" sequenceName="CK_SQ_AUTHOR" startValue="10181" />
    </changeSet>
    

    希望它能正确解决您的问题。

    关于java - 为什么liquibase无法解析db.changelog类路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46810712/

    相关文章:

    java - Primefaces SelectCheckboxMenu 空值

    java - Android Activity onCreate 方法调用的同步方法

    spring - Maven webResources 即时过滤

    UserDaoImpl 处的 java.lang.ExceptionInInitializerError。<clinit>(UserDaoImpl.java :71)

    Spring Security 2.0 Facebook 连接

    java - 我对如何在我的代码中实现单例模式并让它调用我的对象类和方法感到困惑

    java - Struts 2 Junit 测试用例执行失败(StrutsTestCase.getActionProxy :138 » NullPointer)

    java - 如何在 Jenkins 中为每个从属节点配置 Maven 安装?

    java - service层和controller在实践中的区别

    java - 如何设置图像按钮的文本(Android)?