在Gradle Spring Boot Hibernate项目中设置LiquiBase [英] Setup LiquiBase in a Gradle Spring Boot Hibernate project

查看:97
本文介绍了在Gradle Spring Boot Hibernate项目中设置LiquiBase的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Spring Boot项目中很难设置LiquiBase.我尝试浏览文档并找到一些指南-但它们似乎彼此矛盾:(

I'm having a hard time setting up LiquiBase in my Spring Boot project. I tried looking through the docs and finding some guides - but they seem contradict each other :(

我希望通过Gradle使用LiquiBase,并且希望它从Hibernate生成变更日志,并最终获得一个SQL脚本,我可以在服务器上运行该脚本以将架构更新为适当的版本.

I wish to use LiquiBase via Gradle and I want it to generate the changelogs from Hibernate and end up with a SQL script I can run on the server to update the schema to the appropriate version.

要使其通过Gradle运行,我正在使用此插件 https://github.com/liquibase/liquibase-gradle-plugin ,使用其自述文件中显示的推荐设置.

To get it to run via Gradle I'm using this plugin https://github.com/liquibase/liquibase-gradle-plugin using the recommended setup shown in their README.

要使用Hibernate差异,我正在使用 https://github.com/liquibase/liquibase-休眠

To get the Hibernate diff to work I'm using 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

但是失败了

任务:diffChangeLog liquibase-plugin:运行'main'活动...在2018年9月26日星期三CEST(版本3.6.2)启动Liquibase建于2018-07-03 11:28:09)线程主"中的异常java.lang.NoClassDefFoundError:org/springframework/core/io/ClassPathResource在liquibase.ext.hibernate.database.HibernateSpringPackageDatabase.isXmlFile(HibernateSpringPackageDatabase.java:54)

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 .

It looks like it cannot find Spring Boot. So I then tried removing the liquibaseRuntime but then the LiquiBase Gradle plugin complains that liquibaseRuntime is missing.

似乎我陷入了循环.进行此设置的理智的方法是什么?我真的不想重复 liquibaseRuntime 内的每个依赖项.此外,该文档的字面意思是:

Seems I'm stuck in a loop. What is a sane way of setting this up? I really don't want to repeat every dependency inside the liquibaseRuntime. Also the doc literally says:

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 boot config中已经设置了为什么需要它?

Also... I noticed that you have to write database config twice. Why is that needed when it's already set in 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')

使错误消失.但这仍然行不通.

Makes the errors go away. But it still doesn't work.

运行此命令

.\ gradlew diff

.\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

针对空数据库运行时.是的-它不起作用:(

When running against an empty database. So yeah - it doesn't work :(

推荐答案

原来,我需要添加一些未记录的魔术酱.

Turns out I needed to add some undocumented magic sauce.

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

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

这篇关于在Gradle Spring Boot Hibernate项目中设置LiquiBase的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆