In this article, we're going to introduce you to a development tool for your Scala projects. SBT stands for "Simple Build Tool". Let's look at SBT Scala...
It’s an open-source build tool that makes it easy to manage your projects. It lets you manage your dependencies, compile, execute and distribute your JAR files. Let’s take a closer look at its main features.
SBT's main features
In a configuration file, you can manage your dependencies and enter the versions you need. Choosing the version of your dependencies allows you to avoid conflicts between them, avoid bugs and security holes, and facilitate maintenance. SBT downloads the latter from an online library repository, such as Maven Central. On this site, you’ll find the available dependencies, their versions and the syntax you need to integrate them into your project.
First of all, SBT loads your project information from the configuration file. Then, it uses Scala’s compilation system to compile your programs into bytecode files that can be executed by the JVM. These are placed in the “target” directory. SBT uses incremental compilation to optimize compilation. This means that it will only recompile modifications.
SBT offers a solution for testing your code using the `sbt test` command, which executes all the tests in the project. It’s also possible to test a specific class or a file’s tests with `sbt testOnly`. Let’s not forget that unit tests can save you a considerable amount of time. They save you having to rework your entire code when a bug is detected. They ensure that a piece of code is properly integrated into your project.
Publishing JAR files
SBT lets you compress your project into a JAR file using the `sbt package` command. Then, using the `sbt publish` command, you can publish your JAR file on the repository of your choice.
Defining the different modes in SBT Scala
If you’d like to interact more easily with your project, SBT offers an interactive mode available by simply issuing the `sbt` command. The advantages of this mode are: autocompletion of your commands and certain other commands unavailable in the standard SBT console.
This mode monitors your source files and automatically compiles them whenever changes are made. This saves time and makes debugging easier.
For example, for tests, run `sbt “~ test”` in interactive mode, and SBT will automatically trigger the tests in the background.
How is a SBT Scala project structured?
The build.sbt file
This is the configuration file (you can name it anything you like, it just has to end in sbt). Here you specify your dependencies, compilation options, plugins to extend SBT’s functionality and parameters for publishing, testing and deployment.
The target directory
This directory is automatically created by the `sbt compile` command, for example. It contains your class files, your JAR, WAR or ZIP files generated during project assembly. It also contains your documentation files, created from source code comments. Cache files to optimize compilation performance, and finally, your test reports. You are strongly advised not to modify these files yourself.
What is the project file?
It is used to store your project’s configuration files. The most common file is “build.properties”, which contains the project’s construction properties. The “build.sbt” file defined above. The “build.scala” file, which is an alternative to “build.sbt” except that your configuration uses Scala syntax. This alternative is used for more complex projects. Finally, the “plugins.sbt” file contains the SBT plugins used. These add functionalities such as test management, documentation compilation or code generation.
SBT' Scala - main controls
If you’d like to learn more about the technologies used in the Data Engineer profession, take a look at our dedicated training course below!