18.4. Managing Dependencies with SBT
Problem
You want to use one or more external libraries in your Scala/SBT projects.
Solution
You can use both managed and unmanaged dependencies in your SBT projects.
If you have JAR files (unmanaged dependencies) that you want to use in your project, simply copy them to the lib folder in the root directory of your SBT project, and SBT will find them automatically. If those JARs depend on other JAR files, you’ll have to download those other JAR files and copy them to the lib directory as well.
If you have a single managed dependency, such
as wanting to use the Java HtmlCleaner library in
your project, add a libraryDependencies
line like this to your
build.sbt file:
libraryDependencies += "net.sourceforge.htmlcleaner" % "htmlcleaner" % "2.4"
Because configuration lines in build.sbt must be separated by blank lines, a simple but complete file with one dependency looks like this:
name := "BasicProjectWithScalaTest" version := "1.0" scalaVersion := "2.10.0" libraryDependencies += "org.scalatest" %% "scalatest" % "1.9.1" % "test"
To add multiple managed dependencies to your project, define them
as a Seq
in your build.sbt file:
libraryDependencies ++= Seq( "net.sourceforge.htmlcleaner" % "htmlcleaner" % "2.4", "org.scalatest" % "scalatest_2.10" % "1.9.1" % "test", "org.foobar" %% "foobar" % "1.8" )
Or, if you prefer, you can add them one line at a time to the file, separating each line by a blank line:
libraryDependencies += "net.sourceforge.htmlcleaner" % "htmlcleaner" ...
Get Scala Cookbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.