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

mat*_*ter 4 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))
    }
}
Run Code Online (Sandbox Code Playgroud)

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

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

dk1*_*k14 8

这有效

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))
}
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

谈论<<=- 你TaskKey的不正确,看到定义:

def apply[T](label : scala.Predef.String, description : scala.Predef.String // not description
Run Code Online (Sandbox Code Playgroud)

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

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

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) =>
    ...
  }
Run Code Online (Sandbox Code Playgroud)

它的工作方式与之前相同.注意,你必须使用"slickGenerate"而不是"slick-generate",最后一个不能用于"help"命令.

顺便说一句,你现在正在使用Bare构建定义 - 您可能希望切换到sbt docs推荐的Multi-project .sbt定义,另请参阅.