新的 jooq/gradle 配置不生成任何 jooq 类

Posted

技术标签:

【中文标题】新的 jooq/gradle 配置不生成任何 jooq 类【英文标题】:new jooq/gradle configuration not generating any jooq classes 【发布时间】:2018-12-27 03:17:53 【问题描述】:

我得到了一个使用 gradle (v2.1.0) 和 jooq (v3.8.1) 生成类文件的 Ratpack 应用程序。

这是我的 build.gradle 文件:

buildscript 
  repositories 
    jcenter()
  
  dependencies 
    classpath "io.ratpack:ratpack-gradle:1.5.4"
    classpath "com.github.jengelman.gradle.plugins:shadow:1.2.3"
    classpath "mysql:mysql-connector-java:5.1.34"
    classpath 'org.jooq:jooq-codegen:3.8.1'
    classpath 'com.h2database:h2:1.4.186'
  


plugins 
    id "com.github.michaelruocco.embedded-mysql-plugin" version "2.1.7"


apply plugin: "io.ratpack.ratpack-java"
apply plugin: "com.github.johnrengelman.shadow"
apply plugin: "idea"

// db dump: mysqldump -P 3306 -h 127.0.0.1 -u embedded_user -ppassword --all-databases
def RDS_HOSTNAME = "localhost"
def RDS_PORT = 3306
def RDS_DB_NAME = "test"
def RDS_USERNAME = "embedded_user"
def RDS_PASSWORD = "password"

run 
    environment "RDS_HOSTNAME", RDS_HOSTNAME
    environment "RDS_PORT", RDS_PORT
    environment "RDS_DB_NAME", RDS_DB_NAME
    environment "RDS_USERNAME", RDS_USERNAME
    environment "RDS_PASSWORD", RDS_PASSWORD


repositories 
  jcenter()


ext 
    ratpackPac4jVersion="2.0.0"
    ratpackVersion="1.5.1"
    pac4jVersion="2.1.0"


embeddedMysql 
    url = 'jdbc:mysql://' + RDS_HOSTNAME + ':' + RDS_PORT + "/" + RDS_DB_NAME
    username = RDS_USERNAME
    password = RDS_PASSWORD
    version = 'v5_7_latest'


import org.jooq.util.jaxb.*
import org.jooq.util.*

task jooqCodegen 
  doLast 
    String init = "$projectDir/src/main/resources/init.sql".replaceAll('\\\\', '/') 
    Configuration configuration = new Configuration()
      .withJdbc(new Jdbc()
      .withDriver("org.h2.Driver") 
      .withUrl("jdbc:h2:mem:todo;INIT=RUNSCRIPT FROM '$init'") 
    )
      .withGenerator(new Generator()
        .withDatabase(new Database()
          .withName("org.jooq.util.h2.H2Database")
          .withIncludes(".*")
          .withExcludes("")
          .withInputSchema("PUBLIC")
        )
      .withTarget(new Target()
        .withDirectory("$projectDir/src/main/auto_generated") 
        .withPackageName("jooq"))) 
    GenerationTool.generate(configuration)
  


run.dependsOn startEmbeddedMysql

dependencies 
    runtime "org.slf4j:slf4j-simple:1.7.25"

    compile "org.pac4j:ratpack-pac4j:$ratpackPac4jVersion"
    compile "io.ratpack:ratpack-groovy:$ratpackVersion"
    compile "io.ratpack:ratpack-test:$ratpackVersion"
    compile "org.pac4j:pac4j-core:$pac4jVersion"
    compile "org.pac4j:pac4j-oauth:$pac4jVersion"
    compile "org.pac4j:pac4j-openid:$pac4jVersion"
    compile "org.pac4j:pac4j-http:$pac4jVersion"
    compile "org.pac4j:pac4j-gae:$pac4jVersion"
    compile "org.pac4j:pac4j-oidc:$pac4jVersion"
    compile "org.pac4j:pac4j-jwt:$pac4jVersion"
    compile ratpack.dependency('hikari')
    compile "ch.qos.logback:logback-classic:1.0.13"
    compile group: 'io.ratpack', name: 'ratpack-thymeleaf', version: '1.4.0-rc-3'
    compile "mysql:mysql-connector-java:5.1.34"
    compile 'org.jooq:jooq:3.8.1'
    compile group: 'org.hibernate', name: 'hibernate-core', version: '5.3.2.Final'



mainClassName = "xyz.mealsahead.Main"

我开始使用 jooq API 并意识到我使用的是旧版本的 jooq,所以想切换到较新的 API。

我尝试的第一件事是将两个 jooq 引用从 3.8.1 更改为 3.11.2。这引起了问题,因为 jooq 版本似乎更改了 API,因此 jooq 配置不再起作用:

> startup failed:
  build file build.gradle': 72: unable to resolve class Target 
   @ line 72, column 19.
           .withTarget(new Target()
                       ^

  build file build.gradle': 66: unable to resolve class Database 
   @ line 66, column 23.
             .withDatabase(new Database()
                           ^

  build file build.gradle': 65: unable to resolve class Generator 
   @ line 65, column 22.
           .withGenerator(new Generator()
                          ^

  etc.

jooq 有一个 gradle 设置示例,但我不明白它是如何工作的:https://www.jooq.org/doc/3.11/manual/code-generation/codegen-gradle/。奇怪的是,该示例中的 buildscript 块不在顶部,从我在终端和在线看到的情况来看,这是不允许的:

build.gradle': 24: only buildscript  and other plugins  script blocks are allowed before plugins  blocks, no other statements are allowed

所以我不知道该示例将如何工作,除非它适用于与我所拥有的不同版本的 gradle(之前或之后)。

查看 jooq-gradle 插件 (https://github.com/etiennestuder/gradle-jooq-plugin) 的文档,其中包含设置 gradle 脚本的非常清晰的步骤,因此我将 build.gradle 文件修改为如下所示:

buildscript 
  repositories 
    jcenter()
  
  dependencies 
    classpath "io.ratpack:ratpack-gradle:1.5.4"
    classpath "com.github.jengelman.gradle.plugins:shadow:1.2.3"
    classpath "mysql:mysql-connector-java:5.1.34"
    classpath 'com.h2database:h2:1.4.186'
    classpath 'nu.studer:gradle-jooq-plugin:3.0.1'
  


plugins 
    id "com.github.michaelruocco.embedded-mysql-plugin" version "2.1.7"


apply plugin: 'nu.studer.jooq'
apply plugin: "io.ratpack.ratpack-java"
apply plugin: "com.github.johnrengelman.shadow"
apply plugin: "idea"

// db dump: mysqldump -P 3306 -h 127.0.0.1 -u embedded_user -ppassword --all-databases
def RDS_HOSTNAME = "localhost"
def RDS_PORT = 3306
def RDS_DB_NAME = "test"
def RDS_USERNAME = "embedded_user"
def RDS_PASSWORD = "password"

run 
    environment "RDS_HOSTNAME", RDS_HOSTNAME
    environment "RDS_PORT", RDS_PORT
    environment "RDS_DB_NAME", RDS_DB_NAME
    environment "RDS_USERNAME", RDS_USERNAME
    environment "RDS_PASSWORD", RDS_PASSWORD


repositories 
  jcenter()


ext 
    ratpackPac4jVersion="2.0.0"
    ratpackVersion="1.5.1"
    pac4jVersion="2.1.0"


embeddedMysql 
    url = 'jdbc:mysql://' + RDS_HOSTNAME + ':' + RDS_PORT + "/" + RDS_DB_NAME
    username = RDS_USERNAME
    password = RDS_PASSWORD
    version = 'v5_7_latest'


dependencies 
     jooqRuntime 'com.h2database:h2:1.4.193'


jooq 
    String init = "$projectDir/src/main/resources/init.sql".replaceAll('\\\\', '/') 

  version = '3.11.2'
  edition = 'OSS'
  sample(sourceSets.main) 
    jdbc 
      driver = "org.h2.Driver" //'org.postgresql.Driver'
      url = "jdbc:h2:mem:todo;INIT=RUNSCRIPT FROM '$init'" //'jdbc:postgresql://localhost:5432/sample'
                  user = 'sa'
            password = ''
    
    generator 
      name = 'org.jooq.codegen.DefaultGenerator'
      database 
        name = 'org.jooq.meta.h2.H2Database'
        includes = '.*'
        excludes = ''
        inputSchema = 'public'
      
      generate 
        relations = true
        deprecated = false
        records = true
        immutablePojos = true
        fluentSetters = true
      
      target 
        directory = "$projectDir/src/main/auto_generated"
        packageName = 'jooq'
      
    
  


run.dependsOn startEmbeddedMysql

dependencies 
    runtime "org.slf4j:slf4j-simple:1.7.25"

    compile "org.pac4j:ratpack-pac4j:$ratpackPac4jVersion"
    compile "io.ratpack:ratpack-groovy:$ratpackVersion"
    compile "io.ratpack:ratpack-test:$ratpackVersion"
    compile "org.pac4j:pac4j-core:$pac4jVersion"
    compile "org.pac4j:pac4j-oauth:$pac4jVersion"
    compile "org.pac4j:pac4j-openid:$pac4jVersion"
    compile "org.pac4j:pac4j-http:$pac4jVersion"
    compile "org.pac4j:pac4j-gae:$pac4jVersion"
    compile "org.pac4j:pac4j-oidc:$pac4jVersion"
    compile "org.pac4j:pac4j-jwt:$pac4jVersion"
    compile ratpack.dependency('hikari')
    compile "ch.qos.logback:logback-classic:1.0.13"
    compile group: 'io.ratpack', name: 'ratpack-thymeleaf', version: '1.4.0-rc-3'
    compile "mysql:mysql-connector-java:5.1.34"
    compile 'org.jooq:jooq'
    compile group: 'org.hibernate', name: 'hibernate-core', version: '5.3.2.Final'



mainClassName = "xyz.mealsahead.Main"

运行“gradle run”工作正常,所以我假设它正在下载新版本的 jooq,但现在我没有看到任何代码生成。我看到以下消息:

$ gradle generateSampleJooqSchemaSource 
:generateSampleJooqSchemaSource UP-TO-DATE

谁能告诉我:

我正在为此使用适当的 gradle-jooq 插件吗? 是否错误地配置了先前版本的新生成器语法?

【问题讨论】:

【参考方案1】:

关于您手动设置 jOOQ 代码生成器

确实,由于 jOOQ 3.12 即将模块化,一些拆分包必须重命名,即 org.jooq.util 包。在您的手动设置中,您需要从以下位置更改导入:

import org.jooq.util.jaxb.*
import org.jooq.util.*

import org.jooq.codegen.*
import org.jooq.meta.jaxb.*
import org.jooq.meta.*

关于您使用第三方 Gradle 插件

请注意,架构名称区分大小写,因此可能有助于替换它:

inputSchema = 'public'

由此

inputSchema = 'PUBLIC'

你之前已经正确了:.withInputSchema("PUBLIC")

【讨论】:

是外壳!太感谢了。我现在需要阅读该功能以便我能理解它,但我很欣赏这个简单的修复。实际上,我从示例中复制了该粘贴并注意到了它,但我认为这并不重要,因为我是从示例中提取的。 @voodoogiant:“实际上是从示例中复制粘贴并注意到它,但我认为这并不重要,因为我从示例中提取了它”,是的,PostgreSQL 报告不区分大小写的名称为小写,其他数据库将它们报告为大写:-/

以上是关于新的 jooq/gradle 配置不生成任何 jooq 类的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 jOOQ gradle 插件将 postgres 中的 bigint[] 字段转换为类字段

使用新的苹果面板生成推送通知 ssl 证书和配置以进行开发?

docker提交配置好的容器生成一个新的镜像

汇编指令学习(JMPJEJSJP,JO,JB)

使用SQS接收SES消息

SuperObject生成示例