Spring Boot 在Gradle Sping Boot Hibernate项目中安装LiquiBase

bf1o4zei  于 2023-04-30  发布在  Spring
关注(0)|答案(3)|浏览(198)

我在Sping Boot 项目中很难设置LiquiBase。我试着翻阅文档,找到一些指南-但它们似乎相互矛盾。
我希望通过Gradle使用LiquiBase,我希望它从Hibernate生成更新日志,并最终生成一个SQL脚本,我可以在服务器上运行该脚本,以将模式更新到适当的版本。
为了让它通过Gradle运行,我使用这个插件https://github.com/liquibase/liquibase-gradle-plugin,使用他们的README中所示的推荐设置。
为了让Hibernate diff工作,我使用了https://github.com/liquibase/liquibase-hibernate
这是我的版本。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

但失败了
任务:diffChangeLog liquibase-plugin:运行“主”活动。..启动Liquibase at Wed,26 Sep 2018 13:36:24 CEST(version 3.6.2 built at 2018-07-03 11:28:09)线程“main”java中出现异常。lang.NoClassDefFoundError:org/springframework/core/io/ClassPathResource。ext.hibernate.database.HibernateSpringPackageDatabase.isXmlFile(Hibernate SpringPackageDatabase.java:54)
看起来好像找不到Sping Boot 。然后我尝试删除liquibaseRuntime,但LiquiBase Gradle插件抱怨liquibaseRuntime丢失。
我好像陷入了一个循环。怎样才是合理的安排呢?我真的不想重复liquibaseRuntime中的每个依赖项。doc还说:

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'
}

注意
//所有正常的项目依赖项都将在这里。..
为什么会这样?
此外,我注意到你必须编写数据库配置两次。为什么在spring Boot config中已经设置了这个参数?

进度

所以将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

在针对空数据库运行时。

fnx2tebb

fnx2tebb1#

原来我需要加一些无证的魔法酱料。

diff.dependsOn compileJava
diffChangeLog.dependsOn compileJava
generateChangelog.dependsOn compileJava

dependencies {
   // as before
   liquibaseRuntime sourceSets.main.output // replaces liquibaseRuntime files('src/main')
}
g9icjywg

g9icjywg2#

我知道这是一个老线索,但我想为那些后来找到这个答案的人补充一些澄清。..
liquibaseRuntime配置不继承任何其他配置。这是因为在大多数情况下,Liquibase只需要能够解析更改日志并连接到数据库。要使用Hibernate模块,或者从代码生成更改日志,您需要在liquibaseRuntime中添加额外的东西,例如Hibernate或Spring Data,并且您需要srcSets.main.output才能找到您的项目文件本身。
评论说:“你所有的正常项目依赖将在这里除了。..”指的是你在块中有一堆其他的依赖项来构建和运行你的项目,而不是说它们是liquibaseRuntime配置的一部分。如果您确实希望项目中的所有库也成为liquibaseRuntime的一部分,则可以在构建中添加configurations.liquibaseRuntime.extendsFrom configurations.runtime。gradle,或者如果你已经有一个configurations块,你可以添加liquibaseRuntime.extendsFrom runtime到这个块。这应该会将所有项目依赖项和项目文件本身添加到liquibaseRuntime中。
希望这能帮上忙。

5lwkijsr

5lwkijsr3#

这就是我使用Gradle 6的方式。3.0:

configurations {
    liquibaseRuntime.extendsFrom runtimeClasspath
}
dependencies {
    liquibaseRuntime sourceSets.main.output
}

相关问题