为什么 sbt 不自动将 libraryDependencies 添加到类路径?



为什么SBT不自动将库依赖性添加到类路径中?我正在尝试将JDBC-Sqlite添加到我的项目中,但是找不到驱动程序。LIB依赖性由SBT管理,因此应该是类路径的一部分。但是我想不是,所以我该如何添加?

定义了仅在我的盒子上存在的这些图书馆的路径引用感觉很糟糕。

name := "CacheWarmer"
version := "0.1"
scalaVersion := "2.12.3"
mainClass in Compile := Some("process.Daemon")
libraryDependencies ++= Seq(
  "org.xerial" % "sqlite-jdbc" % "3.20.0" % "test"
)
package process
import java.sql.Connection
import java.sql.DriverManager
import java.sql.ResultSet
import java.sql.SQLException
import java.sql.Statement

代码

object Daemon {
  def main(args: Array[String]): Unit = {
    //Gets java.sql.SQLException: No suitable driver found for jdbc:sqlite::memory:
    val connection:Connection = DriverManager.getConnection("jdbc:sqlite::memory:")
  }
}

Maven Central告诉您应该使用SBT中的该依赖性:

libraryDependencies += "org.xerial" % "sqlite-jdbc" % "3.20.0"->注意没有双%

%%告诉SBT将当前的Scala版本附加到工件名称。假设您正在运行Scala 2.11:

libraryDependencies += "org.some" %% "myscala" % "3.20.0"

被解除到:

`libraryDependencies += "org.some" % "myscala_2.11" % "3.20.0"`

您已将sqlite-jdbc依赖项放入test范围中。自然,这种依赖性仅在测试类路径中可用,而在"主要"类路径中不可用。通常,您将test范围用于测试依赖项,例如测试库,例如Scalatest或Junit。

为了使库在您的"主"类路径中可用,您必须使用compile范围,或者等效地,完全不使用范围分类器:

libraryDependencies ++= Seq(
  "org.xerial" % "sqlite-jdbc" % "3.20.0"
)

最新更新