Introduction to sbt

    With this post, I will try to start a series of translations of official documentation about a tool that, with the current growth of the Scala language, is becoming more and more popular, but about which there is very little information in Russian, however.
    It will be about sbt - the project assembly system for the Scala language (although it is important to mention that Java projects (and generally any others) can also be built by them).
    The article is the beginning of the translation of documentation from the scala-sbt.org project website and since this is my first translation experience, I will be glad to any comments and corrections.
    Also, due to the fact that while the translation is in the form of an article, I will skip points that would not look quite right in the context of a separate part of the manual.

    Foreword


    sbt, using a small number of concepts, offers flexible solutions for building projects.
    This guide will talk about some of the things you need to create and support build solutions with sbt.
    This guide is highly recommended for reading. But, if you have no time to read everything, then you can read the most important information in the sections “Assembly .sbt Parameters” , Assembly Areas” , “Additional Assembly Parameters” . But, we do not promise that this good idea will help you skip all the pages of this guide.
    It is best to read this document sequentially, drawing on earlier material.
    Thanks for using sbt! We wish you maximum pleasure from this!

    1. Install sbt


    To create an sbt project you need to take the following steps:
    • Install sbt and create a startup script
    • Create a simple “Hello world” project
    • Create a project directory with source files inside
    • Describe build options
    • Read how to run sbt
    • Continue reading the manual on sbt build options

    Ultimately, the installation boils down to running a JAR file and shell script. But, we will describe several paths for different platforms that can make installation less tedious.
    If you have problems starting sbt, see the “Installation notes” section .

    1.a. Mac Installation

    Using Macports
    $ port install sbt
    Homebrew
    $ brew install sbt

    1.b. Windows installation

    Just download the msi installer and run it.

    1.c. Linux installation

    Officially supported distributions:
    RPM package
    DEB package

    In the future I will talk about how you can download and configure sbt manually. In the meantime, the most interesting.

    2. Hello, World


    Create a project directory with source code

    One of the correct options for an sbt project may be a directory containing one source file. Try creating a hello directory with the hw.scala file, with the following contents:
    object Hi {
      def main(args: Array[String]) = println("Hi!")
    }
    

    Now, in the directory itself, run sbt and type the run command in the interactive console. On Linux or OS X, it looks something like this:
    $ mkdir hello
    $ cd hello
    $ echo 'object Hi { def main(args: Array[String]) = println("Hi!") }' > hw.scala
    $ sbt
    ...
    > run
    ...
    Hi!
    

    When creating a project, sbt works in accordance with the following rules:
    • The source lies in the root directory
    • Sources are in the src / main / scala or src / main / java directory
    • Tests are in src / test / scala or src / test / java
    • Resource files in src / main / resources or src / test / resources
    • Jar files in the lib directory

    By default, sbt will build the project with that version of scala, with which sbt was launched by itself.
    In addition to starting the console, the project can be immediately launched for execution by the sbt run command .

    Build options

    Most projects still need a more sophisticated build process setup. In sbt, the main assembly parameters are stored in the build.sbt file in the root directory of the project.
    For example, if we created a settings file for our hello project, it would look something like this:
    name := "hello"
    version := "1.0"
    scalaVersion := "2.10.3"
    

    Pay attention to empty lines. It’s not just that, they are actually required to separate lines in the configuration file and without them sbt will give an error. We will return to this file in more detail in the following sections.

    Install sbt version

    You can force download and install the necessary version of sbt if you write the following line in the hello / project / build.properties file:
    sbt.version=0.13.5
    Now, at startup, version sbt 0.13.5 will be used. If it is not, then the script will download and install it in the system.
    Keep the sbt version in the project / build.properties file to avoid possible collisions.

    In conclusion


    For the experiment, I decided to confine myself to only these very first sections, and if the reaction is more or less positive, I hope to continue translating the rest.

    PS I would be very grateful for the indicated inaccuracies and comments of the translation. Thanks!

    Also popular now: