Gradle Jooq configure a custom generator

26 Views Asked by At

I would like to change the code generation to a custom one, only it fails because the class cannot be found. It looks like I need add the module as a dependency to this task, but I cannot figure out how to do this in gradle.

My gradle tasks looks as follows.

val jooqGenerateTask = task("jooqGenerate") {
    doLast {
        val initDir = rootProject.projectDir.absolutePath + "/docker/postgres/"
        val container = KPostgreSQLContainer("postgres:13.12")
            .withDatabaseName("jooq")
            .withFileSystemBind(initDir, "/docker-entrypoint-initdb.d")
            .withTmpFs(
                mapOf(
                    "/var/lib/postgresql/data" to "rw"
                )
            )
        container.start()

        val config = Configuration()
            .withJdbc(
                Jdbc()
                    .withDriver("org.postgresql.Driver")
                    .withUrl(container.jdbcUrl)
                    .withUser(container.username)
                    .withPassword(container.password)
            )
            .withGenerator(
                Generator()
                    // Cannot find this class
                    .withName("my.custom.GeneratorKt")
                    .withDatabase(
                        Database()
                            .withName("org.jooq.meta.postgres.PostgresDatabase")
                            .withIncludes(".*")
                            .withExcludes("schema_version")
                            .withInputSchema("public")
                            .withIncludeTables(true)
                            .withIncludeRoutines(false)
                            .withIncludePackages(false)
                            .withIncludeUDTs(false)
                            .withIncludeSequences(false)
                            .withForcedTypes(
                                ForcedType()
                                    .withUserType(JsonObject::class.java.name)
                                    .withConverter(JSONBToJsonObjectConverter::class.java.name)
                                    .withIncludeTypes("jsonb")
                            )
                    )
                    .withGenerate(
                        Generate()
                            .withDeprecated(false)
                            .withRecords(true)
                            .withInterfaces(false)
                            .withFluentSetters(true)
                            .withPojos(false)
                            .withDaos(true)
                    )
                    .withTarget(
                        JooqTarget()
                            .withPackageName("my.jooq")
                            .withDirectory("$projectDir/src/main/java")
                    )
                    .withStrategy(
                        Strategy()
                            .withName("io.github.jklingsporn.vertx.jooq.generate.VertxGeneratorStrategy")
                    )
            )
        GenerationTool.generate(config)
        container.stop()
    }
}

Which throws an exception as.

Caused by: java.lang.ClassNotFoundException: my.custom.GeneratorKt
        at org.gradle.internal.classloader.VisitableURLClassLoader$InstrumentingVisitableURLClassLoader.findClass(VisitableURLClassLoader.java:186)
        at org.jooq.meta.ClassUtils.loadClass(ClassUtils.java:55)
        at org.jooq.codegen.GenerationTool.loadClass(GenerationTool.java:1224)
        at org.jooq.codegen.GenerationTool.run0(GenerationTool.java:399)
        at org.jooq.codegen.GenerationTool.run(GenerationTool.java:246)
        at org.jooq.codegen.GenerationTool.generate(GenerationTool.java:241)
        at Build_gradle$jooqGenerateTask$1$2.execute(build.gradle.kts:305)
        at Build_gradle$jooqGenerateTask$1$2.execute(build.gradle.kts:234)
        at org.gradle.api.internal.AbstractTask$TaskActionWrapper.execute(AbstractTask.java:835)
        at org.gradle.api.internal.AbstractTask$TaskActionWrapper.execute(AbstractTask.java:808)
        at org.gradle.api.internal.tasks.execution.TaskExecution$3.run(TaskExecution.java:248)
        ... 114 more
2

There are 2 best solutions below

1
Lukas Eder On BEST ANSWER

You have to add whatever project contains your GeneratorKt class to your buildscript { } dependencies, and make sure it is built before you generate jOOQ code, see also:

0
Chris On

I did try to add it to the buildScript the problem I ran into was a bootstrap issue, I did figure it out eventually. It's possible to share code with a composite build, which is available in the build step and the app code.

https://docs.gradle.org/current/userguide/partr3_multi_project_builds.html

So the trick was having the code in a seperate module, and use it with

includeBuild("shared")