Liquibase 不从 changeSet 内的类路径加载 *.sql

Liquibase doesn't load *.sql from classpath inside of changeSet

我需要配置 changeSet 执行 sql 从 jar 加载。

我有内部项目 changeSet 可以正常工作

<changeSet id="1" author="sergii" dbms="h2">
  <sqlFile
    encoding="utf8"
    path="schema-ms-sql.0.0.1.sql"
    relativeToChangelogFile="true"
    splitStatements="true"
    stripComments="true"/>
</changeSet>

一些脚本是从不同的库中提供的(在我的例子中是spring-boot-starter-batch),例如:

classpath:/org/springframework/batch/core/schema-h2.sql

请注意,jar 在项目中并且可以访问(生成\测试\运行 次)。 因此,我也需要在我的 changeSet 中注册一个,尝试:

<changeSet id="2" author="sergii" dbms="h2">
  <sqlFile
    encoding="utf8"
    path="classpath*:/org/springframework/batch/core/schema-h2.sql"
    relativeToChangelogFile="true"
    splitStatements="true"
    stripComments="true"/>
</changeSet>

并且它不适用于任何配置(如 "classpath:/org/springframework/batch/core/schema-h2.sql""/org/springframework/batch/core/schema-h2.sql""org/springframework/batch/core/schema-h2.sql""classpath*:/org/springframework/batch/core/schema-h2.sql" 等等),因为

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'liquibase' defined in class path resource [org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfiguration$LiquibaseConfiguration.class]: Invocation of init method failed; nested exception is liquibase.exception.UnexpectedLiquibaseException: java.io.IOException: File does not exist: 'classpath*:/org/springframework/batch/core/schema-h2.sql'

我知道使用 spring 我可以使用自动配置,但我对 liquibase 审计感兴趣...

有什么想法可以使打包的脚本通过 changeSet 工作或包含在 liquibase 审计中吗?

解决方案是更改 sqlFile 标签的属性:

relativeToChangelogFile="false"

结果changeSet如下:

<changeSet id="2" author="sergii" dbms="h2">
  <sqlFile
    encoding="utf8"
    path="classpath:/org/springframework/batch/core/schema-h2.sql"
    relativeToChangelogFile="false"
    splitStatements="true"
    stripComments="true"/>
</changeSet>

具有相对路径的单独 changelog.yaml 文件示例:

databaseChangeLog:
  - changeSet:
      id: my_script_1
      changes:
          - sqlFile:
                dbms: mysql
                encoding: utf8
                path: db/changelog/0_0_1/my_script_1.sql    
                relativeToChangelogFile: false
                splitStatements: true
                stripComments: true
      rollback:
          - sqlFile:
                dbms: mysql
                encoding: utf8
                path: db/changelog/0_0_1/my_script_1_rollback.sql
                relativeToChangelogFile: false
                splitStatements: true
                stripComments: true
  - changeSet:
      id: my_script_2
      changes:
        - sqlFile:
            dbms: mysql
            encoding: utf8
            path: db/changelog/0_0_1/my_script_2.sql
            relativeToChangelogFile: false
            splitStatements: true
            stripComments: true
      rollback:
        - sqlFile:
            dbms: mysql
            encoding: utf8
            path: db/changelog/0_0_1/my_script_2_rollback.sql
            relativeToChangelogFile: false
            splitStatements: true
            stripComments: true