Skip to content

Instantly share code, notes, and snippets.

@chris-martin
Last active December 14, 2015 19:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chris-martin/5140754 to your computer and use it in GitHub Desktop.
Save chris-martin/5140754 to your computer and use it in GitHub Desktop.
import java.io.ByteArrayInputStream
import sbt._
import Keys._
object Build extends sbt.Build {
lazy val jooqProject = Project("jooq", file("."), settings = Seq(
jooqSettings(JooqCodegenConfig(
schemata = Seq("___________"),
includes = """(?i)__________\.(%s)""".format(Seq(
"pkg (foobar | xyzzy) bundle",
"foobar_ ( one | two | three | four_ (a|b) )",
"xyzzt_ ( one | two | three | four_ (a|b) )",
).mkString("|").replaceAll("""\s""", "")),
packageName = "__________.____.____________"
))
))
lazy val jooqCodegenTask = TaskKey[Unit]("jooq-codegen")
lazy val jooqCodegenConfig = SettingKey[JooqCodegenConfig]("jooq-codegen-config")
def jooqSettings(config: JooqCodegenConfig): Seq[Setting[_]] = Seq(
libraryDependencies += Seq(
"com.oracle" % "ojdbc6" % "11.2.0.2.0",
"org.jooq" % "jooq" % "3.0.0-RC1"
),
jooqCodegenConfig := config,
javaSource in Compile <<= (sourceDirectory in Compile)(_/"jooq"),
jooqCodegenTask <<= (streams, baseDirectory, jooqCodegenConfig) map {
(s, bd, jc) => {
if (jc.enable) {
import org.jooq.util.{GenerationTool=>G}
val xml = jc.asXml(bd).buildString(stripComments = false)
s.log.info(xml)
IO.delete(bd/"src"/"main"/"jooq")
G.main(G.load(new ByteArrayInputStream(xml.getBytes("utf-8"))))
}
}
}
)
case class JooqCodegenConfig(schemata: Seq[String] = Seq(), includes: String = "",
packageName: String = "", enable: Boolean = true) {
def asXml(baseDirectory: File) =
<configuration>
<jdbc>
<driver>oracle.jdbc.OracleDriver</driver>
<url>jdbc:oracle:thin:____________________________</url>
<user>_____________</user>
<password>___________</password>
</jdbc>
<generator>
<name>org.jooq.util.DefaultGenerator</name>
<database>
<name>org.jooq.util.oracle.OracleDatabase</name>
<includes>
{includes}
</includes>
<schemata>{
for (schema <- schemata) yield {
<schema>
<inputSchema>{schema}</inputSchema>
</schema>
}
}</schemata>
</database>
<target>
<packageName>{packageName}</packageName>
<directory>{(baseDirectory/"src"/"main"/"jooq").getPath}</directory>
</target>
</generator>
</configuration>
}
}

Demonstration of using SBT to invoke jOOQ-codegen

The "jooq-codegen" task generates sources under "src/main/jooq" (which is added as a Java source directory). I put them here instead of "src/main/java" because the directory must be emptied before generation, so I don't want to chance emptying a directory that might contain other sources.

I check the jOOQ-generated code into version control. This makes it easier to work away from the office where I don't have easy access to the database. It also gives me a better picture of database changes, because they are reflected as commits.

import sbt._
import Keys._
object ProjectBuild extends sbt.Build {
lazy val root = Project("build", file("."), settings = Seq(
libraryDependencies ++= Seq(
"com.oracle" % "ojdbc6" % "11.2.0.2.0",
"org.jooq" % "jooq-codegen" % "3.0.0-RC1"
)
))
}
@lukaseder
Copy link

Fancy giving some support to this user of your plugin? :-)
http://stackoverflow.com/q/18329458/521799

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment