LiquiBase 问题,类路径资源 [db/changelog/db.changelog-master.yaml] 无法解析为 URL,因为它不存在
Posted
技术标签:
【中文标题】LiquiBase 问题,类路径资源 [db/changelog/db.changelog-master.yaml] 无法解析为 URL,因为它不存在【英文标题】:LiquiBase problem , class path resource [db/changelog/db.changelog-master.yaml] cannot be resolved to URL because it does not exist 【发布时间】:2020-06-26 05:10:23 【问题描述】:我在将 liquibase 与 springboot 集成时遇到问题。 我在 pom 文件中添加了 liquibase 依赖项,如下所示:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-core</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
在 src/main/resources 中,我创建了文件夹 db/changelog。 在 db 文件夹中,它位于 liquibase-change.xml 文件,其内容如下:
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd">
<include file="changelog/01-create-employee-scheme.xml" relativeToChangelogFile="true"/>
<include file="changelog/02-data-insert-employees.xml" relativeToChangelogFile="true"/>
</databaseChangeLog>
在文件夹更改日志中的文件:
01-create-employee-scheme.xml<changeSet id="01" author="xy">
<createTable tableName="employee">
<column name="id" type="int">
<constraints nullable="false" primaryKey="true"/>
</column>
<column name="firstname" type="varchar(25)">
<constraints nullable="false"/>
</column>
<column name="lastname" type="varchar(25)">
<constraints nullable="false"/>
</column>
</createTable>
</changeSet>
02-data-insert-employees.xml
<insert tableName="employee">
<column name="id" valueNumeric="1"/>
<column name="firstname" value="x"/>
<column name="lastname" value="y"/>
</insert>
在 Spring Boot 的应用程序属性文件中,我进行了以下配置。 应用程序.properties
#Liquibase
liquibase.change-log=classpath:db/liquibase-changelog.xml
spring.liquibase.enabled=true
#H2 DB
spring.jpa.hibernate.ddl-auto=none
spring.h2.console.enabled=true
spring.datasource.url=jdbc:h2:mem:employeedb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=password
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
当我启动 spring boot 应用程序时抛出的错误如下:
2020-03-14 13:22:54.557 ERROR 5804 --- [ main] o.s.boot.SpringApplication : Application run failed
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.ChangeLogParseException: Error parsing classpath:/db/changelog/db.changelog-master.yaml
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1796) ~[spring-beans-5.2.4.RELEASE.jar:5.2.4.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:595) ~[spring-beans-5.2.4.RELEASE.jar:5.2.4.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517) ~[spring-beans-5.2.4.RELEASE.jar:5.2.4.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323) ~[spring-beans-5.2.4.RELEASE.jar:5.2.4.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.2.4.RELEASE.jar:5.2.4.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321) ~[spring-beans-5.2.4.RELEASE.jar:5.2.4.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.4.RELEASE.jar:5.2.4.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:310) ~[spring-beans-5.2.4.RELEASE.jar:5.2.4.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.4.RELEASE.jar:5.2.4.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1108) ~[spring-context-5.2.4.RELEASE.jar:5.2.4.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:868) ~[spring-context-5.2.4.RELEASE.jar:5.2.4.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550) ~[spring-context-5.2.4.RELEASE.jar:5.2.4.RELEASE]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141) ~[spring-boot-2.2.5.RELEASE.jar:2.2.5.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747) ~[spring-boot-2.2.5.RELEASE.jar:2.2.5.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) ~[spring-boot-2.2.5.RELEASE.jar:2.2.5.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) ~[spring-boot-2.2.5.RELEASE.jar:2.2.5.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) ~[spring-boot-2.2.5.RELEASE.jar:2.2.5.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215) ~[spring-boot-2.2.5.RELEASE.jar:2.2.5.RELEASE]
at com.github.rshtishi.payroll.employee.EmployeeApplication.main(EmployeeApplication.java:13) ~[classes/:na]
Caused by: liquibase.exception.ChangeLogParseException: Error parsing classpath:/db/changelog/db.changelog-master.yaml
at liquibase.parser.core.yaml.YamlChangeLogParser.parse(YamlChangeLogParser.java:83) ~[liquibase-core-3.8.7.jar:na]
at liquibase.Liquibase.getDatabaseChangeLog(Liquibase.java:217) ~[liquibase-core-3.8.7.jar:na]
at liquibase.Liquibase.update(Liquibase.java:190) ~[liquibase-core-3.8.7.jar:na]
at liquibase.Liquibase.update(Liquibase.java:179) ~[liquibase-core-3.8.7.jar:na]
at liquibase.integration.spring.SpringLiquibase.performUpdate(SpringLiquibase.java:366) ~[liquibase-core-3.8.7.jar:na]
at liquibase.integration.spring.SpringLiquibase.afterPropertiesSet(SpringLiquibase.java:314) ~[liquibase-core-3.8.7.jar:na]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1855) ~[spring-beans-5.2.4.RELEASE.jar:5.2.4.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1792) ~[spring-beans-5.2.4.RELEASE.jar:5.2.4.RELEASE]
... 18 common frames omitted
Caused by: java.io.FileNotFoundException: class path resource [db/changelog/db.changelog-master.yaml] cannot be resolved to URL because it does not exist
at org.springframework.core.io.ClassPathResource.getURL(ClassPathResource.java:195) ~[spring-core-5.2.4.RELEASE.jar:5.2.4.RELEASE]
at liquibase.integration.spring.SpringLiquibase$SpringResourceOpener.getResourcesAsStream(SpringLiquibase.java:613) ~[liquibase-core-3.8.7.jar:na]
at liquibase.util.StreamUtil.singleInputStream(StreamUtil.java:186) ~[liquibase-core-3.8.7.jar:na]
at liquibase.parser.core.yaml.YamlChangeLogParser.parse(YamlChangeLogParser.java:26) ~[liquibase-core-3.8.7.jar:na]
... 25 common frames omitted
我不明白为什么当我在 application.properties 文件中指定属性时它正在搜索文件 [db/changelog/db.changelog-master.yaml]:liquibase.change-log=classpath:db/liquibase-changelog.xml
。
问候。
【问题讨论】:
我认为应该是spring.liquibase.change-log=...".
liquibase.change-log`(不带spring
前缀)已被弃用。
Ervin Szilagyi 是对的,而且您的变更日志路径也错误。正如您所写,您将更改日志放入db/changelog
文件夹,那么它应该是spring.liquibase.change-log=classpat:db/changelog/liquibase-change.xml
。顺便说一句 db/changelog/db.changelog-master.yam
是默认弹簧值。
谢谢你,但我修正了其他错误。
添加 spring
前缀有效!
【参考方案1】:
尝试使用spring.liquibase.change-log
属性(而不仅仅是liquibase.change-log
)。当我遇到同样的问题时,这对我有用。
Jira 跟踪器的问题 CORE-3459 中也提到了此解决方案。
【讨论】:
liquibase.change-log 已弃用。它必须是 spring.liquibase.change-log【参考方案2】:我也遇到了同样的问题。用 @SpringBootTest 替换了 Test 类中的 @ContextConfiguration 并且它起作用了。不知道为什么会起作用。
【讨论】:
【参考方案3】:Spring LiquibaseProperties
具有 liquibase 的配置
@ConfigurationProperties(prefix = "spring.liquibase", ignoreUnknownFields = false)
所以,在 property/yml 文件中应该有 liquibase 的 sprig 前缀
spring:
liquibase:
enabled: true
drop-first: false
change-log: classpath:db/liquibase-changelog.xml
default-schema: public
确保你有正确的缩进
【讨论】:
以上是关于LiquiBase 问题,类路径资源 [db/changelog/db.changelog-master.yaml] 无法解析为 URL,因为它不存在的主要内容,如果未能解决你的问题,请参考以下文章
在 Liquibase 4.0 中删除了通过绝对路径指定文件
如果运行 spring boot 并让 liquibase 自动执行迁移,则 Liquibase databasechangelog 表存储相对路径
无法初始化类 liquibase.sqlgenerator.core.LockDatabaseChangeLogGenerator