Search code examples
kotlingradlejooqcode-generation

Code generation with jooq and gradle-jooq-plugin (kotlin)


Im trying to use jooq with gradle (kotlin). Source: gradle-jooq-plugin

I'm am trying it since yesterday, now i do not have any resources to look at.

I assume i don't get it right, since i am new to kotlin. But the examples did not work for me either (i know the explanation of the plugin is good and the examples are easy..)

I am very thankful if any of you can lead me where i did a mistake, because i am more than curious.

Following is a snippet of my build.gradle file. I am testing with Junit 5 (if it would have any impact, i guess not)

import nu.studer.gradle.jooq.JooqEdition

plugins {
    java
    jacoco // test coverage and reports
    id("org.springframework.boot") version "2.2.6.RELEASE"
    id("org.sonarqube") version "2.8"
    id("nu.studer.jooq") version "4.1"
    id("java-library")
    `kotlin-dsl`
}

apply(plugin = "io.spring.dependency-management")
apply(plugin = "nu.studer.jooq")

dependencies {
    apply(plugin = "nu.studer.jooq")

    // ### Spring ###
    implementation("org.springframework.boot:spring-boot-starter-web")
    implementation("org.springframework.boot:spring-boot-starter-jdbc")
    implementation("org.springframework.boot:spring-boot-starter-jooq")
    compileOnly("org.springframework.boot:spring-boot-starter-actuator")


    // ### Database ###
    implementation("org.postgresql:postgresql:42.2.11")
    implementation("org.liquibase:liquibase-core:3.8.8")
    implementation("org.jooq:jooq")
    jooqRuntime("postgresql:postgresql:9.1-901.jdbc4")

}

ext {
    jooq.version = "3.12.3"
}

java {
    sourceCompatibility = JavaVersion.VERSION_11
    targetCompatibility = JavaVersion.VERSION_11
}


jooq {
    version = "3.12.3"
    edition = JooqEdition.OSS

    "sample"(sourceSets["main"]) {

    }
}

My problem

The "sample"(sourceSets["main"]) is from here. A gradle (kotlin) example. But i get the following error:

Expression '"sample"' of type 'String' cannot be invoked as a function. The function 'invoke()' is not found


Solution

  • In your jooq configuration, the sample function you are using is set as string and not function.

    Check at the documentation here: https://github.com/etiennestuder/gradle-jooq-plugin

    You should have this:

    jooq {
      version = '3.12.3'
      edition = 'OSS'
      generateSchemaSourceOnCompilation = true
      sample(sourceSets.main) {
        jdbc {
          driver = 'org.postgresql.Driver'
          url = 'jdbc:postgresql://localhost:5432/sample'
          user = 'some_user'
          password = 'secret'
          properties {
            property {
              key = 'ssl'
              value = 'true'
            }
          }
        }
        generator {
          name = 'org.jooq.codegen.DefaultGenerator'
          strategy {
            name = 'org.jooq.codegen.DefaultGeneratorStrategy'
            // ...
          }
          database {
            name = 'org.jooq.meta.postgres.PostgresDatabase'
            inputSchema = 'public'
            forcedTypes {
              forcedType {
                name = 'varchar'
                expression = '.*'
                types = 'JSONB?'
              }
              forcedType {
                name = 'varchar'
                expression = '.*'
                types = 'INET'
              }
            }
            // ...
          }
          generate {
            relations = true
            deprecated = false
            records = true
            immutablePojos = true
            fluentSetters = true
            // ...
          }
          target {
            packageName = 'nu.studer.sample'
            // directory = ...
          }
        }
      }
    }
    

    Btw, to be able to have Jooq to generate your code, you must provide it with the connection string to your database.