Beginner's guide to SBT 0.10 and IDEA

Luigi Plinge picture Luigi Plinge · Aug 12, 2011 · Viewed 7.2k times · Source

I'm new to SBT and am unsure how to get a project started. Can someone point me to a beginner's guide to creating a Hello World type project, or give me some clues?

My preferred IDE is IDEA. I have run sbt-idea according to the instruction on the IDEA Plugins page. At the moment I'm a bit confused because

  • there are no source folders created - where / how am I supposed to create them and how will SBT know where to look?
  • why is it trying to use Scala 2.8.1, when I have already put scalaVersion := "2.9.0" in the build.sbt file? This means IDEA doesn't recognize object HelloWorld extends App {}.
  • the instructions on the plugins page above suggest changing the Before Launch options of "a Run Configuration (including the Default Run Configuration)". There are 13 different default configurations for different things listed - which one to change? Should I be creating a new one? Are these default configurations just for this project or will it adversely affect all my other projects that don't use SBT?

Thanks.

Answer

Nick A Miller picture Nick A Miller · Aug 12, 2011

This worked for me:

First get sbt and the gen-idea plugin going...

  1. Download the sbt-launch.jar and create the script for launching it as described on the SBT Github wiki.
  2. Create a directory for your new project, such as (on linux) ~/myCode/myNewProject and change to that directory
  3. Run sbt command. This should download the scala libraries and create a 'project' and 'target' directories.
  4. Change to the 'project' directory.
  5. Create a new file 'build.sbt' in this directory with the following lines, as described on the sbt-idea plugin Github wiki:

    resolvers += "sbt-idea-repo" at "http://mpeltonen.github.com/maven/"
    
    addSbtPlugin("com.github.mpeltonen" % "sbt-idea" % "1.0.0")
    
  6. Change back to your main project directory such as ~/myCode/myNewProject. Run sbt. It should download the gen-idea plugin.

  7. From the sbt console (which should be running now), run the gen-idea command. It should create the IDEA project directories. For me, it also emits copious warnings.

Now get the IDEA SBT console plugin going...

  1. Open IDEA and install the "SBT" plugin from the plugin manager and restart IDEA. (Note this is the IDEA plugin, not the sbt plugin described above.) Configure the SBT plugin as described on its wiki (run configurations, location of sbt-launch.jar etc).
  2. Open the freshly generated IDEA project in IDEA.
  3. Put your code and other things in the expected default directories as described on the sbt wiki under 'Directory Layout'. You need to create these directories yourself - sbt doesn't create them automatically. The 'src' and 'test' directories should be at the same level as the 'project' and 'target' directories that sbt created.
  4. Make up a new 'build.sbt' file and put it in ~/myCode/myProject (or whatever you called it). Since I am just figuring out sbt, mine is simple so far - just nominates scalatest as a dependency and uses Scala 2.9:

    name := "myProject"
    
    version := "0.1"
    
    organization := "me"
    
    libraryDependencies += "org.scalatest" % "scalatest_2.9.0" % "1.6.1"
    
    scalaVersion := "2.9.0"
    
  5. Enter the reload command in the SBT console at the bottom of the IDEA screen. It should download scalatest and Scala 2.9 for you. Maybe you need to run 'update' too.