kotlingradlejooqjooq-codegen

Failed when upgrading to use jOOQ Gradle plugin to generate codes


jOOQ 3.19.0 brings an official gradle codegen plugin instead of the 3rd party plugin, I tried to change the original config to the following in my Gradle Kotlin project.

jooq {
    //version.set(${jooqVersion}")  // the default (can be omitted)
    //edition.set(JooqEdition.OSS)  // the default (can be omitted)

    executions {
        create("main") {  // name of the jOOQ configuration
            //generateSchemaSourceOnCompilation =true   // default (can be omitted)

            configuration {
                logging = org.jooq.meta.jaxb.Logging.DEBUG
                jdbc = null // only required for gen from active databases.

                generator {
                    name = "org.jooq.codegen.KotlinGenerator"
                    database {
                        name = "org.jooq.meta.extensions.ddl.DDLDatabase" // gen from ddl schema.

                        // commoutted out this, see: https://github.com/etiennestuder/gradle-jooq-plugin/issues/222
                        // inputSchema = "public"
                        properties {

                            // Specify the location of your SQL script.
                            // You may use ant-style file matching, e.g. /path/**/to/*.sql
                            //
                            // Where:
                            // - ** matches any directory subtree
                            // - * matches any number of characters in a directory / file name
                            // - ? matches a single character in a directory / file name
                            property {
                                key = "scripts"
                                value = "src/main/resources/schema.sql"
                            }

                            // The sort order of the scripts within a directory, where:
                            //
                            // - semantic: sorts versions, e.g. v-3.10.0 is after v-3.9.0 (default)
                            // - alphanumeric: sorts strings, e.g. v-3.10.0 is before v-3.9.0
                            // - flyway: sorts files the same way as flyway does
                            // - none: doesn't sort directory contents after fetching them from the directory
                            property {
                                key = "sort"
                                value = "semantic"
                            }

                            // The default schema for unqualified objects:
                            //
                            // - public: all unqualified objects are located in the PUBLIC (upper case) schema
                            // - none: all unqualified objects are located in the default schema (default)
                            //
                            // This configuration can be overridden with the schema mapping feature
                            property {
                                key = "unqualifiedSchema"
                                value = "none"
                            }

                            // The default name case for unquoted objects:
                            //
                            // - as_is: unquoted object names are kept unquoted
                            // - upper: unquoted object names are turned into upper case (most databases)
                            // - lower: unquoted object names are turned into lower case (e.g. PostgreSQL)
                            property {
                                key = "defaultNameCase"
                                value = "lower"
                            }
                        }
                    }
                    generate {
                        isPojosAsKotlinDataClasses = true // use data classes
                    }
                    target {
                        packageName = "com.example.demo.jooq"
                        directory = "build/generated-src/jooq/main"  // default (can be omitted)
                    }
                    strategy{
                        name = "org.jooq.codegen.DefaultGeneratorStrategy"
                    }
                }
            }
        }
    }
}

But when running the command ./gradlew clean build -x test, it did not generate the database meta classes as expected.

The file change diff can be view from this PR: https://github.com/hantsy/spring-r2dbc-sample/pull/320/files

The original working version is there: https://github.com/hantsy/spring-r2dbc-sample/tree/master/jooq-kotlin-co-gradle


Solution

  • This seems to be a bug in the original implementation of jOOQ 3.19.0, which should be fixed by 3.19.1:

    The new plugin doesn't resolve relative paths, and as such (as you can see if you turn on --info logging), you'll generate your code somewhere in a path relative to the gradle daemon:

      target dir             : C:\Users\lukas\.gradle\daemon\8.5\target\generated-sources\jooq
    

    Before the fix, the workaround is to explicitly pass absolute paths, e.g. like this:

    target {
        directory = "${projectDir}/build/generated-src/jooq/main"
    }
    

    Alternatively, you can provide the basedir to jOOQ explicitly:

    target {
        basedir = "${projectDir}"
        directory = "build/generated-src/jooq/main"
    }