1
votes

I am trying to use liquibase-gradle-plugin to generate database diffs between a database and Springboot Entities. However, I always got the error saying

liquibase.exception.LiquibaseException: Unexpected error running Liquibase: java.lang.RuntimeException: Cannot find database driver: class org.postgresql.Driver

I put classpath depencies in the buildscripts block according to some google result. But it does not help. Below is my build.gradle

buildscript {
    ext {
        springBootVersion = '1.5.6.RELEASE'
    }
    repositories {
        mavenCentral()
        maven { url "https://repo.spring.io/milestone" }
        maven {
            url "https://plugins.gradle.org/m2/"
          }
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
        classpath('org.postgresql:postgresql:9.4.1212')
    }
}

//When the liquibase plugin is applied, it creates a Gradle task for each command supported by Liquibase.
plugins {
    id 'org.liquibase.gradle' version '1.2.4'
}

apply from: 'liquibase.gradle'

liquibase {
    activities {
      diffMain {
        url gradle.ext.url
        username gradle.ext.username
        password gradle.ext.password
        referenceUrl gradle.ext.referenceUrl
        driver org.postgresql.Driver
      }
    }
    runList = 'diffMain'
}
1
Did you check if your postgresql jar gets resolved at all by gradle? Does it exists in the repos you spefified? Also, check to see if using classpath "org.postgresql:postgresql:9.4.1212" instead of classpath('org.postgresql:postgresql:9.4.1212') makes a difference. I don't know how gradle/groovy treats this though and if it matters or not. - Jens
Finally I gave up liquibase gradle plugin and used liquibase CLI to do the diffChangeLog. - Charles Ju

1 Answers

0
votes

You might want to define driver location in activities field of liquibase configuration with classpath property:

liquibase {
    activities {
      diffMain {
        url gradle.ext.url
        username gradle.ext.username
        password gradle.ext.password
        referenceUrl gradle.ext.referenceUrl
        driver org.postgresql.Driver

        /////
        classpath "/driver-location/postgresql-42.2.19.jar"
        /////
      }
    }
    runList = 'diffMain'
}

But more common and right approach is to add driver into liquibase runtime environment:

dependencies {
    liquibaseRuntime "org.postgresql:postgresql:42.2.24"
}