SBT如何使用DataNucleus从模型类生成元模型类?

时间:2015-05-22 16:31:37

标签: scala sbt datanucleus metamodel

如何从持久性模型类(如Client,Product,Invoice)生成元模型类(如QClient,QProduct,QInvoice),以便可以使用JDOQL类型安全查询?

特别是,我对生成元模型类感兴趣,并且还通过SBT和使用带有JDO注释的DataNucleus对持久化类运行字节码增强。

这个问题与
有关 How can I run DataNucleus Bytecode Enhancer from SBT?

1 个答案:

答案 0 :(得分:1)

简而言之,您需要在build.sbt中定义执行元模型和字节码增强生成的函数,如下所示:

def generateQueryEntities(sourcepath: File,
                          sources: Seq[File],
                          generated: File,
                          classes: File,
                          classpath: Seq[File],
                          javacOptions: Seq[String]): Seq[File] = {
  IO.createDirectory(generated)
  javaRunner(
    javaTool = Option("javac"),
    args =
      javacOptions ++
        (if(debug) Seq("-verbose") else Seq.empty[String]) ++
        Seq(
          "-sourcepath", sourcepath.absolutePath,
          "-s",  generated.absolutePath,
          "-d",  classes.absolutePath) ++
        sources.map(p => p.absolutePath),
    classpath = Option(classpath),
    cwd = Option(classes)
  )
  classes.listFiles.filter(f => f.isFile && (f.ext == "class"))
}

def enhanceSchema(classes: File, classpath: Seq[File]): Seq[File] = {
  javaRunner(
    mainClass = Option("javax.jdo.Enhancer"),
    args =
      (if(debug) Seq("-v") else Seq.empty[String]) ++
      Seq(
        "-pu", "code-generation",
        "-d",  classes.absolutePath),
    classpath = Option(classpath),
    cwd = Option(classes)
  )
  classes.listFiles.filter(f => f.isFile && (f.ext == "class"))
}

下一步是为这些功能定义自定义任务。我们只需要genjdoql,如下所示,因为字节码增强可以通过子任务manipulateBytecode连接。请注意,SBT 0.13.8或更高版本是必需的。

val genjdoql      = TaskKey[Seq[File]]("genjdoql",    "DataNucleus JDOQL Entities")

然后,您需要在项目或模块的构建中连接这些函数,如下所示:

lazy val model =
  project.in(file("model"))
    .settings(publishSettings:_*)
    .settings(librarySettings:_*)
    .settings(paranoidOptions:_*)
    .settings(otestFramework: _*)
    .settings(deps_resolvers:_*)
    //XXX .settings(deps_langtools:_*)
    .settings(deps_tagging:_*)
    .settings(deps_stream:_*)
    .settings(deps_database:_*)
    .settings(managedSources:_*)
    .settings(
      Seq(
        // generate JDOQL Entities
        genjdoql in Compile := {
          generateQueryEntities(
            sourcepath = (javaSource in Compile).value,
            sources = (unmanagedSources in Compile).value,
            generated = baseDirectory.value / "target" / scalav(scalaVersion.value) / "src_managed" / "main"  / "java",
            classes = (classDirectory in Compile).value,
            classpath = (managedClasspath in Compile).value.files,
            javacOptions = javacOpts :+ "-AqueryMode=PROPERTY"
          )},
        sourceGenerators in Compile <+= genjdoql in Compile,
        // prevent javac from running annotation processors
        javacOptions ++= Seq( "-proc:none" ),
        // perform bytecode enhancement
        manipulateBytecode in Compile := {
          val previous = (manipulateBytecode in Compile).value
          enhanceSchema(
            classes = (classDirectory in Compile).value,
            classpath =
              (managedClasspath in Compile).value.files ++
                (unmanagedResourceDirectories in Compile).value :+
                (classDirectory in Compile).value)
          previous
        }
      ):_*)
    .dependsOn(util)

如需完整示例,请查看
https://github.com/frgomes/poc-scala-datanucleus