Home > Enterprise >  JOOQ - Codegen - How to generate DAOs and POJOs into two differents Maven module
JOOQ - Codegen - How to generate DAOs and POJOs into two differents Maven module

Time:09-06

I use JOOQ with a PostgreSQL database. For the moment all the code generated by JOOQ is in the same Maven project.

I would like to know if it is possible to separate the JOOQ code generation in two separate Maven modules:

  • in a server module: JOOQ records and DAOs generation
  • in a common module: generation of POJOs only.

The objective is to share the common module between the server and the client modules.

The configuration of the target in my generator is as follows:

<target>
    <packageName>my.package</packageName>
    <directory>target/generated-sources/gen-jooq/</directory>
</target>

CodePudding user response:

There are several approaches to this:

Use a GeneratorStrategy only

You can implement a custom GeneratorStrategy that will completely rewrite the output path of the DAOs and/or POJO types to something you know happens to be a different maven module.

Use multiple code generation runs

Like with many other scenarios where you want to cleanly separate code generation output (e.g. in this question: jOOQ code generation for multiple databases with different schemas), you could specify multiple code generation executions like this:

<plugin>
    <groupId>org.jooq</groupId>
    <artifactId>jooq-codegen-maven</artifactId>
    <executions>
        <execution>
            <id>exec-1</id>
            <phase>generate-sources</phase>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>...</configuration>
        </execution>
        <execution>
            <id>exec-2</id>
            <phase>generate-sources</phase>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>...</configuration>
        </execution>
    </executions>
</plugin>

The two executions can have shared and independent configuration, including the <target>. The generation that produces DAO types will always also produce POJO types, so you might have to remove those from the code generation output, e.g. by deleting the directory right after code generation.

You can still use a GeneratorStrategy to specify diferent packages, if needed

Using some third party packaging tooling

Just because jOOQ's code generator produces everything in a single directory hierarchy doesn't mean you have to leave things this way. The maven-shade-plugin or other similar plugins could be used to split your code after it has been generated, or even after it has been compiled. I won't list all the possible options here, but this will certainly give you an idea.

  • Related