Thursday, 21 April 2011

Setup Scala with SBT in IntelliJ IDEA

Download Dependencies

Setup 

  • Setup SBT according to this page (Launching Sbt section)
    http://code.google.com/p/simple-build-tool/wiki/Setup
  • Add Scala/SBT plugins to IntelliJ IDEA
    • Go to File/Settings/Plugins, choose Available tab, right click on Scala plugin and click on 'Download and Install'. Do the same for SBT plugin.
    • Restart IntelliJ IDEA
    • Go to File/Settings/SBT and set location of your SBT jar file and if you're on windows behind firewall add proxy info to vm params (-Dhttp.proxyHost=<proxy_server> -Dhttp.proxyPort=<proxy_port>)

Create Project

  • Create new Folder ScalaProject
  • Run sbt command from that folder, choose 'y' to create new project (Name: ScalaProject, Organization: test) 
  • Configure sbt-idea plugin (https://github.com/mpeltonen/sbt-idea) - you only need to do it once
    • From sbt prompt run the following (including * character)
        > *sbtIdeaRepo at http://mpeltonen.github.com/maven/
        > *idea is com.github.mpeltonen sbt-idea-processor 0.4.0
        > update
  • Create idea project files
      > idea
  • Open the project in IntelliJ IDEA
  • Configure scala plugin file for sbt-idea
    • Create directory ScalaProject/project/plugins
    • Create new file Plugins.scala in ScalaProject/project/plugins folder 
import sbt._ 
class Plugins(info: ProjectInfo) extends PluginDefinition(info) {
  val sbtIdeaRepo = "sbt-idea-repo" at "http://mpeltonen.github.com/maven/"
  val sbtIdea = "com.github.mpeltonen" % "sbt-idea-plugin" % "0.2.0"
}  
  • Configure sbt project definition file
    • Create directory ScalaProject/project/build
    • Create scala class file ScalaProject in ScalaProject/project/build folder
import sbt._ 
class ScalaProject(info: ProjectInfo) extends DefaultProject(info) with IdeaProject {
  val scalatest = "org.scalatest" % "scalatest" % "1.3"
}      
  • Refresh project files   
    • Launch SBT Console (bottom of the screen) and press Run (green arrow) 
    • From SBT prompt run the following
      > update
      > clean
      > compile
      > idea   
    • Reload the project

Hello World example

  • Create new scala class HelloWorld in src/main/scala
object HelloWorld {
  def main(args: Array[String]){
    println(hello)
  } 
  def hello:String = {
    return "Hello World!";
  }
}  
  • Create new scala test class HelloWorldSpec in src/test/scala
import org.scalatest.FlatSpec
import org.scalatest.matchers.ShouldMatchers
class HelloWorldSpec extends FlatSpec with ShouldMatchers {
  "Hellow World" should "show Hello World" in {
    HelloWorld.hello should equal("Hello World!")
  }
}      

Testing Hello World example

  • Launch SBT Console to run the examples
  • From SBT prompt run the following
    > run (searches for classes with main(args) method and executes them)
    > test (searches for test classes and execute them)
    > ~test (runs tests continuously as soon as it detects changes), press ENTER to exit

Enjoy Scala!

This seems like a good tutorial for beginners
http://www.scala-lang.org/docu/files/ScalaTutorial.pdf

No comments:

Post a Comment