我有这个 gradle 脚本用于自动生成 JOOQ 类。
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath(
'org.jooq:jooq:3.18.4',
'org.jooq:jooq-codegen:3.18.4',
'org.jooq:jooq-meta:3.18.4',
'org.postgresql:postgresql:42.6.0'
)
}
}
apply plugin: 'java'
apply plugin: 'nu.studer.jooq'
import org.jooq.codegen.GenerationTool
import org.jooq.meta.jaxb.*
task generateJooq {
def configuration = new Configuration()
.withJdbc(
new Jdbc()
.withDriver('org.postgresql.Driver')
.withUrl('jdbc:postgresql://localhost:5432/test')
.withUser('postgres')
.withPassword('password'))
.withGenerator(
new Generator()
.withDatabase(new Database().withExcludes("databasechangeloglock|databasechangelog").withInputSchema("public"))
.withGenerate(
new Generate()
.withRoutines(false)
.withPojos(true)
.withDaos(true)
).withTarget(
new Target()
.withPackageName('com.example.model.jooq')
.withDirectory(projectDir.toString() + '/src/main/java')
).withStrategy(
new Strategy()
.withMatchers(
new Matchers()
.withTables(
new MatchersTableType()
.withTableIdentifier(
new MatcherRule()
.withExpression('$0_Entity')
.withTransform(MatcherTransformType.UPPER)
).withPojoClass(new MatcherRule().withExpression('$0_Entity').withTransform(MatcherTransformType.PASCAL))
)
)
)
)
doLast {
GenerationTool.generate(configuration)
}
}
但是当我对表进行任何更改(例如,进行迁移)时,我需要更改我的 pojo、记录等类。当我运行任务
generateJooq
时,它会删除所有类并重新创建它们,问题是所有 lombok 注释(数据、构建器)都消失了。
我希望在运行任务时,JOOQ 将仅重新创建更改的表,或者如果不重新创建,并且仅在现有类中添加新字段,那就更好了。
jOOQ的代码生成器不会触及内容未更改的文件,以实现增量编译。但这显然只有在您不接触生成的代码时才有效(包括如果您重写代码生成器以实现不同的行为,这是可以的)。
如果您在 jOOQ 生成代码之后更改生成的代码,jOOQ 无法知道您正在这样做。 jOOQ 也无法知道现有文件的更改是源自您的手动干预,还是源自以前的 jOOQ 版本,或者其他什么。新的输出与现有内容完全不同,因此 jOOQ 必须覆盖该文件。 现在,由于上述原因,手动更改生成的代码根本就不是一个好主意。如果您
必须更改生成的代码,您应该:
使用JavaGenerator
JavaGenerator
并覆盖其一些行为。这样,您可以告诉 Gradle 本身不需要重新生成代码(不过,如果您进行了架构更改,jOOQ 仍然会重新生成所有文件,而不仅仅是“已更改”的文件) ,因为您在 jOOQ 之后应用更改)。