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

时间:2018-03-11 12:17:44

标签: hibernate spring-boot gradle liquibase liquibase-hibernate

在尝试将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
  • Hibernate:5.2.14
  • Liquibase-hibernate5插件:3.6

我尝试在SO和周围发布了很多帖子,但无法弄明白。 任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:0)

因此,java.lang.NoClassDefFoundError意味着类加载器未加载该类。 因此,只需要将sprind-data-jpa添加到类路径中。 buildscript中更新的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'
        classpath 'org.springframework.data:spring-data-jpa:2.0.5.RELEASE'
    }

}