0Pricing
Scala for Backend Engineering & Functional Programming · Lesson

Dependency Management and Plugins

Manage external libraries, declare dependencies, and utilize SBT plugins to extend build functionalities.

Dependency Management and Plugins is a free Scala for Backend Engineering & Functional Programming lesson on CoddyKit — lesson 2 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 External Libraries?

When building Scala projects, you often need functionality that isn't part of the core language. Things like logging, database access, or web frameworks are common examples.

Instead of writing all this code yourself, you can use external libraries (also called dependencies). These are pre-written code packages that other developers have created and shared.

Adding Libraries to build.sbt

SBT makes it easy to add these external libraries. You declare them in your build.sbt file using the libraryDependencies setting.

Here's the basic syntax:

  • "org.group": The organization or group ID.
  • % "artifact": The artifact ID (library name). Use %% for Scala libraries to automatically match your Scala version.
  • % "version": The specific version of the library you want.

Example:

libraryDependencies += "org.slf4j" % "slf4j-simple" % "1.7.32"

Dependency Scopes: Compile & Test

Libraries might only be needed for specific parts of your project. SBT uses scopes to manage this.

  • compile (default): Needed for compiling and running your main code.
  • test: Only needed for compiling and running your test code (e.g., a testing framework like ScalaTest).
  • runtime: Not needed for compilation, but required when the application runs.

You specify the scope like this:

libraryDependencies += "org.scalatest" %% "scalatest" % "3.2.16" % "test"

Automatic Dependency Fetching

Imagine a library you use, like a web framework, also uses other libraries itself. These are called transitive dependencies.

SBT automatically handles fetching all these indirect dependencies for you. When you add a library, SBT looks up its own dependencies and downloads them too, ensuring your project has everything it needs.

This saves you from manually tracking dozens of libraries!

Logging with SLF4J

Let's use a common logging library, SLF4J (Simple Logging Facade for Java), with its simple implementation. First, add it to your build.sbt:

libraryDependencies += "org.slf4j" % "slf4j-simple" % "1.7.32"

Now, run the Scala code below to see it in action. Notice how we import org.slf4j.LoggerFactory, which comes from our new dependency.

import org.slf4j.LoggerFactory

object Main {
  private val logger = LoggerFactory.getLogger(getClass.getName)

  def main(args: Array[String]): Unit = {
    logger.info("Hello from a logged message!")
    println("This is a standard print output.")
  }
}

Extending SBT with Plugins

Beyond managing libraries, SBT itself can be extended using plugins. Plugins are special JAR files that add new commands or settings to SBT.

For example, there are plugins for packaging applications, deploying to servers, or even for integrating with specific tools like code formatters.

They help automate common tasks and streamline your development workflow.

Declaring Plugins in plugins.sbt

Unlike regular dependencies that go into build.sbt, SBT plugins are declared in a special file: project/plugins.sbt. This file lives inside the project directory at the root of your SBT project.

The syntax is similar to dependencies, but you use addSbtPlugin:

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

This tells SBT to download and enable the specified plugin for your project.

Creating a Fat JAR with sbt-assembly

One popular plugin is sbt-assembly. It creates a single, executable "fat JAR" containing your application's code and all its dependencies.

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

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

After adding it, you can run sbt assembly from your terminal to create the JAR. The Scala code below is what sbt-assembly would package.

object Main {
  def main(args: Array[String]): Unit = {
    println("This is a simple application.")
    println("sbt-assembly would package this along with its dependencies into a single JAR.")
  }
}

Version Management & Resolvers

As your project grows, managing many dependencies can be tricky. Here are some tips:

  • Consistent Versions: Use the same version for a library across all modules to avoid conflicts.
  • Resolvers: SBT fetches libraries from repositories (like Maven Central). You can add custom resolvers if a library isn't in the default ones.

By default, SBT uses standard public repositories. For private libraries, you'd specify a custom resolver in build.sbt.

Order the Steps

You want to add a new external library to your Scala project and use it. Put the following steps in the correct order.

Lesson Summary

Great job! You've learned how to manage external code in your Scala projects.

  • Dependencies (libraries) are added to build.sbt using libraryDependencies.
  • SBT handles transitive dependencies and supports different scopes (like compile and test).
  • Plugins extend SBT's functionality and are declared in project/plugins.sbt using addSbtPlugin.

Mastering dependency and plugin management is key to building complex and robust Scala applications efficiently!

Frequently asked questions

Is the “Dependency Management and Plugins” lesson free?

Yes — the full text of “Dependency Management and Plugins” 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 “Dependency Management and Plugins”?

Manage external libraries, declare dependencies, and utilize SBT plugins to extend build functionalities. 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 2 of 3, so you can start here or from the beginning and move at your own pace.

How long does the “Dependency Management and Plugins” 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