无法让 liquibase-hibernate 插件与 gradle 一起工作

Posted

技术标签:

【中文标题】无法让 liquibase-hibernate 插件与 gradle 一起工作【英文标题】:Unable to get liquibase-hibernate plugin work with gradle 【发布时间】:2018-08-19 13:38:13 【问题描述】:

在尝试将 liquibase:hibernate 插件与 spring-boot:gradle:postgres 集成时,我在运行 gradle diff 命令时遇到了这个错误。

Unexpected error running Liquibase: org/springframework/orm/jpa/persistenceunit/DefaultPersistenceUnitManager

SEVERE 3/11/18 5:27 PM: liquibase: org/springframework/orm/jpa/persistenceunit/DefaultPersistenceUnitManager
java.lang.NoClassDefFoundError: org/springframework/orm/jpa/persistenceunit/DefaultPersistenceUnitManager
        at liquibase.ext.hibernate.database.HibernateSpringPackageDatabase.createEntityManagerFactory(HibernateSpringPackageDatabase.java:69)
        at liquibase.ext.hibernate.database.HibernateEjb3Database.buildMetadataFromPath(HibernateEjb3Database.java:51)
        at liquibase.ext.hibernate.database.HibernateDatabase.buildMetadata(HibernateDatabase.java:136)
        at liquibase.ext.hibernate.database.HibernateDatabase.setConnection(HibernateDatabase.java:75)
        at liquibase.database.DatabaseFactory.findCorrectDatabaseImplementation(DatabaseFactory.java:131)
        at liquibase.database.DatabaseFactory.openDatabase(DatabaseFactory.java:151)
        at liquibase.integration.commandline.CommandLineUtils.createDatabaseObject(CommandLineUtils.java:85)
        at liquibase.integration.commandline.Main.createReferenceDatabaseFromCommandParams(Main.java:1283)
        at liquibase.integration.commandline.Main.doMigration(Main.java:996)
        at liquibase.integration.commandline.Main.run(Main.java:188)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93)
        at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
        at org.codehaus.groovy.runtime.callsite.StaticMetaMethodSite.invoke(StaticMetaMethodSite.java:46)
        at org.codehaus.groovy.runtime.callsite.StaticMetaMethodSite.call(StaticMetaMethodSite.java:91)
        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:125)
        at org.liquibase.gradle.LiquibaseTask.runLiquibase(LiquibaseTask.groovy:113)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)

这是我的 build.gradle:



    buildscript 
        ext 
            springBootVersion = '2.0.0.RELEASE'
            runList = 'main'
            diffLog = '$projectDir/src/main/resources/db/changelog/db.changelog-master.xml'
        
        repositories 
            mavenCentral()
            maven  url 'https://plugins.gradle.org/m2/' 
            maven  url 'https://repo.spring.io/libs-snapshot' 
        
        dependencies 
            classpath("org.springframework.boot:spring-boot-gradle-plugin:$springBootVersion")
            classpath 'org.liquibase:liquibase-gradle-plugin:1.2.4'
            classpath 'org.liquibase:liquibase-core'
            classpath 'org.postgresql:postgresql:42.2.1'
            classpath 'org.liquibase.ext:liquibase-hibernate5:3.6'
        
    

    apply plugin: 'java'
    apply plugin: 'eclipse'
    apply plugin: 'org.springframework.boot'
    apply plugin: 'io.spring.dependency-management'
    apply plugin: 'org.liquibase.gradle'

    group = 'com.project.sample'
    version = '0.0.1-SNAPSHOT'
    sourceCompatibility = 1.8
    diffLog = '$projectDir/src/main/resources/db/changelog/db.changelog-$version.xml'

    repositories 
        mavenCentral()
    


    dependencies 
        compile('org.springframework.boot:spring-boot-starter-data-jpa')
        compile('org.springframework.boot:spring-boot-starter-security')
        compile('org.springframework.boot:spring-boot-starter-web')
        compile('org.liquibase:liquibase-core')
        compile('org.springframework.session:spring-session-core')
        runtime('org.postgresql:postgresql')
        testCompile('org.springframework.boot:spring-boot-starter-test')
        testCompile('org.springframework.security:spring-security-test')
    

    liquibase 
        activities 
            //noinspection GroovyAssignabilityCheck
            main 
                changeLogFile project.ext.diffLog
                url 'jdbc:postgresql://localhost:5432/quora'
                username 'ueducation'
                password 'u3duc@tion'
                referenceUrl 'hibernate:spring:com.project.sample.src.main.web.entities?dialect=org.hibernate.dialect.PostgreSQL9Dialect'
                referenceDriver 'liquibase.ext.hibernate.database.connection.HibernateDriver'
                logLevel 'debug'
            
            runList = project.ext.runList
        
    

版本:

Spring-data-jpa : 2.0.5 休眠:5.2.14 Liquibase-hibernate5 插件:3.6

我尝试浏览了很多关于 SO 及周边的帖子,但无法弄清楚。 任何帮助表示赞赏。

【问题讨论】:

【参考方案1】:

所以,java.lang.NoClassDefFoundError 意味着类没有被类加载器加载。 因此,只需要将 sprind-data-jpa 添加到类路径中。 build.gradle 中更新后的 buildscript 看起来像

buildscript 
    ext 
        springBootVersion = '2.0.0.RELEASE'
        runList = 'main'
        diffLog = '$projectDir/src/main/resources/db/changelog/db.changelog-master.xml'
    
    repositories 
        mavenCentral()
        maven  url 'https://plugins.gradle.org/m2/' 
        maven  url 'https://repo.spring.io/libs-snapshot' 
    
    dependencies 
        classpath("org.springframework.boot:spring-boot-gradle-plugin:$springBootVersion")
        classpath 'org.liquibase:liquibase-gradle-plugin:1.2.4'
        classpath 'org.liquibase:liquibase-core'
        classpath 'org.postgresql:postgresql:42.2.1'
        classpath 'org.liquibase.ext:liquibase-hibernate5:3.6'
        classpath 'org.springframework.data:spring-data-jpa:2.0.5.RELEASE'
    


【讨论】:

以上是关于无法让 liquibase-hibernate 插件与 gradle 一起工作的主要内容,如果未能解决你的问题,请参考以下文章

如果电脑机箱前端开关坏了,怎么让主板启动

笔记本插拔网线后网络无法连接

esxi 不插显示器 无法进系统

键盘插上后,电脑无法检测键盘.

解决IBM E49插电源+电池无法开机,只插电池可以开机的问题

div如何插文字?