Use external libraries inside the build.sbt file

  • A+
Category:Languages

Is it somehow possible to use an external library inside the build.sbt file?

E.g. I want to write something like this:

import scala.io.Source import io.circe._ // not possible  version := myTask  lazy val myTask: String = {   val filename = "version.txt"   Source.fromFile(filename).getLines.mkString(", ")   // do some json parsing using the circe library   // ...  } 

 


One of the things I actually like about sbt is that the build project is (in most ways) just another project (which is also potentially configured by a meta-build project configured by a meta-meta-build project, etc.). This means you can just drop the following line into a project/build.sbt file:

libraryDependencies += "io.circe" %% "circe-jawn" % "0.11.1" 

You could also add this to plugins.sbt if you wanted, or any other .sbt file in the projects directory, since the filenames (excluding the extension) have no meaning beyond human convention, but I'd suggest following convention and going with build.sbt.

Note though that sbt implicitly imports sbt.io in .sbt files, so the circe import in your build.sbt (at the root level—i.e. the build config, not the build build config) will need to look like this:

import _root_.io.circe.jawn.decode  scalaVersion := decode[String]("/"2.12.8/"").right.get 

(For anyone who hasn't seen it before, the _root_ here just means "start the package hierarchy here instead of assuming io is the imported one".)

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: