TExcelTAD
Home › Scala › Scala Build Tools & Libraries

Scala Build Tools & Libraries

Single files are perfect for learning, but real projects have many source files, tests, third-party libraries and a packaging step. Build tools automate all of that. The Scala world has three main options: scala-cli for scripts and small apps, sbt — the long-standing standard — and Mill, a newer, simpler alternative.

Comparing the tools

scala-cli sbt Mill
Configuration //> using directives in source files, or project.scala build.sbt (a Scala DSL) build.mill (plain Scala)
Multi-module builds limited excellent excellent
Learning curve very low medium–high medium
Best for scripts, prototypes, small services, teaching most production projects and libraries teams that want fast, readable builds
Runs tests scala test . sbt test mill app.test

scala-cli projects

Put directives in a project.scala file, and every .scala file in the directory becomes part of the project:

scala
// project.scala
//> using scala 3.7.1
//> using jvm 21
//> using dep com.lihaoyi::upickle:4.1.0
//> using test.dep org.scalameta::munit:1.1.1
bash · try it yourself
scala run .
Compiling project (Scala 3.7.1, JVM (21))
Compiled project (Scala 3.7.1, JVM (21))
Server listening on port 8080

Other useful commands: scala compile ., scala test ., scala fmt . (formats code with scalafmt) and scala --power package . -o app --assembly to build a single runnable JAR.

sbt projects

An sbt build is described in build.sbt at the project root:

scala
// build.sbt
ThisBuild / scalaVersion := "3.7.1"
ThisBuild / organization := "com.example"

lazy val core = project
  .settings(
    name := "shop-core",
    libraryDependencies ++= Seq(
      "com.lihaoyi"   %% "upickle" % "4.1.0",
      "org.scalameta" %% "munit"   % "1.1.1" % Test
    )
  )

lazy val api = project
  .dependsOn(core)
  .settings(name := "shop-api")

The %% operator appends the Scala binary version to the artifact name (e.g. upickle_3), because Scala libraries are published separately for each major Scala version. Java libraries use a single %.

sbt is usually used interactively: start sbt once and type tasks in its shell, which keeps the compiler warm and makes later commands fast.

bash · try it yourself
sbt
[info] welcome to sbt 1.11.2 (Eclipse Adoptium Java 21.0.5)
[info] loading project definition from /home/me/shop/project
[info] loading settings for project root from build.sbt...
[info] set current project to shop (in build file:/home/me/shop/)
[info] sbt server started at local:///home/me/.sbt/1.0/server/sbt.sock
[info] started sbt server
sbt:shop> 
sbt task What it does
compile compile main sources
run run the main class
test run all tests
testOnly *CartSuite run matching test classes
~test re-run tests whenever a file changes
console a REPL with your project on the classpath
reload re-read build.sbt after editing it
core/compile run a task in one sub-project

Finding and adding libraries

Scala libraries are published to Maven Central, just like Java ones. Search on index.scala-lang.org (Scaladex) to find a library and copy its coordinates for scala-cli, sbt or Mill.

Coordinates Meaning
com.lihaoyi::os-lib:0.11.4 (scala-cli, Mill) group, artifact, version; :: adds the Scala suffix
"com.lihaoyi" %% "os-lib" % "0.11.4" (sbt) the same library in sbt syntax
org.postgresql:postgresql:42.7.4 a Java library: single :, no Scala suffix

Popular libraries

Area Libraries
Effects & concurrency Cats Effect, ZIO, Pekko (fork of Akka)
HTTP servers & clients http4s, Tapir, ZIO HTTP, sttp, Play
JSON Circe, uPickle, jsoniter-scala, ZIO JSON
Databases Doobie, Skunk, Quill, Magnum, ScalikeJDBC
Functional programming Cats, Kittens, Monocle
Big data Apache Spark, Apache Flink
Testing munit, ScalaTest, ScalaCheck, Weaver
Scripting & tools os-lib, requests-scala, mainargs (the com-lihaoyi toolkit)

Project layout

Both sbt and Mill (in its Maven-compatible mode) use the conventional layout:

text
shop/
├── build.sbt
├── project/
│   ├── build.properties     # sbt.version=1.11.2
│   └── plugins.sbt          # e.g. sbt-scalafmt, sbt-assembly
├── src/
│   ├── main/
│   │   ├── scala/           # application code
│   │   └── resources/       # config files, templates
│   └── test/
│       └── scala/           # tests
└── .scalafmt.conf

Packaging and running in production

bash · try it yourself
sbt assembly
[info] compiling 12 Scala sources to /home/me/shop/target/scala-3.7.1/classes ...
[info] Built: /home/me/shop/target/scala-3.7.1/shop-assembly-0.1.0.jar
[success] Total time: 18 s

The resulting "fat JAR" contains your code plus all dependencies and runs with java -jar shop-assembly-0.1.0.jar. Other options are native packages via sbt-native-packager, Docker images, GraalVM native images and Scala Native binaries.

Tip: Pin exact versions of Scala, sbt and every library. Reproducible builds save hours of "works on my machine" debugging.

Test yourself

In sbt, what does %% do in "org.typelevel" %% "cats-core" % "2.12.0"?

  1. Marks a test dependency
  2. Appends the Scala binary version to the artifact name
  3. Makes the dependency optional
  4. Downloads the latest version

Which tool reads //> using dep ... directives?

  1. sbt
  2. Mill
  3. scala-cli
  4. Maven

Where do test sources go in a standard sbt project?

  1. src/test/scala
  2. test/
  3. src/main/test
  4. Next to each source file