In this post I’ll explain a couple of simple tools to manage your dependencies and compilation options with SBT on a simple and clean way.
The main problem is that, in large projects, SBT files tend to grow and become unusable. I have used several techniques along the time (mostly based on manual ordering, comments and good intentions) but today I learned how to do it on a simpler and way more powerful way.
In recent versions of SBT, it is possible to include Scala objects with content. The idea is then to manage our dependencies, resolvers and anything else in Scala files, and then use them on the SBT. In this post I’ll show how to use it, transforming a real SBT file of a very simple project.
This is our original SBT file. It is OK for now, but we’ve all seen how this ends up after a couple years and 10 developers go by the project.
So, how can we improve it? We’ll take the dependencies out to a separate file, the resolvers to another, and then link everything.
IMPORTANT: these files must be located at /project so they are found by the build.sbt.
This is how we manage the dependencies. As you can see there are two objects, one for the dependencies and one for the versions. This is done so everything is clearer to find and keep updated.
An additional advantage of this approach is that it is composable. If your Dependencies object grows too much, you can split it in different ones for the different modules of your application and then join them all together.
We can manage the repositories on the same way.
And finally, this is our SBT file. Note that we don’t need to explicitly include the previous two files, as they are automatically loaded if they are in the correct path. As you can see, it’s really simple, elegant and really improves legibility.