Snippets

Shorn Tolley JOOQ subproject

Created by Shorn Tolley last modified
import groovy.xml.MarkupBuilder
import org.graws.plugin.GrawsPlugin

buildscript {
  repositories {
    mavenCentral()
    jcenter()
  }
  dependencies {
    classpath "org.springframework.boot:spring-boot-gradle-plugin:"+springBootVersion
    // needed for the docker tasks
    classpath "org.graws:graws-plugin:$grawsPluginVersion"
    classpath 'org.jooq:jooq-codegen:3.10.1'
    classpath 'org.postgresql:postgresql:42.0.0'
    
  }
}

plugins{
  id 'java'
}

version = rootProject.version

project.mkdir(file(project.buildDir))

ext {
  
  apiSvcJooqGenDbUrl = System.getProperty(
    "api_svc_jooq_gen_db_url", 
    "jdbc:postgresql://192.168.99.100:5544/postgres")
  assert !apiSvcJooqGenDbUrl.contains("p6spy") : 
    "don't use a p6spy url unless you've added p6spy to the classpath"
  apiSvcJooqGenDbUser = System.getProperty("api_svc_jooq_gen_db_user", "postgres")
  apiSvcJooqGenDbPassword = 
    System.getProperty("api_svc_jooq_gen_db_password", "")
  
  jooqGenSrcDir = file("$buildDir/gen-jooq")
  
}

apply plugin: GrawsPlugin

sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {
  mavenCentral()
  jcenter()
}


dependencies {
  compile 'org.jooq:jooq:3.10.1'
}

compileJava {
  options.compilerArgs << "-Xlint:unchecked" << "-Xlint:deprecation"
}


// your IDE should be set to UTF-8, too
tasks.withType(JavaCompile) {
  options.encoding = "UTF-8"
}

clean.doFirst {
}

/**
 * This can't be part of the standard build process, because then you would
 * need a database in order to compile.
 */
task generateApiSvcJooqSchemaSourceFromLocalDb(){
  dependsOn ":api-svc:database:flywayMigrateLocalDb"
  group = "build"
  description = "generates JOOQ schema mapping code to a temp dir"

  ext{
    jooqCodegenSchema = 'http://www.jooq.org/xsd/jooq-codegen-3.10.0.xsd'
    enumConverters = []
    interfaceMapping = []
  }

  doFirst {
    delete jooqGenSrcDir
  }

  // ripped from
  // http://www.jooq.org/doc/3.6/manual/code-generation/codegen-gradle/
  doLast {
    def xmlWriter = new StringWriter()
    def xmlBuilder =
      new MarkupBuilder(xmlWriter).configuration('xmlns': jooqCodegenSchema){
        jdbc() {
          driver('org.postgresql.Driver')
          url(apiSvcJooqGenDbUrl)
          user(apiSvcJooqGenDbUser)
          password(apiSvcJooqGenDbPassword)
        }
        generator() {
          database() {
            inputSchema("api_svc")
            excludes(".*_audit" + // audit tables
              "|.*trigger"  // triggers (esp. auditing triggers)
            )
            // "version" columns are assumed to relate to optimistic locking
            recordVersionFields("version")

          }
          generate([:]) {
            relations("true")     // for fetchParent/Child etc
            records("true")       // these are the core objects we use
            fluentSetters("true") // makes using streams easier
            deprecated("false")
            pojos("false")
            immutablePojos("false")
            pojosEqualsAndHashCode("true")
          }
          target() {
            packageName('apisvc.jooq.db')
            directory(jooqGenSrcDir)
          }
        }
      }

    def xmlString = xmlWriter.toString()
//     println "xml...\n"+ xmlString

    // Run the code generator
    org.jooq.util.GenerationTool.generate(
      javax.xml.bind.JAXB.unmarshal(
        new StringReader(xmlString),
        org.jooq.util.jaxb.Configuration.class )
    )
  }
}

task overwriteJooqSrcFromLocalDb(type: Copy){
  dependsOn generateApiSvcJooqSchemaSourceFromLocalDb
  description = "copies generate JOOQ code over the top of whatever is in the" +
    " source base, which then has to be committed by the developer"
  group = "build"
  ext.jooqSrcDestinationDir = "src/main/java"
  doFirst {
    delete file("$jooqSrcDestinationDir/apisvc/jooq/db")
  }

  destinationDir = file(jooqSrcDestinationDir)

  from jooqGenSrcDir
}

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.