0Pricing
Scala for Backend Engineering & Functional Programming · Lesson

Multi-Project Builds and Deployment

Configure and manage multi-project SBT builds for larger applications and prepare for deployment.

Multi-Project Builds and Deployment is a free Scala for Backend Engineering & Functional Programming lesson on CoddyKit — lesson 3 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Scala for Backend Engineering & Functional Programming learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Why Multi-Project Builds?

As Scala applications grow, managing all code in a single project can become difficult. Multi-project builds help organize your codebase.

They allow you to:

  • Separate different modules (e.g., core logic, API, utilities).
  • Manage dependencies between these modules clearly.
  • Apply specific settings to individual parts of your application.

This approach improves modularity and maintainability.

Defining Multiple Projects

In SBT, you define multiple projects within your main build.sbt file. Each project is typically a lazy val.

The root project usually aggregates or depends on these sub-projects.

Here's a basic setup for a root project and two sub-projects:

lazy val root = project
  .in(file("."))
  .aggregate(core, api)

lazy val core = project
  .in(file("core"))

lazy val api = project
  .in(file("api"))

Sub-Project Directory Structure

For a multi-project build, each sub-project typically resides in its own directory at the root level of your project.

The standard structure looks like this:

  • my-app/ (root project directory)
    • build.sbt (main build file)
    • project/ (SBT build definition files)
    • core/ (sub-project 'core' directory)
      • src/ (source code for 'core')
    • api/ (sub-project 'api' directory)
      • src/ (source code for 'api')

Example Sub-Project Code

Each sub-project can contain its own Scala source files. For instance, our core project might have a simple utility.

Try running this example from within the core project:

package com.coddykit.core

object Greeter {
  def greet(name: String): String = s"Hello, $name!"
}

object CoreApp {
  def main(args: Array[String]): Unit = {
    println(Greeter.greet("CoddyKit"))
  }
}

Inter-Project Dependencies

Often, one sub-project needs to use code from another. For example, your api project might need the Greeter from the core project.

You declare these dependencies using .dependsOn() in your build.sbt.

lazy val root = project
  .in(file("."))
  .aggregate(core, api)

lazy val core = project
  .in(file("core"))

lazy val api = project
  .in(file("api"))
  .dependsOn(core)

Sharing Common Settings

Many settings (like Scala version or common dependencies) might be the same across all your sub-projects.

To avoid repetition, you can define a commonSettings block and apply it to each project.

lazy val commonSettings = Seq(
  scalaVersion := "2.13.8",
  organization := "com.coddykit"
)

lazy val root = project
  .in(file("."))
  .aggregate(core, api)
  .settings(commonSettings)

lazy val core = project
  .in(file("core"))
  .settings(commonSettings)

lazy val api = project
  .in(file("api"))
  .dependsOn(core)
  .settings(commonSettings)

Aggregating Tasks

When you have a root project aggregating sub-projects, running a task like compile on the root will automatically run it on all aggregated sub-projects.

For example, typing sbt compile in your root directory will compile both core and api.

The .aggregate() method is key for this behavior.

Packaging for Deployment

Once your application is built, you need to package it for deployment. This usually means creating an executable JAR (Java Archive) file.

For multi-project builds, you often want a single JAR that includes all your project's code and its dependencies. This is called a 'fat JAR' or 'uber JAR'.

SBT doesn't create fat JARs by default, but plugins can help.

SBT Assembly Plugin for Fat JARs

The sbt-assembly plugin is widely used to create fat JARs. It bundles all compiled classes and dependencies into one executable JAR.

First, add the plugin to project/plugins.sbt:

addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "2.0.0")

Multi-Project Packaging

Which of the following statements about packaging a multi-project Scala application for deployment is TRUE?

Recap & Next Steps

You've learned how to structure and manage multi-project builds in SBT!

  • We covered defining sub-projects in build.sbt.
  • You saw how to organize directories and manage inter-project dependencies with .dependsOn().
  • We discussed sharing common settings and aggregating tasks.
  • Finally, we explored packaging for deployment, specifically using the sbt-assembly plugin to create fat JARs.

Mastering multi-project builds is crucial for larger, more complex Scala applications, ensuring modularity and efficient development workflows.

Frequently asked questions

Is the “Multi-Project Builds and Deployment” lesson free?

Yes — the full text of “Multi-Project Builds and Deployment” is free to read here on the web, and the Scala for Backend Engineering & Functional Programming course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Scala for Backend Engineering & Functional Programming course, upgrade to CoddyKit PRO.

What will I learn in “Multi-Project Builds and Deployment”?

Configure and manage multi-project SBT builds for larger applications and prepare for deployment. You practise Scala for Backend Engineering & Functional Programming with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Scala for Backend Engineering & Functional Programming?

No prior experience is required. Scala for Backend Engineering & Functional Programming on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 3, so you can start here or from the beginning and move at your own pace.

How long does the “Multi-Project Builds and Deployment” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Scala for Backend Engineering & Functional Programming lesson?

Yes. Every Scala for Backend Engineering & Functional Programming lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. SBT Project Setup and Basics
  2. Dependency Management and Plugins
  3. Multi-Project Builds and Deployment
← Back to Scala for Backend Engineering & Functional Programming