我有这个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时,它会删除所有类并重新创建它们的类和problem is that all the lombok annotations (data, builder) disappear. 我希望在运行任务时,JOOQ将只重新创建更改的表,如果不重新创建甚至更好,并且只在现有类中添加新的字段.

推荐答案

谢谢Lukas Eder,但我没有弄明白:)写下了这篇文章

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

import java.nio.charset.StandardCharsets
import java.nio.file.Files
import java.nio.file.Paths

task generateJooq {
    var database = new Database().withExcludes("databasechangeloglock|databasechangelog").withInputSchema("public")
    def configuration = new Configuration()
            .withJdbc(
                    new Jdbc()
                            .withDriver('org.postgresql.Driver')
                            .withUrl('jdbc:postgresql://localhost:5432/test')
                            .withUser('postgres')
                            .withPassword('bigdata8'))
            .withGenerator(
                    new Generator()
                            .withDatabase(database)
                            .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)

        Files.walk(Paths.get(srcDir() + 'com/example/model/jooq/tables/pojos'))
                .filter(path -> Files.isRegularFile(path))
                .forEach(path -> {
                    List<String> list = Files.readAllLines(path)

                    boolean importAdded = false
                    boolean annotationsAdded = false
                    for (int i = 1; i < list.size(); i++) {
                        if (list.get(i - 1).startsWith("import") && list.get(i) == "") {
                            list.add(i + 1, "import lombok.Builder;")
                            i += 2
                            importAdded = true
                        }
                        if (importAdded) {
                            if (list.get(i).startsWith("public class")) {
                                list.add(i - 1, "@Builder")
                                annotationsAdded = true
                                break
                            }
                        }
                    }

                    if (!importAdded || !annotationsAdded) {
                        throw new RuntimeException()
                    }

                    Files.write(path, list, StandardCharsets.UTF_8)
                })
    }
}

String srcDir() {
    return projectDir.path + "/src/main/java/"
}

Java相关问答推荐

当列顺序更改时,Table View列列表的Change. wasPermanted()总是返回假

为什么我的画布没有显示在PFA应用程序中?

如何使用解析器组合子解析Java数组类型签名?

为什么一个java函数会返回一个作为参数传递给它的对象?

使用java访问具体子类特定方法的最佳方法是什么?

只需最少的代码更改即可将版本号标记添加到日志(log)

如何找到MongoDB文档并进行本地化?

Spring Boot Maven包

放气总是压缩整个街区吗?

如何获取Instant#of EpochSecond(?)的最大值

与不同顺序的组进行匹配,不重复组但分开

使用存储在字符串变量中的路径目录打开.pdf文件

为什么Collectors.toList()不能保证易变性

try 使用类来包含JSON响应

Spring Validator批注不起作用

使用for循环时出现堆栈溢出错误,但如果使用if块执行相同的操作,则不会产生错误

为什么这种递归会有这样的行为?

不能在 map 上移除折线

Java 21保护模式的穷尽性

ANTLR 接受特殊字符,例如 .标识符或表达式中的(点)和 ,(逗号)