Creating Scala Project With SBT 0.12


I have recently started working with scala. SBT is the tool of choice for Scala based projects. Let us see how to get it working on windows.

Installing SBT for Windows
Create a batch file sbt.bat which contains following script

   $ set SCRIPT_DIR=%~dp0
   $ java -Xmx512M -jar "%SCRIPT_DIR%sbt-launch.jar" %*
   

Now, put the sbt-launch.jar file in same directory where you put sbt.bat file.Put sbt.bat on your path so that you can launch sbt in any directory by typing sbt at the command prompt.
After installing SBT if we type in sbt in the command prompt in an empty directory, this is what we are likely to see

D:\hello>sbt
[info] Set current project to default-30dbd1 (in build file:/D:/hello/)

In order to create project execute the following commands in the sbt session.

> set name :="hello"
[info] Reapplying settings...
[info] Set current project to hello (in build file:/D:/hello/)
>
> set scalaVersion :="2.9.2"
[info] Reapplying settings...
[info] Set current project to hello (in build file:/D:/hello/)
> set version :="1.0"
[info] Reapplying settings...
[info] Set current project to hello (in build file:/D:/hello/)
> session save
[info] Reapplying settings...
[info] Set current project to hello (in build file:/D:/hello/)
> exit

SBT creates files and directories when we executed the commands. It creates build.sbt and it contains the same values we typed in sbt session. Other directories like target and project are of little consequence to us.
My project directory contains following sub directories
Imgae
This is how my my build.sbt files looks like after executing the commands in sbt session

name :="hello"

scalaVersion :="2.9.2"

version :="1.0"

One thing that has also changed from the past is that the src directories are not created by default. I used the sbteclipse plugin to do it for me.
Create file plugins.sbt inside project and add the following contents :

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

Execute the following command.

D:\hello>sbt eclipse

After execute eclipse command, following directory created as following
DirImage

It creates common source directories like src/main/scala, src/main/test, etc and also eclipse project files.

D:\hello\src>dir
 Volume in drive D is New Volume
 Volume Serial Number is A83C-E41D
 Directory of D:\hello\src
01/21/2013  03:36 PM    <DIR>          .
01/21/2013  03:36 PM    <DIR>          ..
01/21/2013  03:36 PM    <DIR>          main
01/21/2013  03:36 PM    <DIR>          test
               0 File(s)              0 bytes
               4 Dir(s)   5,875,458,048 bytes free

The project is ready to imported in Eclipse IDE.That is all we need for creating Scala project using the latest SBT. We now know how to create an Scala project using SBT.

About anuj2013

Software Consultant At Knoldus Software LLP
This entry was posted in Scala and tagged , . Bookmark the permalink.

4 Responses to Creating Scala Project With SBT 0.12

  1. Pingback: Multi Project Builds with SBT 0.12 for Scala Project | Knoldus

  2. Ramesh says:

    thanks , that was helpful :)

  3. Unknown says:

    Is there a way to pass version :=”1.0″ along with command line? We use the command sbt one-jar to build our project, which will create a jar file, which will have name projectname_1.0_scalaversion.jar , If i can pass version in the command line itself, I can calculate the number of commits to my git repo and set that as a version: argument, that way I can name my jar better.

  4. chuan says:

    cool, helpful

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s