Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use external libraries inside the build.sbt file

Tags:

scala

sbt

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
  // ... 
}
like image 540
Florian Baierl Avatar asked Jan 23 '19 10:01

Florian Baierl


People also ask

How do we specify library dependencies in sbt?

The libraryDependencies key Most of the time, you can simply list your dependencies in the setting libraryDependencies . It's also possible to write a Maven POM file or Ivy configuration file to externally configure your dependencies, and have sbt use those external configuration files.

What is a build sbt file?

sbt . That file lists all the source files your project consists of, along with other information about your project. Sbt will read the file and then it knows what to do to compile the complete project. Besides managing your project, some build tools, including sbt, can automatically manage dependencies for you.


1 Answers

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".)

like image 147
Travis Brown Avatar answered Oct 05 '22 15:10

Travis Brown