Liquibase Hibernate Plugin Does Not Work

ε祈祈猫儿з 提交于 2019-12-05 07:06:23

I got it working by adding these jars to my classpath. This is super confusing and not well documented. The process I went through was:

  1. Download the source for the correct plugin project found here (https://github.com/liquibase/liquibase-hibernate/releases) in my case it was liquibase-hibernate4-3.5.

  2. Run mvn dependency:copy-dependencies. This dumps them into /target/dependency/. Copy all these jars and put them into your LIQUIBASE_HOME/lib directory.

  3. I'm using gradle so I used a custom task to copy all my dependencies. If you're using maven you can use the same step from 2 on your own project to fetch all your depdenencies. I copied these libs from my output directory into the LIQUIBASE_HOME/lib directory.

    task copyToLib(type: Copy) {
        into "$buildDir/output/libs"
        from configurations.runtime
    }
    
  4. I also put the correct hibernate-liquibase-4.3.5.jar into the LIQUIBASE_HOME/lib directory.

That gave me all the dependencies I needed for the plugin.

This is a big nasty ball of mess, but what can you do :(

If you check the source code for this plugin, the following dependencies (all the spring dependencies) are marked as "provided" scope in the pom:

spring-test
spring-jdbc
spring-beans
spring-context
spring-orm

So the plugin will assume these have been provided on the classpath by your app, but this won't automatically happen in your maven build, as it will not run with the same classpath that your application will.

So, to fix, add the dependencies in your plugin, easier than doing the jar file shuffle:

<plugin>
            <groupId>org.liquibase</groupId>
            <artifactId>liquibase-maven-plugin</artifactId>
            <version>3.4.1</version>
            <dependencies>

                <dependency>
                    <groupId>org.liquibase.ext</groupId>
                    <artifactId>liquibase-hibernate5</artifactId>
                    <version>3.6</version>
                </dependency>
                <dependency>
                    <groupId>org.liquibase</groupId>
                    <artifactId>liquibase-core</artifactId>
                    <version>3.5.3</version>
                </dependency>
                <dependency>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-jdbc</artifactId>
                    <version>4.3.2.RELEASE</version>
                </dependency>
                <dependency>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-beans</artifactId>
                    <version>4.3.2.RELEASE</version>
                </dependency>
                <dependency>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-context</artifactId>
                    <version>4.3.2.RELEASE</version>
                </dependency>
                <dependency>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-orm</artifactId>
                    <version>4.3.2.RELEASE</version>
                </dependency>
            </dependencies>
        </plugin>

If you are keen, you can do a pull request on the github repo to remove the provided scopes from the pom, oh and probably this line too:

            <role>architect</role>

The path I tried was a bit different with the accepted anwer. I created a dev profile in my war's pom.xml (I am using maven) which does exactly the same with the default profile but including the Liquibase-hibernate dependency jar files under the war/WEB-INF/lib. And then instead of using the liquibase command line provided, I use the liquibase.jar file directly with the classpath is my war file:

java -jar $LIQUIBASE/liquibase.jar --classpath=<WAR FILE HERE>  --defaultsFile=<yourpath>/liquibase.properties --logLevel=info

And then created a connvenience script which will build my project with that liquibase-hibernate profile then execute the diffChangeLog command.

I got the same error. I fixed it by removing the hbm2ddl-property in the hibernate.cfg.xml. Somehow the driver class cannot be read in the cfg.xml if this property is on. There is a part in the liquibase-hibernate wiki in git where it says "Why not to use HBM2DDL" but it does not really say to remove it from the cfg.xml.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!