如何将库依赖项添加到Build.scala的类路径中? [英] How to add library dependency to classpath of Build.scala?

查看:120
本文介绍了如何将库依赖项添加到Build.scala的类路径中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的Build.scala中使用 sqlite-jdbc驱动程序一个带有一些必要表的sqlite db,然后再进行编译.这是我为实现这一目标而写的:

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
}

这很好,但是当我运行compile时,出现此错误:

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

现在,这有点令人沮丧,因为我认为我可以通过创建递归项目将这种依赖关系添加到Build.scala的类路径中.我的目录结构如下:

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

my-project/project/project/build.sbt看起来像这样:

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

编辑:我也将该行放在my-project/project/build.sbt中,但不能解决我的问题.

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

那么...我做错了什么?为了使sqlite驱动程序正常工作,我需要对类路径具有这种依赖性.

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

推荐答案

那么...我做错了什么?

So... what did I do wrong?

您走了两步太远了.将libraryDependenciesmy-project/project/project/build.sbt移到my-project/project/build.sbt,以便在my-project/project/Build.scala中可用.

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.

这篇关于如何将库依赖项添加到Build.scala的类路径中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆