Skip to Content
Course content

184: Managing Dependencies in sbt

Click on the "Edit" button in the top corner of the screen to edit your slide content.

I was working on a small utility recently to parse some JSON configuration files, and I decided to use upickle because it's fast and doesn't require a mountain of boilerplate. I jumped straight into the code, feeling productive, and wrote this:

import upickle.default._

case class AppConfig(port: Int, host: String)
object AppConfig {
  implicit val rw: ReadWriter[AppConfig] = macroRW
}

val json = """{"port": 8080, "host": "localhost"}"""
val config = read[AppConfig](json)
println(s"Starting server on ${config.host}:${config.port}")

The Missing Symbol Headache

Of course, the compiler immediately turned red. cannot find symbol upickle. I paused for a second—I'm so used to the IDE handling things that I completely forgot that Scala doesn't just "know" where external libraries live. Unlike the standard library, upickle isn't baked into the language. It lives in a repository (usually Maven Central), and my project has no idea it's supposed to be there.

If I just keep adding imports, I'm just fighting a losing battle. I need to tell sbt (the Scala Build Tool) to go fetch this specific piece of code and make it available on my classpath during compilation.

Telling sbt where to look

I opened my build.sbt file. Right now, it's pretty bare. To fix this, I need to add a libraryDependencies setting. I've seen people just copy-paste these lines, but let's look at the anatomy of the line I'm adding:

libraryDependencies += "com.lihaoyi" %% "upickle" % "3.1.0"

Wait, why the double percent %%? I remember this tripping me up early on. A single % is used for Java libraries. But Scala has a problem: binary compatibility between versions (like 2.12 vs 2.13 vs 3). If I use %%, sbt automatically appends the Scala version I'm using to the artifact name. It turns upickle into upickle_2.13 (or whatever version you're on) behind the scenes. It saves us from manually tracking which version of the library matches our compiler.

I saved the file, and sbt automatically reloaded. I checked the console, saw a flurry of downloads, and suddenly the red squiggly lines in my editor vanished. The code now compiles because sbt downloaded the JAR and told the compiler, "Hey, look in here for the upickle package."

Cleaning up the production classpath

Now, I wanted to write a test to make sure my JSON parsing doesn't blow up if the input is malformed. I decided to use munit for the testing framework. My first instinct was to just add it the same way:

libraryDependencies += "org.scalameta" %% "munit" % "0.7.29"

That works, but there's a problem. If I package this app into a JAR to ship it to a server, I don't want the testing framework bundled in. I don't need munit to run the actual app; I only need it while I'm developing and running tests. Including it in the production build just bloats the file.

I adjusted the line to use a "scope":

libraryDependencies += "org.scalameta" %% "munit" % "0.7.29" % Test

By adding % Test at the end, I'm telling sbt: "Download this, use it when I run sbt test, but keep it completely out of the main compilation and the final production artifact." It's a small detail, but in a large professional project with hundreds of dependencies, managing these scopes is the difference between a 50MB JAR and a 500MB JAR.




📋 Practical Task

Building a File-Counting Utility with os-lib

Your task is to set up a new Scala project that uses the os-lib library (a popular library for file system operations) to count how many files exist in a specific directory. You will also need to add a testing framework that is only available during the test phase.

Requirements:

  • Modify your build.sbt to include os-lib (groupId: com.lihaoyi, artifactId: os-lib, version: 0.9.1) as a compile-time dependency.
  • Add munit (groupId: org.scalameta, artifactId: munit, version: 0.7.29) as a test-scope dependency.
  • In your main code, use os.list(".") to get a list of files in the current directory and print the size of that list.
  • Ensure the project compiles and that os-lib is accessible in the main source folder, but munit is only accessible if you were to create a file in src/test/scala.
Rating
0 0

There are no comments for now.

to be the first to leave a comment.