首页 > 解决方案 > java.lang.ClassNotFoundException: org.postgresql.Driver from jooq code generating task

问题描述

I am implementing java-spring-boot project ( jdk11, spring boot 2.3.0.RELEASE ) using jooq-hikari-hibernate-postgres for back end.

I have already created the tables using flyway.

I have created a task name "generateJooqRepo" in build.gradle to generate the java code for the repository. The task is running however failing with error message:

Execution failed for task ':generateJooqRepo'.
  > java.lang.ClassNotFoundException: org.postgresql.Driver

Any insight on what I am doing wrong?

build.gradle:

import org.jooq.codegen.*

plugins {
    id 'org.springframework.boot' version '2.3.0.RELEASE'
    id 'org.flywaydb.flyway' version '6.4.4'
    id "nu.studer.jooq" version "4.2"
}

apply plugin: 'io.spring.dependency-management'
apply plugin: 'application'
apply plugin: 'maven'
apply plugin: 'jacoco'
apply plugin: 'idea'
apply plugin: 'nu.studer.jooq'

configurations {
    deployerJars
}


sourceCompatibility = '11.0.1'
targetCompatibility = '11.0.1'

repositories {
    // Use jcenter for resolving your dependencies.
    // You can declare any Maven/Ivy/file repository here.
    jcenter()
    mavenCentral()

}



dependencies {

    //Spring Dependencies
    implementation 'org.springframework.boot:spring-boot-starter-actuator:2.3.0.RELEASE'
    implementation 'org.springframework.boot:spring-boot-starter-web:2.3.0.RELEASE'

    //postges
    jooqRuntime  'org.postgresql:postgresql:42.2.14'
    implementation 'org.postgresql:postgresql:42.2.14'

    //The gradle-jooq-plugin needs dependencies in a separate configuration.
    // It uses the jooqRuntime configuration to detect the needed dependencies, it doesn't use the compile or implementation configuration.
    jooqRuntime group: 'org.jooq', name: 'jooq-meta-extensions', version: '3.13.2'

    //flyway
    compile 'org.flywaydb:flyway-core:6.4.4'

    //jooq dependency
    implementation 'org.springframework.boot:spring-boot-starter-jooq:2.3.0.RELEASE'
    compile 'org.jooq:jooq:3.13.2'
    compile 'org.jooq:jooq-meta:3.13.2'
    compile 'org.jooq:jooq-codegen:3.13.2'


    // This dependency is found on compile classpath of this component.
    implementation 'org.apache.commons:commons-lang3:3.9'
}

group = 'com.ringo.tapos.service'
version = '1.0.0-SNAPSHOT'
mainClassName = "com.ringo.tapos.service.Application"


// Use your favourite XML builder to construct the code generation configuration file
// ----------------------------------------------------------------------------------
task generateJooqRepo {
    doLast {
        def writer = new StringWriter()
        def xml = new groovy.xml.MarkupBuilder(writer)
                .configuration('xmlns': 'http://www.jooq.org/xsd/jooq-codegen-3.13.0.xsd') {
                    jdbc() {
                        driver('org.postgresql.Driver')
                        url('jdbc:postgresql://localhost:5432/postgres')
                        user('postgres')
                        password('postgres')
                        schema('tapos')
                    }
                    generator() {
                        database() {
                        }
                        generate([:]) {
                            pojos true
                            daos true
                            relations true
                            deprecated false
                            records true
                            //immutablePojos false
                        }
                        target() {
                            packageName('com.ringo.tapos.service.repositories')
                            directory('src/main/java')
                        }
                    }
                }
        GenerationTool.generate(writer.toString())
    }
}

标签: javagradlejooqjooq-codegen-maven

解决方案


您还应该将 postgres 驱动程序依赖项添加到 jooqRuntime 配置中 jooqRuntime 'org.postgresql:postgresql:42.2.14'

更新:如果你想在nu.studer.jooq插件提供的任务之外的 gradle 脚本中使用它,你应该将 postgres 驱动程序添加到你的 buildscript 类路径中

buildscript {
    dependencies {
        classpath  'org.postgresql:postgresql:42.2.14'
    }
}

推荐阅读