java - liquibase gradle postgresql 错误的驱动程序

标签 java spring postgresql gradle liquibase

环境:

  • Spring 启动(STS 3.9.1)
  • Gradle 2.13
  • PostgreSQL 9.6.5
  • Liquibase Gradle 插件 (liquibase-gradle-plugin:1.2.4)

场景:

我们有一个 spring boot rest api,我们希望它有一个在进程的某个阶段更新数据库的 CI 进程,为此我们想为它使用 gradle

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath('org.springframework.boot:spring-boot-gradle-plugin:1.5.7.RELEASE')
// tag::build[]
        classpath('se.transmode.gradle:gradle-docker:1.2')
// end::build[]
        classpath('org.liquibase:liquibase-gradle-plugin:1.2.4')
        classpath('org.postgresql:postgresql:42.1.4')
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
// tag::plugin[]
apply plugin: 'docker'
// end::plugin[]
apply plugin: 'org.liquibase.gradle'

bootRun {
    systemProperty 'spring.profiles.active', System.properties['spring.profiles.active']
}


def changeLog = "$projectDir/src/main/resources/db/db.changelog-master.yaml"

task('dev') << {
println 'executing dev'
liquibase{
    activities {
        main {
        changeLogFile changeLog
        url 'postgresql://localhost/mydatabase'
        username 'postgres'
        password 'mysecret'
        driver 'org.postgresql.Driver'
        }
    }
}
}

jar {
    baseName = 'applicationTestLiquibasegradle'
    version =  '0.0.3'
}

// This is used as the docker image prefix (org)
group = 'com.cropmetrics'
version = '0.0.3'
sourceCompatibility = 1.8

// tag::task[]
task buildDocker(type: Docker, dependsOn: build) {
  applicationName = jar.baseName
  dockerfile = file('Dockerfile')
  doFirst {
    copy {
      from jar
      into "${stageDir}/target"
    }
  }
}
// end::task[]


repositories {
    mavenCentral()
}


dependencies {
  compileOnly('org.projectlombok:lombok:1.16.18')
  compile('org.springframework.boot:spring-boot-starter')
  compile('org.springframework.boot:spring-boot-starter-web')
  compile('org.springframework.boot:spring-boot-starter-data-jpa')
  // JPA Data (We are going to use Repositories, Entities, Hibernate, etc...)
  compile 'org.springframework.boot:spring-boot-starter-data-jpa'

  compile('org.liquibase:liquibase-core')
  compile('org.postgresql:postgresql:42.1.4.jre7')
  testCompile('org.springframework.boot:spring-boot-starter-test')
}

所以我们创建了一个开发任务来执行这个:

gradle task dev update

哪个应该更新数据库

如果我们允许应用程序自动更新数据库,效果很好,但如果我们想使用 taskt 插件并使用 gradle 任务,我们就会收到错误

    * What went wrong:
Execution failed for task ':update'.
> liquibase.exception.LiquibaseException: Unexpected error running Liquibase: liquibase.exception.DatabaseException: Connection could not be created to postgresql://localhost/cropmetricsdatabase with driver org.postgresql.Driver.  Possibly the wrong driver for the given database URL

所以问题似乎与liquibase-gradle插件有关。

最佳答案

您的数据库 URL 看起来有点奇怪。缺少 jdbc 部分和可能的端口号。

来自 documentation : 驱动程序识别以下形式的 JDBC URL:

jdbc:postgresql:database
jdbc:postgresql:/
jdbc:postgresql://host/database
jdbc:postgresql://host/
jdbc:postgresql://host:port/database
jdbc:postgresql://host:port/

关于java - liquibase gradle postgresql 错误的驱动程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46921580/

相关文章:

java - 从服务异常中检索 Spring Integration 中的消息

java - 为什么在我的 JBoss Tomcat 7 + Spring MVC 项目中出现 HTTP Status 404?

ruby-on-rails - 在 postgres : there is no transaction in progress 上测试规范时发出警告

java - JFrame 关闭时抛出异常

java - 如何从另一个类初始化自己的列表。 JAVA

postgresql - 如何使用 PostgreSql 在代码优先方法中自动创建表

ruby-on-rails - 当且仅当查询不适用于 Rails 和 Postgres 时,如果包含,则全部关闭

java - 使用 JAVA 进行正则表达式过滤

java - 我的线程恰好循环了 43 次然后抛出异常

java - HBase 中的组合键