Home > Net >  jOOQ codegen strategy class not found
jOOQ codegen strategy class not found

Time:06-17

I'm trying to add a -Pojo suffix to my generated jOOQ pojos.

The strategy implementation is straightforward enough:

package my.app.jooq.strategy

import org.jooq.codegen.DefaultGeneratorStrategy
import org.jooq.codegen.GeneratorStrategy
import org.jooq.codegen.GeneratorStrategy.Mode.POJO
import org.jooq.meta.Definition

class MyGeneratorStrategy : DefaultGeneratorStrategy() {
    override fun getJavaClassName(definition: Definition, mode: GeneratorStrategy.Mode): String {
        return when (mode) {
            POJO -> super.getJavaClassName(definition, mode)   "Pojo"
            else -> super.getJavaClassName(definition, mode)
        }
    }
}

but codegen just refuses to pick it up. (ClassNotFoundException).

According to https://groups.google.com/g/jooq-user/c/LM5ioRHNhJw :

you would have to create a separate project/module only for that strategy class, in order to create a dependency graph like so:

  1. Code generation module... depends on
  2. Strategy module... depends on
  3. jOOQ libraries

so I did... jOOQ doesn't care.

PM org.jooq.tools.JooqLogger error
SEVERE: Error in file: /home/user/code/my-app/backend/db/build/tmp/generateMyAppJooq/config.xml. Error : my.app.jooq.strategy.MyGeneratorStrategy
java.lang.ClassNotFoundException: my.app.jooq.strategy.MyGeneratorStrategy
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
    at org.jooq.codegen.GenerationTool.loadClass0(GenerationTool.java:1075)
    at org.jooq.codegen.GenerationTool.loadClass(GenerationTool.java:1005)
    at org.jooq.codegen.GenerationTool.run0(GenerationTool.java:405)
    at org.jooq.codegen.GenerationTool.run(GenerationTool.java:233)
    at org.jooq.codegen.GenerationTool.generate(GenerationTool.java:228)
    at org.jooq.codegen.GenerationTool.main(GenerationTool.java:200)

here's how the codegen module is set up:

plugins {
    id 'nu.studer.jooq' version "${plugin_jooq}"
    id 'io.spring.dependency-management'
}

dependencyManagement {
    imports {
        mavenBom SpringBootPlugin.BOM_COORDINATES
        mavenBom "org.jetbrains.kotlin:kotlin-bom:${fw_kotlin}"
    }
}

dependencies {
    api "org.postgresql:postgresql"

    implementation project(":backend-jooq-config")

    api "org.springframework.boot:spring-boot-starter-jooq"
    implementation "org.jooq:jooq-meta"
    implementation "org.jooq:jooq-codegen"

    jooqGenerator "org.postgresql:postgresql"
}

jooq {
    version = dependencyManagement.importedProperties['jooq.version']
    edition = nu.studer.gradle.jooq.JooqEdition.OSS

    configurations {
        myApp {
            generateSchemaSourceOnCompilation = true

            generationTool {
                logging = org.jooq.meta.jaxb.Logging.WARN
                jdbc {
                    driver = 'org.postgresql.Driver'
                    url = 'jdbc:postgresql://localhost:5432/myapp'
                    user = 'user'
                    password = 'password'
                    properties {
                        property {
                            key = 'PAGE_SIZE'
                            value = 2048
                        }
                    }
                }
                generator {
                    name = 'org.jooq.codegen.DefaultGenerator'
                    strategy {
                        name = 'my.app.jooq.strategy.MyGeneratorStrategy'
                    }
                    database {
                        name = 'org.jooq.meta.postgres.PostgresDatabase'
                        inputSchema = 'public'
                        includes = '.*'
                        excludes = ''
                    }
                    generate {
                        daos = true
                        springAnnotations = true
                    }
                    target {
                        directory = 'src/main/java'
                        packageName = 'my.app.db'
                    }
                }
            }
        }
    }
}

I don't see what I'm doing wrong.

And yes, the strategy is being compiled:

find .. -name "*GeneratorStrategy*"
../backend/jooq-config/build/classes/kotlin/main/my/app/jooq/strategy/MyGeneratorStrategy.class
../backend/jooq-config/build/classes/kotlin/main/my/app/jooq/strategy/MyGeneratorStrategy$WhenMappings.class
../backend/jooq-config/src/main/kotlin/my/app/jooq/strategy/MyGeneratorStrategy.kt

how do I resolve this?

CodePudding user response:

Fixing your configuration

There's an example here on how to set up this third party code generation plugin to use a custom strategy: https://github.com/etiennestuder/gradle-jooq-plugin/tree/master/example/configure_custom_generator_strategy

As you can see, the dependency is specified as:

jooqGenerator project(':backend-jooq-config')

Not

implementation project(':backend-jooq-config')

Using a declarative configuration instead

For such simple cases, jOOQ also offers an out-of-the-box configuration called "matcher strategy," where it matches names and replaces them by something else. E.g.

generator {
  strategy {
    matchers {
      tables {
        table {
          pojoClass {
            transform = 'PASCAL'
            expression = '\$0_POJO'
          }
        }
      }
    }
  }
}
  • Related