0

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())
    }
}
VictorGram
  • 2,521
  • 7
  • 48
  • 82

1 Answers1

1

You should add postgres driver dependency to jooqRuntime configuration as well jooqRuntime 'org.postgresql:postgresql:42.2.14'

UPDATE: You should add postgres driver to your buildscript classpath if you want to use it in gradle script outside of the tasks provided by nu.studer.jooq plugin

buildscript {
    dependencies {
        classpath  'org.postgresql:postgresql:42.2.14'
    }
}
Alexander.Furer
  • 1,817
  • 1
  • 16
  • 24
  • OK, since it's not the task provided by plugin (that correctly adds dependencies from jooqRuntime configuration ), you should be using type `exec` or `javaExec` if you like and specify classpath manually as `configurations.jooqRuntime` – Alexander.Furer Jun 16 '20 at 20:48
  • Not clear about what you are suggesting. May I request little more clarification or an example? Thank you for your response – VictorGram Jun 16 '20 at 21:01
  • You are getting the exception because driver jar belongs to jooq configuration... It's not in classpath of gradle process. As a quick workaround you can also add driver dependency to build script classpath section – Alexander.Furer Jun 16 '20 at 21:07