You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将JOOQ生成的DAO与POJO拆分到两个Maven模块

将JOOQ代码生成拆分到多Maven模块的实现方案

问题背景

我在PostgreSQL数据库中使用JOOQ,目前所有JOOQ生成的代码都集中在同一个Maven项目中。希望将代码生成拆分到两个独立的Maven模块:

  • server模块:生成JOOQ records和DAO
  • common模块:仅生成POJO,用于服务端和客户端模块共享

原生成器的target配置如下:

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

解决方案

我采用Lukas Eder提到的第二种策略解决了该问题:在common和server模块分别配置JOOQ生成逻辑,共享同一个公共配置文件;代码生成完成后,在process-sources阶段通过antrun插件移除各自模块不需要的类。

common模块配置(仅保留POJO)

在common模块中添加antrun插件配置,删除records目录、表相关类及根包下的其他类,只保留POJO:

<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>

server模块配置(仅删除POJO)

在server模块中添加antrun插件配置,仅删除POJO目录:

<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>

内容的提问来源于stack exchange,提问作者PEUGENE

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 22:01:06