Skip to content
Advertisement

JOOQ – Codegen – How to generate DAOs and POJOs into two differents Maven module

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>

Solution I solved my problem based on the second strategy in Lukas Eder’s answer.

I have a Jooq generation configuration in the common module. I have another generation configuration in my server module. The 2 configurations share a configuration file for the common parts.

After the generation of the sources, the excess classes are removed by the antrun plugin during the process-sources phase.

The antrun configuration in the common module, only pojos are keeped.

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-antrun-plugin</artifactId>
        <version>1.7</version>
        <executions>
          <execution>
            <phase>process-sources</phase>
            <goals>
              <goal>run</goal>
            </goals>
    
            <configuration>
              <target>
                <delete includeEmptyDirs="true">
                  <fileset dir="target/generated-sources/gen-jooq/my/package/tables/records/" />
                  <fileset dir="target/generated-sources/gen-jooq/my/package/tables/" includes="*.java" />
                  <fileset dir="target/generated-sources/gen-jooq/my/package" includes="*.java" />
                </delete>
              </target>
            </configuration>
          </execution>
        </executions>
      </plugin>

And in the server module, only the pojos are deleted :

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-antrun-plugin</artifactId>
            <version>1.7</version>
            <executions>
                <execution>
                    <phase>process-sources</phase>
                    <goals>
                        <goal>run</goal>
                    </goals>

                    <configuration>
                        <target>
                            <delete includeEmptyDirs="true">
                                <fileset dir="target/generated-sources/gen-jooq/my/package/tables/pojos/" />
                            </delete>
                        </target>
                    </configuration>
                </execution>
            </executions>
        </plugin>

Advertisement

Answer

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.

6 People found this is helpful
Advertisement