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

多版本PostgreSQL下jOOQ代码生成插件执行报错解决方案咨询

问题解决:jOOQ-codegen-maven多阶段跨PG版本执行报错

你的推测完全正确:jOOQ代码生成器在同一个Maven进程中会缓存数据库方言实例,第一个执行阶段检测的PG版本会被复用给第二个阶段,导致元数据查询SQL不兼容(PostgreSQL 10+用prokind替代了旧版本的proisagg字段)。以下是两种可靠的解决方法:

方法1:用exec-maven-plugin拆分JVM进程执行

通过exec-maven-plugin分别启动独立JVM执行两个代码生成任务,彻底避免缓存共享问题:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>3.1.0</version>
    <executions>
        <!-- 生成PG9.6对应代码 -->
        <execution>
            <id>jooq-codegen-pg96</id>
            <phase>generate-sources</phase>
            <goals>
                <goal>java</goal>
            </goals>
            <configuration>
                <mainClass>org.jooq.codegen.GenerationTool</mainClass>
                <arguments>
                    <argument>jooq-config-1.xml</argument>
                </arguments>
                <classpathScope>compile</classpathScope>
            </configuration>
        </execution>
        <!-- 生成PG14对应代码 -->
        <execution>
            <id>jooq-codegen-pg14</id>
            <phase>generate-sources</phase>
            <goals>
                <goal>java</goal>
            </goals>
            <configuration>
                <mainClass>org.jooq.codegen.GenerationTool</mainClass>
                <arguments>
                    <argument>jooq-config-2.xml</argument>
                </arguments>
                <classpathScope>compile</classpathScope>
            </configuration>
        </execution>
    </executions>
</plugin>

方法2:拆分到不同Maven Profile

将两个代码生成任务放到独立的Profile中,执行时分别触发不同Profile,每个Profile独立运行:

<profiles>
    <profile>
        <id>pg96</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.jooq</groupId>
                    <artifactId>jooq-codegen-maven</artifactId>
                    <version>3.12.3</version>
                    <executions>
                        <execution>
                            <id>jooq-codegen-pg96</id>
                            <phase>generate-sources</phase>
                            <goals>
                                <goal>generate</goal>
                            </goals>
                            <configuration>
                                <configurationFile>jooq-config-1.xml</configurationFile>
                            </configuration>
                        </execution>
                    </executions>
                    <dependencies>
                        <!-- 匹配PG9.6的JDBC驱动版本 -->
                        <dependency>
                            <groupId>org.postgresql</groupId>
                            <artifactId>postgresql</artifactId>
                            <version>42.2.5</version>
                        </dependency>
                    </dependencies>
                </plugin>
            </plugins>
        </build>
    </profile>
    <profile>
        <id>pg14</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.jooq</groupId>
                    <artifactId>jooq-codegen-maven</artifactId>
                    <version>3.12.3</version>
                    <executions>
                        <execution>
                            <id>jooq-codegen-pg14</id>
                            <phase>generate-sources</phase>
                            <goals>
                                <goal>generate</goal>
                            </goals>
                            <configuration>
                                <configurationFile>jooq-config-2.xml</configurationFile>
                            </configuration>
                        </execution>
                    </executions>
                    <dependencies>
                        <!-- 匹配PG14的JDBC驱动版本 -->
                        <dependency>
                            <groupId>org.postgresql</groupId>
                            <artifactId>postgresql</artifactId>
                            <version>42.5.4</version>
                        </dependency>
                    </dependencies>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>

执行时分别运行:

mvn generate-sources -P pg96
mvn generate-sources -P pg14

额外注意事项

  • 确保两个jooq-config-*.xml的<dialect>字段指定对应版本的方言:PG9.6用org.jooq.postgres.Postgres96Dialect,PG14用org.jooq.postgres.Postgres14Dialect(jOOQ 3.12+支持这些版本方言)。
  • 每个任务对应匹配版本的PG JDBC驱动,避免驱动版本与数据库版本不兼容。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.17 10:35:20