Search code examples
scalasbt

How to use external dependencies in sbt's .scala files?


This is for Scala 2.11.1 and sbt 0.13.5.

Say I have a Scala/sbt project with the following directory structure:

root/
  build.sbt
  src/ ..
  project/
    plugins.sbt
    build.properties
    LolUtils.scala

and I want to use some external library in LolUtils.scala. How is this generally accomplished in sbt?

If I simply add the libs I need into build.sbt via libraryDependencies += .. then it doesn't find them and fails on the import line with not found: object ...

If I add a separate project/build.sbt, for some reason it starts failing to resolve my plugins, plus I need to manually specify the Scala version in the nested project/build.sbt, which is unnecessary duplication.

What's the best way to accomplish this?


Solution

  • sbt is recursive which means that it uses itself to compile a build definition, i.e. *.sbt files and *.scala files under project directory. To add extra dependencies to use them in the build definition you have to declare them in a project/build.sbt.

    There is one caveat to that. You can set any scalaVersion to your project, that is in build.sbt, but you should not modify scalaVersion in the project/build.sbt as it might conflict with the version sbt itself uses (that may or may not lead to binary incompatibility for plugins).

    Sbt 0.13.5 is using Scala 2.10.4, and the library you're going to use must be compatible with that particular version of Scala.

    > about
    [info] This is sbt 0.13.5
    ...
    [info] sbt, sbt plugins, and build definitions are using Scala 2.10.4