将任务添加到sbt 13.x build.sbt

时间:2015-02-14 02:08:12

标签: scala sbt

我已将此添加到build.sbt

libraryDependencies += "com.typesafe.slick" %% "slick-codegen" % "2.1.0"

lazy val slickGenerate = TaskKey[Seq[File]]("slick code generation")

slickGenerate <<= slickGenerateTask 

lazy val slickGenerateTask = {
    (sourceManaged in Compile, dependencyClasspath in Compile, runner in Compile, streams) map { (dir, cp, r, s) =>
      val dbName = "dbname"
      val userName = "user"
      val password = "password"
      val url = s"jdbc:mysql://server:port/$dbName" 
      val jdbcDriver = "com.mysql.jdbc.Driver"
      val slickDriver = "scala.slick.driver.MySQLDriver"
      val targetPackageName = "models"
      val outputDir = (dir / dbName).getPath // place generated files in sbt's managed sources folder
      val fname = outputDir + s"/$targetPackageName/Tables.scala"
      println(s"\nauto-generating slick source for database schema at $url...")
      println(s"output source file file: file://$fname\n")
      r.run("scala.slick.codegen.SourceCodeGenerator", cp.files, Array(slickDriver, jdbcDriver, url, outputDir, targetPackageName, userName, password), s.log)
      Seq(file(fname))
    }
}

任务的代码本身并不令人兴奋。它只需要创建一个自动生成的scala源文件。问题是,sbt开始正常,但这个新任务显然是被sbt识别,无法在sbt提示符下运行。对于任务定义的:=语法,我也很幸运。现有文档只是令人困惑。

如何在sbt提示符中提供此新任务?

1 个答案:

答案 0 :(得分:7)

这有效

libraryDependencies += "com.typesafe.slick" %% "slick-codegen" % "2.1.0"

lazy val slickGenerate = taskKey[Seq[File]]("slick code generation")

slickGenerate := {
  val dbName = "dbname"
  val userName = "user"
  val password = "password"
  val url = s"jdbc:mysql://server:port/$dbName"
  val jdbcDriver = "com.mysql.jdbc.Driver"
  val slickDriver = "scala.slick.driver.MySQLDriver"
  val targetPackageName = "models"
  val outputDir = ((sourceManaged in Compile).value / dbName).getPath // place generated files in sbt's managed sources folder
  val fname = outputDir + s"/$targetPackageName/Tables.scala"
  println(s"\nauto-generating slick source for database schema at $url...")
  println(s"output source file file: file://$fname\n")
  (runner in Compile).value.run("scala.slick.codegen.SourceCodeGenerator", (dependencyClasspath in Compile).value.files, Array(slickDriver, jdbcDriver, url, outputDir, targetPackageName, userName, password), streams.value.log)
  Seq(file(fname))
}

sbt 0.13.x中,您不需要所有blabla map sameblabla样板。只需按(runner in Compile).value访问值 - 宏将为您完成所有其他操作。

> slickGenerate
[info] Updating {file:/Users/user/slick/}slick...
[info] Resolving org.fusesource.jansi#jansi;1.4 ...
[info] Done updating.

auto-generating slick source for database schema at jdbc:mysql://server:port/dbname...
output source file file: file:///Users/user/slick/target/scala-2.10/src_managed/main/dbname/models/Tables.scala

> help slickGenerate
slick code generation

谈论<<= - 您的TaskKey不正确,请参阅定义:

def apply[T](label : scala.Predef.String, description : scala.Predef.String // not description

因此,旧定义<<=使用&#34;生成光滑的代码&#34;作为标签,而新的:=使用代码给定的命令名称(新样式),所以它使用你的&#34;生成光滑的代码&#34;作为一个文件。这看起来很奇怪且不一致,但这是事实,而且它的部分原因是向后兼容性。

所以,正确的旧式版本是:

import sbt.Keys._

lazy val slickGenerate = TaskKey[Seq[File]]("slick-generate", "generate slick code")

slickGenerate <<= slickGenerateTask

def slickGenerateTask =
  (sourceManaged in Compile, dependencyClasspath in Compile, runner in Compile, streams) map { (dir, cp, r, s) =>
    ...
  }

它的工作方式与之前相同。请注意,您必须使用&#34; slickGenerate&#34;,而不是&#34; slick-generate&#34;,最后一个不适用于&#34; help&#34;命令。

顺便说一句,您现在正在使用Bare build definition - 您可能希望切换到Multi-project .sbt definition,因为它是sbt docs see also推荐的。