在 Gradle Spring Boot Hibernate 项目中设置 LiquiBase
Setup LiquiBase in a Gradle Spring Boot Hibernate project
我在 Spring 引导项目中设置 LiquiBase 时遇到了困难。我尝试查看文档并找到一些指南 - 但它们似乎相互矛盾:(
我希望通过 Gradle 使用 LiquiBase,我希望它从 Hibernate 生成变更日志并以一个 SQL 脚本结束,我可以 运行 在服务器上更新架构到适当的版本。
为了通过 Gradle 将其设置为 运行 我正在使用此插件 https://github.com/liquibase/liquibase-gradle-plugin 使用其 README 中显示的推荐设置。
为了使 Hibernate diff 正常工作,我正在使用 https://github.com/liquibase/liquibase-hibernate
这是我的 build.gradle 文件:
buildscript {
ext {
springBootVersion = '2.0.5.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
plugins {
id 'java'
id 'net.ltgt.apt' version '0.10' // https://projectlombok.org/setup/gradle
id 'org.liquibase.gradle' version '2.0.1' // https://github.com/liquibase/liquibase-gradle-plugin
}
apply plugin: 'eclipse-wtp'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'war'
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
maven {
credentials {
username = oracleUser
password = oraclePass
}
url 'https://www.oracle.com/content/secure/maven/content'
}
}
liquibase {
activities {
main {
changeLogFile 'main.groovy'
url 'jdbc:oracle:thin:@localhost:1521:XE'
referenceUrl 'hibernate:spring:com.example?dialect=org.hibernate.dialect.Oracle10gDialect'
username 'user'
password 'pass'
}
}
}
configurations {
providedRuntime
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-data-jpa')
compile('org.springframework.boot:spring-boot-starter-data-rest')
compile('org.springframework.boot:spring-boot-starter-hateoas')
compile('org.springframework.boot:spring-boot-starter-jooq')
compile('org.springframework.boot:spring-boot-starter-web')
compile('org.springframework.boot:spring-boot-starter-mail')
compile('com.github.waffle:waffle-spring-boot-starter:1.9.0')
compile('com.oracle.jdbc:ojdbc8:12.2.0.1')
runtime('org.springframework.boot:spring-boot-devtools')
compileOnly('org.projectlombok:lombok')
apt('org.projectlombok:lombok:1.18.2')
liquibaseRuntime('org.liquibase:liquibase-core:3.6.2')
liquibaseRuntime('org.liquibase:liquibase-groovy-dsl:2.0.1')
liquibaseRuntime('org.liquibase.ext:liquibase-hibernate5:3.6')
liquibaseRuntime('com.oracle.jdbc:ojdbc8:12.2.0.1') // duplicate...
providedRuntime('org.springframework.boot:spring-boot-starter-tomcat')
testCompile('org.springframework.boot:spring-boot-starter-test')
testCompile('org.springframework.restdocs:spring-restdocs-mockmvc')
}
运行 通过
> .\gradlew diffChangeLog -PrunList=main
但失败
Task :diffChangeLog liquibase-plugin: Running the 'main' activity... Starting Liquibase at Wed, 26 Sep 2018 13:36:24 CEST (version 3.6.2
built at 2018-07-03 11:28:09) Exception in thread "main"
java.lang.NoClassDefFoundError:
org/springframework/core/io/ClassPathResource
at liquibase.ext.hibernate.database.HibernateSpringPackageDatabase.isXmlFile(HibernateSpringPackageDatabase.java:54)
好像找不到Spring Boot。所以我然后尝试删除 liquibaseRuntime
但随后 LiquiBase Gradle 插件抱怨 liquibaseRuntime
丢失。
我好像陷入了一个循环。设置它的明智方法是什么?
我真的不想重复 liquibaseRuntime
中的每个依赖项。文档字面上也说:
dependencies {
// All of your normal project dependencies would be here in addition to...
liquibaseRuntime 'org.liquibase:liquibase-core:3.6.1'
liquibaseRuntime 'org.liquibase:liquibase-groovy-dsl:2.0.1'
liquibaseRuntime 'mysql:mysql-connector-java:5.1.34'
}
注意
// All of your normal project dependencies would be here in addition to...
是的。为什么...
请帮忙!
另外...我注意到您必须编写两次数据库配置。为什么在 spring 启动配置中已经设置了它?
进度
因此将 liquibaseRuntime
更改为
liquibaseRuntime('org.liquibase:liquibase-core:3.6.2')
liquibaseRuntime('org.liquibase:liquibase-groovy-dsl:2.0.1')
liquibaseRuntime('org.liquibase.ext:liquibase-hibernate5:3.6')
liquibaseRuntime('com.oracle.jdbc:ojdbc8:12.2.0.1')
liquibaseRuntime('org.springframework.boot:spring-boot-starter-data-jpa')
liquibaseRuntime files('src/main')
消除错误。但是还是不行。
运行这个命令
.\gradlew diff
给我这个输出
> Task :diff
liquibase-plugin: Running the 'main' activity...
Starting Liquibase at Wed, 26 Sep 2018 16:47:19 CEST (version 3.6.2 built at 2018-07-03 11:28:09)
Diff Results:
Reference Database: null @ hibernate:spring:com.example.model?dialect=org.hibernate.dialect.Oracle10gDialect (Default Schema: HIBERNATE)
Comparison Database: SYSTEM @ jdbc:oracle:thin:@localhost:1521:XE (Default Schema: SYSTEM)
Compared Schemas: HIBERNATE -> SYSTEM
Product Name:
Reference: 'Hibernate'
Target: 'Oracle'
Product Version:
Reference: '5.2.17.Final'
Target: 'Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production'
Missing Catalog(s):
HIBERNATE
Unexpected Catalog(s): NONE
Changed Catalog(s): NONE
Missing Column(s): NONE
Unexpected Column(s): NONE
Changed Column(s): NONE
Missing Foreign Key(s): NONE
Unexpected Foreign Key(s): NONE
Changed Foreign Key(s): NONE
Missing Index(s): NONE
Unexpected Index(s): NONE
Changed Index(s): NONE
Missing Primary Key(s): NONE
Unexpected Primary Key(s): NONE
Changed Primary Key(s): NONE
Missing Sequence(s): NONE
Unexpected Sequence(s): NONE
Changed Sequence(s): NONE
Missing Stored Procedure(s): NONE
Unexpected Stored Procedure(s): NONE
Changed Stored Procedure(s): NONE
Missing Table(s): NONE
Unexpected Table(s): NONE
Changed Table(s): NONE
Missing Unique Constraint(s): NONE
Unexpected Unique Constraint(s): NONE
Changed Unique Constraint(s): NONE
Missing View(s): NONE
Unexpected View(s): NONE
Changed View(s): NONE
Liquibase command 'diff' was executed successfully.
BUILD SUCCESSFUL in 7s
1 actionable task: 1 executed
当 运行针对空数据库时。所以是的 - 它不起作用 :(
原来我需要添加一些未记录的魔术酱。
diff.dependsOn compileJava
diffChangeLog.dependsOn compileJava
generateChangelog.dependsOn compileJava
dependencies {
// as before
liquibaseRuntime sourceSets.main.output // replaces liquibaseRuntime files('src/main')
}
我知道这是一个旧线程,但我想为后来找到此答案的人添加一些说明...
liquibaseRuntime
配置不继承任何其他配置。这是因为在大多数情况下,Liquibase 只需要能够解析更改日志并连接到数据库即可。要使用 Hibernate 模块之类的东西,或者从您的代码生成更改日志,您需要向 liquibaseRuntime 添加额外的东西,例如 Hibernate 或 Spring Data,并且您需要 srcSets.main.output
能够自己找到您的项目文件。
评论说 "All of your normal project dependencies would be here in addition to..." 指的是这样一个事实,即您将在块中有一堆其他依赖项来构建和 运行 您的项目,而不是它们将成为 liquibaseRuntime
配置。如果您确实希望项目中的所有库也成为 liquibaseRuntime
的一部分,您可以添加 configurations.liquibaseRuntime.extendsFrom configurations.runtime
您的 build.gradle,或者如果您已经有 configurations
块,您可以将 liquibaseRuntime.extendsFrom runtime
添加到该块。这应该将所有项目依赖项和项目文件本身添加到您的 liquibaseRuntime。
希望对您有所帮助。
这就是我使用 Gradle 6.3.0 的方式:
configurations {
liquibaseRuntime.extendsFrom runtimeClasspath
}
dependencies {
liquibaseRuntime sourceSets.main.output
}
我在 Spring 引导项目中设置 LiquiBase 时遇到了困难。我尝试查看文档并找到一些指南 - 但它们似乎相互矛盾:(
我希望通过 Gradle 使用 LiquiBase,我希望它从 Hibernate 生成变更日志并以一个 SQL 脚本结束,我可以 运行 在服务器上更新架构到适当的版本。
为了通过 Gradle 将其设置为 运行 我正在使用此插件 https://github.com/liquibase/liquibase-gradle-plugin 使用其 README 中显示的推荐设置。
为了使 Hibernate diff 正常工作,我正在使用 https://github.com/liquibase/liquibase-hibernate
这是我的 build.gradle 文件:
buildscript {
ext {
springBootVersion = '2.0.5.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
plugins {
id 'java'
id 'net.ltgt.apt' version '0.10' // https://projectlombok.org/setup/gradle
id 'org.liquibase.gradle' version '2.0.1' // https://github.com/liquibase/liquibase-gradle-plugin
}
apply plugin: 'eclipse-wtp'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'war'
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
maven {
credentials {
username = oracleUser
password = oraclePass
}
url 'https://www.oracle.com/content/secure/maven/content'
}
}
liquibase {
activities {
main {
changeLogFile 'main.groovy'
url 'jdbc:oracle:thin:@localhost:1521:XE'
referenceUrl 'hibernate:spring:com.example?dialect=org.hibernate.dialect.Oracle10gDialect'
username 'user'
password 'pass'
}
}
}
configurations {
providedRuntime
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-data-jpa')
compile('org.springframework.boot:spring-boot-starter-data-rest')
compile('org.springframework.boot:spring-boot-starter-hateoas')
compile('org.springframework.boot:spring-boot-starter-jooq')
compile('org.springframework.boot:spring-boot-starter-web')
compile('org.springframework.boot:spring-boot-starter-mail')
compile('com.github.waffle:waffle-spring-boot-starter:1.9.0')
compile('com.oracle.jdbc:ojdbc8:12.2.0.1')
runtime('org.springframework.boot:spring-boot-devtools')
compileOnly('org.projectlombok:lombok')
apt('org.projectlombok:lombok:1.18.2')
liquibaseRuntime('org.liquibase:liquibase-core:3.6.2')
liquibaseRuntime('org.liquibase:liquibase-groovy-dsl:2.0.1')
liquibaseRuntime('org.liquibase.ext:liquibase-hibernate5:3.6')
liquibaseRuntime('com.oracle.jdbc:ojdbc8:12.2.0.1') // duplicate...
providedRuntime('org.springframework.boot:spring-boot-starter-tomcat')
testCompile('org.springframework.boot:spring-boot-starter-test')
testCompile('org.springframework.restdocs:spring-restdocs-mockmvc')
}
运行 通过
> .\gradlew diffChangeLog -PrunList=main
但失败
Task :diffChangeLog liquibase-plugin: Running the 'main' activity... Starting Liquibase at Wed, 26 Sep 2018 13:36:24 CEST (version 3.6.2 built at 2018-07-03 11:28:09) Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/core/io/ClassPathResource at liquibase.ext.hibernate.database.HibernateSpringPackageDatabase.isXmlFile(HibernateSpringPackageDatabase.java:54)
好像找不到Spring Boot。所以我然后尝试删除 liquibaseRuntime
但随后 LiquiBase Gradle 插件抱怨 liquibaseRuntime
丢失。
我好像陷入了一个循环。设置它的明智方法是什么?
我真的不想重复 liquibaseRuntime
中的每个依赖项。文档字面上也说:
dependencies { // All of your normal project dependencies would be here in addition to... liquibaseRuntime 'org.liquibase:liquibase-core:3.6.1' liquibaseRuntime 'org.liquibase:liquibase-groovy-dsl:2.0.1' liquibaseRuntime 'mysql:mysql-connector-java:5.1.34' }
注意
// All of your normal project dependencies would be here in addition to...
是的。为什么...
请帮忙!
另外...我注意到您必须编写两次数据库配置。为什么在 spring 启动配置中已经设置了它?
进度
因此将 liquibaseRuntime
更改为
liquibaseRuntime('org.liquibase:liquibase-core:3.6.2')
liquibaseRuntime('org.liquibase:liquibase-groovy-dsl:2.0.1')
liquibaseRuntime('org.liquibase.ext:liquibase-hibernate5:3.6')
liquibaseRuntime('com.oracle.jdbc:ojdbc8:12.2.0.1')
liquibaseRuntime('org.springframework.boot:spring-boot-starter-data-jpa')
liquibaseRuntime files('src/main')
消除错误。但是还是不行。
运行这个命令
.\gradlew diff
给我这个输出
> Task :diff liquibase-plugin: Running the 'main' activity... Starting Liquibase at Wed, 26 Sep 2018 16:47:19 CEST (version 3.6.2 built at 2018-07-03 11:28:09) Diff Results: Reference Database: null @ hibernate:spring:com.example.model?dialect=org.hibernate.dialect.Oracle10gDialect (Default Schema: HIBERNATE) Comparison Database: SYSTEM @ jdbc:oracle:thin:@localhost:1521:XE (Default Schema: SYSTEM) Compared Schemas: HIBERNATE -> SYSTEM Product Name: Reference: 'Hibernate' Target: 'Oracle' Product Version: Reference: '5.2.17.Final' Target: 'Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production' Missing Catalog(s): HIBERNATE Unexpected Catalog(s): NONE Changed Catalog(s): NONE Missing Column(s): NONE Unexpected Column(s): NONE Changed Column(s): NONE Missing Foreign Key(s): NONE Unexpected Foreign Key(s): NONE Changed Foreign Key(s): NONE Missing Index(s): NONE Unexpected Index(s): NONE Changed Index(s): NONE Missing Primary Key(s): NONE Unexpected Primary Key(s): NONE Changed Primary Key(s): NONE Missing Sequence(s): NONE Unexpected Sequence(s): NONE Changed Sequence(s): NONE Missing Stored Procedure(s): NONE Unexpected Stored Procedure(s): NONE Changed Stored Procedure(s): NONE Missing Table(s): NONE Unexpected Table(s): NONE Changed Table(s): NONE Missing Unique Constraint(s): NONE Unexpected Unique Constraint(s): NONE Changed Unique Constraint(s): NONE Missing View(s): NONE Unexpected View(s): NONE Changed View(s): NONE Liquibase command 'diff' was executed successfully. BUILD SUCCESSFUL in 7s 1 actionable task: 1 executed
当 运行针对空数据库时。所以是的 - 它不起作用 :(
原来我需要添加一些未记录的魔术酱。
diff.dependsOn compileJava
diffChangeLog.dependsOn compileJava
generateChangelog.dependsOn compileJava
dependencies {
// as before
liquibaseRuntime sourceSets.main.output // replaces liquibaseRuntime files('src/main')
}
我知道这是一个旧线程,但我想为后来找到此答案的人添加一些说明...
liquibaseRuntime
配置不继承任何其他配置。这是因为在大多数情况下,Liquibase 只需要能够解析更改日志并连接到数据库即可。要使用 Hibernate 模块之类的东西,或者从您的代码生成更改日志,您需要向 liquibaseRuntime 添加额外的东西,例如 Hibernate 或 Spring Data,并且您需要 srcSets.main.output
能够自己找到您的项目文件。
评论说 "All of your normal project dependencies would be here in addition to..." 指的是这样一个事实,即您将在块中有一堆其他依赖项来构建和 运行 您的项目,而不是它们将成为 liquibaseRuntime
配置。如果您确实希望项目中的所有库也成为 liquibaseRuntime
的一部分,您可以添加 configurations.liquibaseRuntime.extendsFrom configurations.runtime
您的 build.gradle,或者如果您已经有 configurations
块,您可以将 liquibaseRuntime.extendsFrom runtime
添加到该块。这应该将所有项目依赖项和项目文件本身添加到您的 liquibaseRuntime。
希望对您有所帮助。
这就是我使用 Gradle 6.3.0 的方式:
configurations {
liquibaseRuntime.extendsFrom runtimeClasspath
}
dependencies {
liquibaseRuntime sourceSets.main.output
}