向 sbt 添加任务 13.x build.sbt
adding task to sbt 13.x build.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 启动正常,但是这个新任务显然 not 被 sbt 识别并且不能在 sbt 提示符中被 运行。我对任务定义的 :=
语法也不太满意。现有文档令人困惑。
如何在 sbt 提示符中提供这个新任务?
这个有效
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
因此,旧定义 <<=
使用 "generate slick code" 作为标签,而新 :=
使用代码给定的命令名称(新样式),因此它使用您的 "generate slick code" 作为文档。这看起来很奇怪且不一致,但这是事实,部分原因是向后兼容。
所以,正确的旧式版本是:
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) =>
...
}
它的工作方式与以前相同。请注意,您必须使用 "slickGenerate",而不是 "slick-generate",最后一个不适用于 "help" 命令。
顺便说一下,您正在使用 Bare build definition now - you may want to switch to Multi-project .sbt definition as it's recommended by sbt docs, 。
我已将此添加到 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 启动正常,但是这个新任务显然 not 被 sbt 识别并且不能在 sbt 提示符中被 运行。我对任务定义的 :=
语法也不太满意。现有文档令人困惑。
如何在 sbt 提示符中提供这个新任务?
这个有效
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
因此,旧定义 <<=
使用 "generate slick code" 作为标签,而新 :=
使用代码给定的命令名称(新样式),因此它使用您的 "generate slick code" 作为文档。这看起来很奇怪且不一致,但这是事实,部分原因是向后兼容。
所以,正确的旧式版本是:
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) =>
...
}
它的工作方式与以前相同。请注意,您必须使用 "slickGenerate",而不是 "slick-generate",最后一个不适用于 "help" 命令。
顺便说一下,您正在使用 Bare build definition now - you may want to switch to Multi-project .sbt definition as it's recommended by sbt docs,