1. Introduction

SBT is an interactive build tool for Scala and Java projects. It has native support for compiling Scala projects and integrates with several Scala test libraries. It also allows us to define tasks in Scala and run them in parallel from an interactive shell.

The build.sbt file contains information required to build our project, including details about dependencies. Our previous article goes into more detail about how to package a Scala app with SBT.

In this article, we’ll look at how to exclude dependencies in a project that we’re building with SBT.

2. Exclude Dependencies from a Dependency

The build.sbt file contains dependency information for our project. In this file, we’re able to define dependencies to include and exclude in the project. Dependencies often contain further dependencies themselves. We refer to these as transitive dependencies.

Often, we’ll be in a position where we don’t want to include these transitive dependencies – for instance, if we’re already manually including the dependency ourselves, or if another dependency already includes it transitively and we don’t want to have version issues at runtime. Therefore, it’s important to have the ability to prevent dependencies from including other dependencies we don’t want.

We exclude dependencies in SBT by using either the exclude or excludeAll keywords. It’s important to understand which one to use: excludeAll is more flexible but cannot be represented in a pom.xml. Therefore, we should use exclude if a pom.xml will be published for the project.

In the build.sbt example below, we’re adding two dependencies: log4j and scala-parser-combinators. The log4j dependency includes javax.jmx and com.sun.jmx dependencies, which we want to exclude from the build. The exclude keyword takes two arguments – the organization name and the module name to exclude:

scalaVersion := "2.13.8"
name := "sbt-exclude-dependency"
organization := "org.baeldung"
version := "1.0"
libraryDependencies ++= Seq(
  "org.scala-lang.modules" %% "scala-parser-combinators" % "2.1.1",
  "log4j" %% "log4j" % "1.2.15" exclude("javax.jms", "jms") exclude("com.sun.jmx", "jmxri")

Let’s look at the same example again. This time, we’ll use the excludeAll keyword to exclude the same dependencies:

libraryDependencies ++= Seq(
  "org.scala-lang.modules" %% "scala-parser-combinators" % "2.1.1",
  "log4j" % "log4j" % "1.2.15" excludeAll(
    ExclusionRule(organization = "com.sun.jmx", name = "jmxi"),
    ExclusionRule(organization = "javax.jms"))

We can omit the name parameter if we want to exclude all modules within an organization.

3. Exclude Dependencies from All Dependencies

We’ll often want to exclude a transitive dependency from all dependencies in the project, rather than from a single defined dependency. To achieve this, we can set up ExclusionRules in excludeDependencies. Let’s remove commons-logging from every dependency in the project:

excludeDependencies ++= Seq(
  ExclusionRule("commons-logging", "commons-logging")

4. Conclusion

To wrap up, we’ve covered how to use exclude and excludeAll to exclude dependencies in SBT. It’s important to prefer exclude over excludeAll if a pom.xml needs to be generated.

We’ve also seen how to use excludeDependencies to define transitive exclusions across all dependencies in the project.

To learn more about the syntax inside build.sbt, be sure to check out one of our previous articles on SBT.

Comments are closed on this article!