我是 Scala 和 Slick 的新手。尝试从这里https://github.com/slick/slick-codegen-example使用代码生成器示例并将其更改为 Postgres 驱动程序。这是我在 Build.Scala 文件中的代码:
import sbt._
import Keys._
import Tests._
object myBuild extends Build {
val slickVersion = "3.0.2"
lazy val mainProject = Project(
id="main",
base=file("."),
settings = Project.defaultSettings ++ Seq(
scalaVersion := "2.11.6",
libraryDependencies ++= List(
"com.typesafe.slick" %% "slick" % slickVersion,
"com.typesafe.slick" %% "slick-codegen" % slickVersion,
"org.slf4j" % "slf4j-nop" % "1.7.12",
"org.postgresql" % "postgresql" % "9.4-1201-jdbc41"
),
slick <<= slickCodeGenTask, // register manual sbt command
sourceGenerators in Compile <+= slickCodeGenTask // register automatic code generation on every compile, remove for only manual use
)
)
// code generation task
lazy val slick = TaskKey[Seq[File]]("gen-tables")
lazy val slickCodeGenTask =
(sourceManaged, dependencyClasspath in Compile, runner in Compile, streams) map { (dir, cp, r, s) =>
val outputDir = (dir / "slick").getPath // place generated files in sbt's managed sources folder
val url = "jdbc:postgresql://localhost:5432/db'" // connection info
val jdbcDriver = "org.postgresql.Driver"
val slickDriver = "slick.driver.PostgresDriver"
val pkg = "dao"
val user="postgres"
val password="pass"
toError(r.run("slick.codegen.SourceCodeGenerator", cp.files, Array(slickDriver, jdbcDriver, url, outputDir, pkg, user, password), s.log))
val fname = outputDir + "/dao/Tables.scala"
Seq(file(fname))
}
}
我唯一改变的是驱动程序和登录名,密码。据我了解,每次运行时它都应该生成代码
激活器运行
但它不会生成任何代码。另外,它如何手动运行?