mushroom mushroom - 1 month ago 9
Scala Question

How can I use a library dependency in the definition of an sbt task?

I am defining an sbt task that needs to call code in a library. Here is a build.sbt file with what I've tried so far:

libraryDependencies ++= Seq("com.some.company" %% "some-lib" % "1.0.0")

val doSomething = taskKey[Unit]("does something")

doSomething := {
import com.some.company.function
function()
}


The imports do not work. How do I define a task that depends on code in an external library?

Answer

To build the .sbt file itself in the root directory, SBT uses information in the project directory. So put a build.sbt in the project directory and set the libraryDependencies key there:

libraryDependencies ++= Seq("com.some.company" %% "some-lib" % "1.0.0")

So, to clarify, you now have two build.sbt files:

  1. ./build.sbt
  2. ./project/build.sbt