How to add library dependency to classpath of Buil

2019-05-21 05:58发布

问题:

I'm trying to use the sqlite-jdbc driver in my Build.scala to generate a sqlite db with some necessary tables before compilation. This is what I wrote to achieve that:

compile in Compile <<= (compile in Compile) map { result =>
  val sqliteDb = file("my_sqlite.db")
  if (!sqliteDb.exists()) {
    val connection = DriverManager.getConnection(s"jdbc:sqlite:${sqliteDb.getAbsolutePath}")
    val statement = connection.prepareStatement("create table EXAMPLE ( ... );")
    statement.execute()
    statement.close()
    connection.close()
  }
  result
}

That's all well and good, but when I run compile I get this error:

[error] (my-project/compile:compile) java.sql.SQLException: No suitable driver found for jdbc:sqlite:/Users/2rs2ts/src/my-project/my_sqlite.db

Now that's a bit frustrating since I thought I could add that dependency to Build.scala's classpath by creating a recursive project. My directory structure looks like this:

my-project/
  project/
    Build.scala
    build.sbt
    project/
      build.sbt

And my-project/project/project/build.sbt looks like this:

libraryDependencies += "org.xerial" % "sqlite-jdbc" % "3.8.10.1"

Edit: I also put that line in my-project/project/build.sbt and it did not resolve my issue.

So... what did I do wrong? I need this dependency on the classpath in order to get the sqlite driver to work.

回答1:

So... what did I do wrong?

You went two steps too far. Move that libraryDependencies from my-project/project/project/build.sbt to my-project/project/build.sbt that way it will be available in my-project/project/Build.scala.