Introduction to sbt

Written by HeadsnHands | Published 2017/05/19
Tech Story Tags: scala | sbt | programming | projects | solutions

TLDRvia the TL;DR App

In this article I want to summarize the official data about the tool which usage increased with growth of Scala programming language usage. I speak about sbt — the system of projects’ compilation for Scala. It’s also important to mention that Java projects and any other projects overall can be compiled by this method also.

Because it’s just an introductory piece I’ll be leaving some things out for the future parts of the guide.

Preamble

sbt, by using small number of concepts, offers flexible solutions for projects compilation.

This guide will tell you about several moments that you need to create and support the compilation solutions made with sbt help.

If you don’t have time to read the whole manual, please, at least read the main parts of info in paragraphs about ‘.sbt compilation parameters’, ‘fields of compilations’ and ‘additional compilations’ parameters’. Still, it may be not such a good idea to skip the other parts of the manual.

1. Installing sbt

To create sbt project you need to follow these steps:

  • Install sbt and create loading scripts
  • Create a simple project “Hello world”
  • Create directory of the project with initial code inside
  • Describe the compilation parameters
  • Read how to run sbt
  • Continue reading the manual about sbt compilation parameters

In the end, all you need for installation is to run JAR file and shell script. However, I describe several paths for you to imply on various platforms to make the process even less painful.

If you’re having problems with sbt start, please, look at the ‘Remarks’ directory.

1.a. Install for Mac

With a help of Macports

$ port install sbt

Homebrew

$ brew install sbt

1.b. Install for Windows

Download the msi installer and run it.

1.c. Install for Linux

Official distribution kits:

RPM package

DEB package

Later I will tell you more about how to download and adjust sbt manually. Let’s now proceed to the most interesting part.

2. Hello, World

Create a project directory with initial code

One way to create a correct sbt project is to create a directory with one file with initial code. Try creating a directory hello with file hw.scala with such content:

object Hi { def main(args: Array[String]) = println(“Hi!”)}

Now, start sbt in this directory and type run command ‘run’ on the interactive console. While working on Linux or OS X, you’ll see it like this:

$ mkdir hello$ cd hello$ echo ‘object Hi { def main(args: Array[String]) = println(“Hi!”) }’ > hw.scala$ sbt…> run…Hi!

Upon project creation, sbt works according to these rules:

  • Source code is available in root directory
  • Source codes lay in src/main/scala or src/main/java directories
  • Tests are available in src/test/scala or src/test/java directories
  • Resource files available in src/main/resources or src/test/resources
  • File jar is available in lib

By default, sbt gathers project for scala version which helped to run sbt. Additionally to running console, the project can also adjusted to perform sbt run command upon its start.

Compilation parameters

Most of the projects still need more complex adjustments of the compilation process. The main compilation parameters in sbt are stored in build.sbt file in root directory of the project.

For example, if we want to create the settings file for our hello project, it will look like this:

name := “hello”

version := “1.0”

scalaVersion := “2.10.3”

Take the blank spaces into account. It was made on purpose, you need it to separate the lines in configuration file, without it sbt will write an error. We’ll be talking more about this file in the later directories.

Installation of sbt version

You can forcibly download and install new version of sbt if in the file hello/project/build.properties you’ll write this line:

sbt.version=0.13.5

Now, when you run a file it will use new version of sbt 0.13.5. If it’s not available the script will download it and install it into your system.

You need to store your sbt version exactly in project/build.properties file to avoid collisions possible.

To conclude

In terms of experiment I decided to limit myself by these first directories. If the matter is interesting to my readers, I will continue writing a manual.

P.S. If you liked the story, please, give our team a heart.


Published by HackerNoon on 2017/05/19