使用 Gradle Kotlin DSL 配置 jOOQ 代码生成(Quarkus & Kotlin)

概述

本文档记录了在一个基于 QuarkusKotlin 的项目中使用 Gradle Kotlin DSL 配置 **jOOQ 代码生成(Codegen)**的步骤和关键配置项。jOOQ 代码生成能够将数据库 Schema 转换为类型安全的 Kotlin/Java 代码,极大地提升开发效率和代码质量。

1. 核心依赖和插件配置

build.gradle.kts 文件中,需要引入 jOOQ Gradle 插件、Quarkus 依赖以及 jOOQ Codegen 所需的 JDBC 驱动。

1.1 插件

Kotlin

1
2
3
4
5
6
7
plugins {
kotlin("jvm") version "2.2.20"
id("io.quarkus")

// jOOQ Codegen Gradle 插件。gradlePluginPortal() 查不到,可能发布到 mavenCentral()
id("org.jooq.jooq-codegen-gradle") version "3.19.23"
}

1.2 依赖配置

关键点: jOOQ 代码生成所需的依赖(jooq-codegen 和 JDBC 驱动)需要添加到 jooqCodegen 配置中,而不是常规的 implementation

Kotlin

1
2
3
4
5
6
7
8
9
10
11
12
13
14
dependencies {
// ... Quarkus 运行依赖,生成可不添加 ...
implementation("io.quarkus:quarkus-jdbc-mysql")
implementation("io.quarkiverse.jooq:quarkus-jooq:2.1.0") // Quarkus jOOQ 集成

// ----------------------------------------------------
// jOOQ 代码生成 (Codegen) 依赖
// ----------------------------------------------------
jooqCodegen("org.jooq:jooq-codegen:3.19.23")
jooqCodegen("org.jooq:jooq-meta:3.19.23")
// **关键:用于代码生成的 MySQL JDBC 驱动**
jooqCodegen("com.mysql:mysql-connector-j:8.3.0")
// ... 其他依赖 ...
}

2. jOOQ 代码生成配置 (jooq 块)

使用 jooq 块来定义代码生成器的详细配置。默认情况下,插件会创建一个名为 jooqCodegenMain 的任务。

Kotlin

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
jooq {
configuration {
// 2.1. JDBC 连接配置
jdbc {
driver = "com.mysql.cj.jdbc.Driver"
// 注意:完整的连接 URL,包含必要的时区和编码参数
url = "jdbc:mysql://localhost:4407/dev-xxx?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=Asia/Shanghai&useSSL=false&allowPublicKeyRetrieval=true"
user = "root"
password = "123456"
}

// 2.2. 代码生成器配置
generator {
// **关键:使用 KotlinGenerator 来生成 Kotlin 代码**
name = "org.jooq.codegen.KotlinGenerator"

database {
name = "org.jooq.meta.mysql.MySQLDatabase"
// 要生成代码的模式 (Schema)。MYSQL 为数据库名称
inputSchema = "dev-xxx"
includes = ".*"
excludes = "flyway_schema_history" // 排除特定表
}

// 2.3. 生成内容配置 (Generate)(选配)
generate {
// 启用生成 Kotlin 数据类作为 POJO
isPojos = true
isPojosAsKotlinDataClasses = true

// 生成 DAO 类 (Data Access Objects)
isDaos = true
isSpringDao = true // 启用 Spring/Quarkus 兼容的 DAO

// 生成 jOOQ Record 类
isRecords = true

// 添加注解,如 @Generated, @NotNull
isGeneratedAnnotation = true
isNullableAnnotation = true
}

// 2.4. 目标配置 (Target)
target {
packageName = "com.example.jooq.generated"
directory = "build/generated-src/jooq/main" // 代码输出目录
}
}
}
}

3. 源码集和任务依赖配置

生成的代码必须被 Gradle 识别并加入到编译路径中。同时,要确保代码生成任务在编译之前运行。

3.1 添加源码目录

将 jOOQ 生成的目录添加到 main 源码集的 Kotlin 路径。

Kotlin

1
2
3
4
5
6
7
8
sourceSets {
main {
kotlin {
// 将生成的代码输出目录添加到 Source Set
srcDir("build/generated-src/jooq/main")
}
}
}

3.2 任务依赖

确保在 Kotlin 编译 (KotlinCompile) 和 Quarkus 构建 (quarkusBuild) 之前,强制运行 jOOQ 代码生成任务(默认任务名为 jooqCodegenjooqCodegenMain)。

Kotlin

1
2
3
4
5
6
7
8
9
tasks.withType<KotlinCompile>().configureEach {
// 确保编译前 jOOQ 代码已生成
dependsOn(tasks.named("jooqCodegen"))
}

tasks.named("quarkusBuild") {
// 确保构建前 jOOQ 代码已生成
dependsOn(tasks.named("jooqCodegen"))
}

4. 运行代码生成

配置完成后,您可以通过运行 Gradle 任务来执行代码生成:

Bash

1
2
3
./gradlew jooqCodegen
# 或
./gradlew jooqCodegenMain

执行成功后,生成的 Kotlin 代码(包括 Records, POJOs, DAOs, Tables 等)将位于 build/generated-src/jooq/main 目录下。

参考

JOOQ - Running the code generator with Gradle


使用 Gradle Kotlin DSL 配置 jOOQ 代码生成(Quarkus & Kotlin)
http://example.com/2025/10/16/Gradle Kotlin 配置 JOOQ 代码生成/
作者
Holy
发布于
2025年10月16日
许可协议