如何在gradle插件中设置jOOQ审计列?

2024-05-01

尝试编写一个小型演示应用程序来检查jOOQ的审核专栏 https://www.jooq.org/doc/3.17/manual/code-generation/codegen-advanced/codegen-config-database/codegen-database-forced-types/codegen-database-forced-types-audit/正在做我们期望他们做的事情。

只要有一个last_modified时间戳和一个last_modifier整数ID

根据上面链接页面上的信息,我希望它看起来像这样:

构建.gradle.kts(jooq-codegen)

import org.jooq.meta.jaxb.ForcedType
import org.jooq.meta.jaxb.Logging

plugins {
    id("nu.studer.jooq") version "7.1.1"
}

val jooqGroup by extra{"org.jooq.trial-java-8"}
val jooqVersion by extra{"3.17.3"}

dependencies {
    api("$jooqGroup:jooq:$jooqVersion")
    implementation("$jooqGroup:jooq-codegen:$jooqVersion")
    implementation("$jooqGroup:jooq-meta:$jooqVersion")
    jooqGenerator("org.postgresql:postgresql:42.4.2")
    runtimeOnly("org.postgresql:postgresql:42.4.2")
}

jooq {
    version.set(jooqVersion)
    edition.set(nu.studer.gradle.jooq.JooqEdition.TRIAL_JAVA_8)

    configurations {
        create("poc") {
            generateSchemaSourceOnCompilation.set(false)

            jooqConfiguration.apply {
                logging = Logging.WARN
                jdbc.apply {
                    driver = "org.postgresql.Driver"
                    url = "jdbc:postgresql://localhost:5432/mydemo"
                    user = "foo"
                    password = "bar"
                }
                generator.apply {
                    name = "org.jooq.codegen.DefaultGenerator"
                    database.apply {
                        name = "org.jooq.meta.postgres.PostgresDatabase"
                        inputSchema = "public"
                        forcedTypes.addAll(listOf(
                            ForcedType().apply {
                                name = "varchar"
                                includeExpression = ".*"
                                includeTypes = "JSONB?"
                            },
                            ForcedType().apply {
                                name = "varchar"
                                includeExpression = ".*"
                                includeTypes = "INET"
                            },
                            ForcedType().apply {
                                auditUpdateUser = true
                                name = "int"
                                includeExpression = "last_modifier"
                            },
                            ForcedType().apply {
                                auditUpdateTimestamp = true
                                name = "timestamptz"
                                includeExpression = "last_modified"
                            }
                        ))
                    }
                    generate.apply {
                        isPojos = false
                        isDaos = false
                        isRecords = true
                        isDeprecated = false
                    }
                    target.apply {
                        packageName = "my.app.jooq"
                        directory = "src/main/java"
                    }
                    strategy.name = "org.jooq.codegen.DefaultGeneratorStrategy"
                }
            }
        }
    }
}

除了两者auditUpdateUser and auditUpdateTimestamp are an

未解决的参考

我怀疑原因是审计列似乎只能从 jOOQ 开始使用3.17 and Studer最新插件版本 https://github.com/etiennestuder/gradle-jooq-plugin/releases落后于仅支持3.16.4.

鉴于此 - 并假设这确实是正确的 - 我如何最好地启用审计列?

我当然可以尝试子类化ForcedType看看我是否可以在那里添加对它的支持,直到插件正式支持它。

但是有没有一种更简单的方法来设置审计列而无需这样做呢?

UPDATE

所以问题是org.jooq.meta.jaxb.ForcedType顶部导入的是版本3.16.4代替3.17.3.

正如卢卡斯在下面的回答中提到的那样,我们可以通过添加来强制执行正确的版本

buildscript {
    configurations["classpath"].resolutionStrategy.eachDependency {
        if (requested.group == "org.jooq") {
            useVersion("3.17.3")
        }
    }
}

到脚本的开头。

然而,这似乎引入了 jOOQ 的开源版本,它需要 java 17(并且目标项目仍在 java 11 上)。

java.lang.UnsupportedClassVersionError: org/jooq/meta/jaxb/Configuration 已由更新版本的 Java 运行时(类文件版本 61.0)编译,此版本的 Java 运行时仅识别最高版本 55.0 的类文件

我认为我们不需要更新到 java 17 来使用与 java-8 兼容的依赖项。

https://github.com/etiennestuder/gradle-jooq-plugin/issues/183 https://github.com/etiennestuder/gradle-jooq-plugin/issues/183建议类似的东西

dependencies {
    api("$jooqGroup:jooq:$jooqVersion")
    implementation("$jooqGroup:jooq-codegen:$jooqVersion")
    implementation("$jooqGroup:jooq-meta:$jooqVersion")
    jooqGenerator("org.postgresql:postgresql:42.4.2")
    runtimeOnly("org.postgresql:postgresql:42.4.2")

    modules {
        module("org.jooq:jooq") {
            replacedBy("$jooqGroup:jooq", "nu.studer.jooq bugfix #183") // https://github.com/etiennestuder/gradle-jooq-plugin/issues/183
        }
    }
}

可能有帮助,但到目前为止还没有任何明显的效果。

UPDATE

为了再现性,build.gradle.kts (jooq-codegen)项目是一个小型 Spring 应用程序的一部分,该应用程序有两个模块:jooq-codegen and server。这是另外两个 gradle 文件:

构建.gradle.kts(app)

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
    id("org.springframework.boot") version "2.7.3"
    id("io.spring.dependency-management") version "1.0.13.RELEASE"
    kotlin("jvm") version "1.6.21"
    kotlin("plugin.spring") version "1.6.21"
}

dependencies {
    implementation("org.springframework.boot:spring-boot-starter-web")
}

springBoot {
    mainClass.value("my.app.server.JooqAuditLoggingApplication")
}

allprojects {
    repositories {
        mavenCentral()
        mavenLocal{
            content{
                includeGroup("org.jooq.trial-java-8")
            }
        }
    }
    group = "my.app"
    version = "0.0.1-SNAPSHOT"

    tasks.withType<JavaCompile>{
        options.encoding = "UTF-8"
    }
}

subprojects {
    apply(plugin ="io.spring.dependency-management")
    apply(plugin ="java-library")

    java.sourceCompatibility = JavaVersion.VERSION_11
    java.targetCompatibility = JavaVersion.VERSION_11
}

project("jooq-codegen"){
}

project("server"){
    apply(plugin = "org.jetbrains.kotlin.jvm")
    apply(plugin ="org.jetbrains.kotlin.plugin.spring")

    dependencies {
        implementation("org.jetbrains.kotlin:kotlin-reflect")
        implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
    }

    tasks.withType<KotlinCompile> {
        kotlinOptions {
            freeCompilerArgs = listOf("-Xjsr305=strict")
            jvmTarget = "17"
        }
    }

    tasks.withType<Test> {
        useJUnitPlatform()
    }
}

构建.gradle.kts(server)

import org.flywaydb.gradle.task.FlywayMigrateTask

plugins {
    id("org.springframework.boot") version "2.7.3"
    id("org.flywaydb.flyway") version "9.0.1"
}

dependencies {
    implementation(project(":jooq-codegen"))
    implementation("org.springframework.boot:spring-boot-starter-jooq"){
        exclude("org.jooq","jooq")
    }
    implementation("org.springframework.boot:spring-boot-starter-security")
    implementation("org.springframework.boot:spring-boot-starter-web")
    implementation("org.springframework.boot:spring-boot-starter-thymeleaf")
    testImplementation("org.springframework.boot:spring-boot-starter-test")
    testImplementation("org.springframework.security:spring-security-test")
    implementation("org.flywaydb:flyway-core")
}

flyway {*emphasized text*
    url = "jdbc:postgresql://localhost:5432/mydemo"
    user = "foo"
    password = "bar"
    schemas = arrayOf("public")
    locations = arrayOf("classpath:db/migration")
    ignoreMigrationPatterns = arrayOf("*:missing")
    cleanDisabled = false
}
tasks.withType<FlywayMigrateTask>{
    dependsOn("processResources")
}

根据插件的自述文件,部分“强制执行 jOOQ 配置 XML 模式版本” https://github.com/etiennestuder/gradle-jooq-plugin#enforcing-the-jooq-configuration-xml-schema-version,这应该有效吗?

buildscript {
    configurations["classpath"].resolutionStrategy.eachDependency {
        if (requested.group == "org.jooq-trial-java-8") {
            useVersion("3.17.3")
        }
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在gradle插件中设置jOOQ审计列? 的相关文章

随机推荐