我想将代码生成更改为自定义代码生成,只是因为找不到类而失败. 看起来我需要将该模块作为依赖项添加到该任务中,但我不知道如何在Gradle中做到这一点.

我的Gradle任务如下所示.

val jooqGenerateTask = task("jooqGenerate") {
    doLast {
        val initDir = rootProject.projectDir.absolutePath + "/docker/postgres/"
        val container = KPostgreSQLContainer("postgres:13.12")
            .withDatabaseName("jooq")
            .withFileSystemBind(initDir, "/docker-entrypoint-initdb.d")
            .withTmpFs(
                mapOf(
                    "/var/lib/postgresql/data" to "rw"
                )
            )
        container.start()

        val config = Configuration()
            .withJdbc(
                Jdbc()
                    .withDriver("org.postgresql.Driver")
                    .withUrl(container.jdbcUrl)
                    .withUser(container.username)
                    .withPassword(container.password)
            )
            .withGenerator(
                Generator()
                    // Cannot find this class
                    .withName("my.custom.GeneratorKt")
                    .withDatabase(
                        Database()
                            .withName("org.jooq.meta.postgres.PostgresDatabase")
                            .withIncludes(".*")
                            .withExcludes("schema_version")
                            .withInputSchema("public")
                            .withIncludeTables(true)
                            .withIncludeRoutines(false)
                            .withIncludePackages(false)
                            .withIncludeUDTs(false)
                            .withIncludeSequences(false)
                            .withForcedTypes(
                                ForcedType()
                                    .withUserType(JsonObject::class.java.name)
                                    .withConverter(JSONBToJsonObjectConverter::class.java.name)
                                    .withIncludeTypes("jsonb")
                            )
                    )
                    .withGenerate(
                        Generate()
                            .withDeprecated(false)
                            .withRecords(true)
                            .withInterfaces(false)
                            .withFluentSetters(true)
                            .withPojos(false)
                            .withDaos(true)
                    )
                    .withTarget(
                        JooqTarget()
                            .withPackageName("my.jooq")
                            .withDirectory("$projectDir/src/main/java")
                    )
                    .withStrategy(
                        Strategy()
                            .withName("io.github.jklingsporn.vertx.jooq.generate.VertxGeneratorStrategy")
                    )
            )
        GenerationTool.generate(config)
        container.stop()
    }
}

这抛出了一个例外,因为.

Caused by: java.lang.ClassNotFoundException: my.custom.GeneratorKt
        at org.gradle.internal.classloader.VisitableURLClassLoader$InstrumentingVisitableURLClassLoader.findClass(VisitableURLClassLoader.java:186)
        at org.jooq.meta.ClassUtils.loadClass(ClassUtils.java:55)
        at org.jooq.codegen.GenerationTool.loadClass(GenerationTool.java:1224)
        at org.jooq.codegen.GenerationTool.run0(GenerationTool.java:399)
        at org.jooq.codegen.GenerationTool.run(GenerationTool.java:246)
        at org.jooq.codegen.GenerationTool.generate(GenerationTool.java:241)
        at Build_gradle$jooqGenerateTask$1$2.execute(build.gradle.kts:305)
        at Build_gradle$jooqGenerateTask$1$2.execute(build.gradle.kts:234)
        at org.gradle.api.internal.AbstractTask$TaskActionWrapper.execute(AbstractTask.java:835)
        at org.gradle.api.internal.AbstractTask$TaskActionWrapper.execute(AbstractTask.java:808)
        at org.gradle.api.internal.tasks.execution.TaskExecution$3.run(TaskExecution.java:248)
        ... 114 more

推荐答案

您必须将包含GeneratorKt类的任何项目添加到buildscript { }依赖项中,并确保在生成jOOQ代码之前构建它,另请参阅:

Kotlin相关问答推荐

Kotlin协程挂起继续线程

Kotlin -从列表中获取特定过滤器的唯一列表值

Scala性状线性化等价于Kotlin

用Quarkus和Reactor重写异步过滤器中的数据流

如何将消费者放入 Kotlin 的 map 中?

mutableStateOf 和 mutableStateListOf 有什么区别?

将一个列表元素分组为多个组(indexBy)

Kotlin 列表扩展功能

如何在 Compose 中创建可重用的修饰符?

为 Gradle 子项目配置 Kotlin 扩展

如何从 kotlin 函数中 Select 正确的枚举值

TestContainers PostgreSQLContainer 与 Kotlin 单元测试:Not enough information to infer type variable SELF

在 SplashActivity 中显示的 Firebase 应用内消息.如何在 MainActivity 中显示它?

JobIntentService 被销毁,当应用程序被销毁时

模拟异常 - 没有找到答案

如何从 Firestore 查询中排除元素?

在 Kotlin 函数上使用 Mokito anyObject() 时,指定为非 null 的参数为 null

IllegalStateException:function = , count = 3, index = 3

在 Kotlin 中返回函数有不那么丑陋的方法吗?

如何在不绑定ViewModel(MVVM)中的UI的情况下使用android导航?