Skip to content

Add SBT wrapper to a project

This tutorial will show how to add SBT wrapper to a project, so we don’t have to install either Sbt or Scala in order to build it.

Similarly to Gradle Wrapper, SBT users also have the ability to add a wrapper to their projects, thanks to the excellent sbt-extras project maintained by a group of more than 50 contributors up to this date. This project provides a sbt bash script that works similarly to gradlew for Gradle projects.

According to the documentation, the script will automatically detect and download the required Scala and Sbt versions on runtime. This means you can work on multiple projects with different version combinations without having to update your environment settings.

How to add the SBT wrapper?

First, download the sbt script from the repository and put it in the root folder of your project, then rename it to sbtx to avoid clashing with any real Sbt installations. You can do all of it by using a single shell command on the root folder of your project:

curl -Ls https://git.io/sbt > sbtx && chmod 0755 sbtx

Then it should appear like the following in your project:

sbtx sbt wrapper in the root folder of the project

Building the project

Now you just run it like any other bash scripts. From the root folder of the project that would be

./sbtx compile

And as you watch the output, you’ll notice it downloads the correct Scala and Sbt versions if necessary.

Downloading sbt launcher for 1.2.8:
  From  https://repo.scala-sbt.org/scalasbt/maven-releases/org/scala-sbt/sbt-launch/1.2.8/sbt-launch-1.2.8.jar
    To  /home/bgasparotto/.sbt/launchers/1.2.8/sbt-launch.jar
Downloading sbt launcher 1.2.8 md5 hash:
  From  https://repo.scala-sbt.org/scalasbt/maven-releases/org/scala-sbt/sbt-launch/1.2.8/sbt-launch-1.2.8.jar.md5
    To  /home/bgasparotto/.sbt/launchers/1.2.8/sbt-launch.jar.md5
/home/bgasparotto/.sbt/launchers/1.2.8/sbt-launch.jar: OK
[info] Loading settings for project global-plugins from idea.sbt ...
[info] Loading global plugins from /home/bgasparotto/.sbt/1.0/plugins
[info] Loading project definition from /home/bgasparotto/git/learning-scala/project
[info] Loading settings for project learning-scala from build.sbt ...
[info] Set current project to learning-scala (in build file:/home/bgasparotto/git/learning-scala/)
[info] Executing in batch mode. For better performance use sbt's shell
[info] Compiling 12 Scala sources to /home/bgasparotto/git/learning-scala/target/scala-2.11/classes ...
[info] Done compiling.
[success] Total time: 4 s, completed Nov 18, 2020 1:13:07 AM

Hope it helps. See ya!

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *