2

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

anjey
  • 93
  • 2
  • 11

1 Answers1

2

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.

Federico Piazza
  • 30,085
  • 15
  • 87
  • 123
  • Thank you for your answer. First, i am aware the database connection has to be there. But i left it out, since the part above it won't work. I think your example is in groovy, but i am using kotlin. And for kotlin there is an [example](https://github.com/etiennestuder/gradle-jooq-plugin/blob/master/example/use_kotlin_dsl/build.gradle.kts) And i tried it without quotes, but then i get an error message `unresolved reference: sample` – anjey Apr 13 '20 at 17:45
  • 1
    @anjey, sorry. Can you please check at here https://github.com/etiennestuder/gradle-jooq-plugin/tree/master/example/use_kotlin_dsl_with_groovy there you have another Kotlin example. It uses `sample` as well through call `apply`. Gradle is groovy code, so I still don't see why you have to use Kotlin there, anyway I think it should work through that example. In addition, did you check https://github.com/rohanprabhu/kotlin-dsl-gradle-jooq-plugin? – Federico Piazza Apr 13 '20 at 18:00
  • Thank you, i took the first link. At least something is happening now. Still a sidenote. I would like a **clean** version of it in kotlin dsl, and not to have it mixed with groovy. May be it is not that important in regards of good practice? Anyway thank your for the solution! – anjey Apr 13 '20 at 18:22
  • @anjey I agree, I have edited my previous comment since I found a kotlin dsk gradle jooq plugin. You could take a look at it, it might be what you like. – Federico Piazza Apr 13 '20 at 18:56
  • I will look into it, thank you. As i was guessing, there is an issue with the kotlin dsl pluging. https://github.com/etiennestuder/gradle-jooq-plugin/issues/120 . Also a workaround is at the link for anyone who is interested. – anjey Apr 13 '20 at 20:54