Liquibase无法找到ChangeSet构造函数问题求助(Gradle多模块Spring Boot)
解决Liquibase执行
update时的ChangeSet构造器匹配错误 问题背景
以下是Gradle多模块Spring Boot项目的相关配置与环境信息:
build.gradle 配置
apply plugin: 'org.springframework.boot' apply plugin: 'org.liquibase.gradle' dependencies { liquibaseRuntime 'org.liquibase:liquibase-core:4.22.0' liquibaseRuntime 'org.liquibase:liquibase-groovy-dsl:3.0.0' liquibaseRuntime 'org.postgresql:postgresql:42.3.8' liquibaseRuntime 'info.picocli:picocli:4.7.3' liquibaseRuntime 'org.yaml:snakeyaml:2.0' liquibaseRuntime 'org.liquibase.ext:liquibase-hibernate5:4.22.0' ... } liquibase { activities { main { classpath 'src/main/resources' changeLogFile 'src/main/resources/db/changelog/db.changelog-master.yaml' url 'jdbc:postgresql://localhost:5432/dbname' username 'postgres' password 'postgres' driver 'org.postgresql.Driver' referenceUrl 'hibernate:spring:com.test.test?dialect=org.hibernate.dialect.PostgreSQL95Dialect&hibernate.physical_naming_strategy=org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy&hibernate.implicit_naming_strategy=org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy' } } }
plugins 配置
plugins { id 'java' id 'java-library' id 'application' id 'org.springframework.boot' version "$springBootVersion" id 'org.liquibase.gradle' version '2.2.0' }
变更集文件
db.changelog-master.yaml:
databaseChangeLog: - include: file: db/changelog/changes/first.groovy
first.groovy:
package db.changelog.changes databaseChangeLog() { changeSet(id: 'create-schema', author: 'testowner') { sql(stripComments: true, splitStatements: false, endDelimiter: ';') { "CREATE TABLE public.test_entity (" + "id bigserial NOT NULL);" } } }
文件结构
src main resources db changelog changes first.groovy db.changelog-master.yaml
环境信息
Java SDK:
java 17.0.2 2022-01-18 LTS Java(TM) SE Runtime Environment (build 17.0.2+8-LTS-86) Java HotSpot(TM) 64-Bit Server VM (build 17.0.2+8-LTS-86, mixed mode, sharing)
java-config.gradle:
apply plugin: 'java' sourceCompatibility = JavaVersion.VERSION_17 targetCompatibility = JavaVersion.VERSION_17
错误信息
执行liquibase update命令时出现如下错误:
Caused by: liquibase.exception.ChangeLogParseException: Error parsing src/main/resources/db/changelog/db.changelog-master.yaml : Could not find matching constructor for: liquibase.changelog.ChangeSet(java.lang.String, java.lang.String, java.lang.Boolean, java.lang.Boolean, java.lang.String, null, null, null, java.lang.Boolean, null, liquibase.changelog.DatabaseChangeLog)
解决方案
1. 对齐Liquibase Core与Groovy DSL版本
liquibase-core版本为4.22.0,但liquibase-groovy-dsl使用的3.0.0版本与该Core版本不兼容,导致Groovy DSL生成的ChangeSet调用参数与Core版本的构造器不匹配。
修改build.gradle中的依赖,将Groovy DSL版本调整为与Core一致:
dependencies { liquibaseRuntime 'org.liquibase:liquibase-core:4.22.0' // 改为匹配Core版本的Groovy DSL liquibaseRuntime 'org.liquibase:liquibase-groovy-dsl:4.22.0' liquibaseRuntime 'org.postgresql:postgresql:42.3.8' liquibaseRuntime 'info.picocli:picocli:4.7.3' liquibaseRuntime 'org.yaml:snakeyaml:2.0' liquibaseRuntime 'org.liquibase.ext:liquibase-hibernate5:4.22.0' ... }
2. 移除Groovy变更集中的不必要Package声明
Groovy变更集文件位于resources目录下,不属于Java源码包结构,无需添加package声明。修改first.groovy:
// 移除package声明 // package db.changelog.changes databaseChangeLog() { changeSet(id: 'create-schema', author: 'testowner') { sql(stripComments: true, splitStatements: false, endDelimiter: ';') { "CREATE TABLE public.test_entity (" + "id bigserial NOT NULL);" } } }
3. 验证Gradle Liquibase插件兼容性
当前使用的org.liquibase.gradle插件版本2.2.0兼容Liquibase 4.x版本,若上述修改后仍有问题,可尝试升级插件版本:
plugins { id 'java' id 'java-library' id 'application' id 'org.springframework.boot' version "$springBootVersion" // 升级到较新的插件版本 id 'org.liquibase.gradle' version '2.2.2' }
4. 清理缓存并重新执行
执行以下命令清理Gradle缓存并重新运行Liquibase update:
./gradlew clean ./gradlew liquibaseUpdate
内容的提问来源于stack exchange,提问作者Mert Doe
相关产品推荐
相关产品推荐

