mercredi 2 janvier 2013

Quick Sbt tutorial

As a beginner in Scala, I naturally began using sbt and it's very easy.

First, what is sbt? Sbt stands for Scala Build Tool. A build tool provides facility to compile, run, test, package your projects. Make, maven, ant, ivy can be included in this category. Sbt is one of them and focus mainly the Scala language.

In this post, I will show how to install sbt, setup a project, import it in Eclipse and add library dependencies.

Installation

sbt comes pre-built with several available packages for different OS : 
  • ZIP or TGZ packages
  • MSI for Windows
  • RPM package
  • DEB package
  • Homebrew or Macports for Mac
  • Gentoo emerge overlays
Or you can manually install sbt. Before, make sure that Java is installed because sbt/scala needs the JVM (Java Virtual Machine) to work. Download sbt-launch.jar and put it under a directory, for example sbt_home. Then, create a script that will launch sbt.  In windows, put the following lines the script (sbt.bat) :



In Linux,  include this line in the script:


Don't forget on Linux to make the script executable : chmod u+x sbt

Then create a variable SBT_HOME that references sbt_home directory and put it in the path.

In Linux, go to .bashrc file and adds the following lines (I presum that the sbt_home is in the home user directory) :



In windows, do the following steps :

  1. Start -> Computer- > Right-click -> System properties
  2. Click on  Advanced system settings > Advanced tab
  3. Click on Environment Variables, under System Variables
  4. Create a new variable SBT_HOME as a key, the sbt_home path directory as a value
  5. Edit the Path by adding %SBT_HOME%/sbt
  6. Ok

Create a project

We will now create a project called myfirstproject and import this project in Eclipse. 

First, create a directory myfirstproject. We will create a project layout that is similar to maven project layout. sbt uses the same directory structure as Maven for source files by default. On linux, type :



Under the same directory, create the build.sbt file. This file contains informations to build the project. For instance, the name, the version, the dependencies, the repositories and on which scala compiler this project depends on. Add the following lines :



Run sbt

To import your project to Eclipse, you must add sbteclipse to your plugin definition file. You can use either the global one at ~/.sbt/plugins/plugins.sbt or the project-specific one at PROJECT_DIR/project/plugins.sbt:

addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "2.1.1")


Then run the command sbt eclipse. In Eclipse use the Import Wizard to import Existing Projects into Workspace. Make sure in Eclipse that you have the scala plugin installed or download the Scala Ide from Typesafe.

Here are some of the most common sbt commands :
  • clean Deletes all generated files (in the target directory).
  • compile Compiles the main sources (in src/main/scala and src/main/java directories).
  • test Compiles and runs all tests.
  • console Starts the Scala interpreter with a classpath including the compiled sources and all dependencies. To return to sbt, type :quit, Ctrl+D (Unix), or Ctrl+Z (Windows).
  • run * Runs the main class for the project in the same virtual machine as sbt.
  • package Creates a jar file containing the files in src/main/resources and the classes compiled fromsrc/main/scala and src/main/java.
  • help Displays detailed help for the specified command. If no command is provided, displays brief descriptions of all commands.
  • reload Reloads the build definition (build.sbt, project/*.scala, project/*.sbt files). Needed if you change the build definition.


If you find typing command very tedious for creating a new project, there is a github project called giter8 that can generate files and directories output from templates. There is a lot of templates for giter, templates for Play, scala, akka, etc. You can find more on those website : 
  • https://github.com/n8han/giter8. You will find information about how to install, use giter in the command line
  • An example to setup a akka project.  The same method can be applied with the correct template : scala-sbt
  • List of templates for giter : https://github.com/typesafehub


Hello World

Create a new scala package (com.myfirstproject) and create the file Hello.scala in this package : 


In a terminal, type sbt run on the project root directory : 


Add Dependencies

Adding dependencies in sbt is also easy. If you want  to add a dependency, for instance scalatest, edit build.sbt and add the following line : 

libraryDependencies += "org.scalatest" %% "scalatest" % "1.6.1" % "test"
sbt uses uses the standard Maven2 repository by default but if your dependency isn't on one of the default repositories, you'll have to add a resolver to help sbt find it. For instance, if you want to add spring libraries in your project, edit build.sbt

libraryDependencies += "org.springframework.scala" % "spring-scala" % "1.0.0.M1"
resolvers += "Spring Dependencies" at "https://repo.springsource.org/libs-milestone" 

Then run sbt eclipse again to update the classpath. Once done, go to Eclipse and refresh the project. 


You can find more documentation in the sbt website : http://www.scala-sbt.org/

Thanks for reading and don't forget to leave some comments!!