18 July 2013

Get started with a simple real world structure for a simple Scala project

Manual setup

  1. Install sbt from here
  2. Create a new folder for your project, e.g.: mkdir myProject and cd myProject
  3. Create the project folder structure:
    • mkdir -p src/main/scala (Mac/Linux)
    • mkdir src\main\scala (Windows)
  4. Create a file in src/main/scala e.g.
    • echo 'object Hi { def main(args: Array[String]) = println("Hi!") }' > hw.scala (Mac/Linux)
    • echo object Hi { def main(args: Array[String]) = println("Hi!") } > hw.scala (Windows)
  5. To allow managing dependencies, project name, Scala version etc, create a file named build.sbt in your project root (e.g. in myProject/build.sbt) for example:
  name := "hello"
  
  version := "1.0"
  
  scalaVersion := "2.10.2"
  1. Type sbt run to comple + run the project (Should print “Hi!” to the console)
  2. Type sbt ~compile to have the project continually compile when files change

Download this project skeleton as a zip file

Setup using giter8

(All of the following have instructions for Windows / Mac / Linux)

  1. Install Scala from here (Optional, sbt will install scala for you)
  2. Install sbt from here
  3. Install conscript from here
  4. Install giter8: cs n8han/giter8
  5. Create a new folder for your project: mkdir myProject and cd myProject
  6. Use a templated project, e.g.: g8 typesafehub/scala-sbt
  7. Answer the interactive prompt questions (use default options)
  8. cd to it cd myProject\scala-project
  9. Run sbt run (should compile, then print Hello, Scala Project)

IDE support (Eclipse / Intellij)

  1. Open $ ~/.sbt/plugins/build.sbt (Mac/Linux) or %userprofile%\.sbt\plugins\build.sbt (Windows)
  2. Add the following lines (the empty line in between is important)
  addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "2.2.0")

  addSbtPlugin("com.github.mpeltonen" % "sbt-idea" % "1.4.0")

(see https://github.com/typesafehub/sbteclipse and https://github.com/mpeltonen/sbt-idea for more details)

  1. for Eclipse, in the project folder, type sbt eclipse, if you want to download sources add with-source=true e.g. sbt eclipse with-source=true
  2. To open the project in Eclipse: File -> Import -> Existing Projects into Workspace
  3. for Intellij IDEA, in the project folder, type sbt gen-idea
NOTE: Intellij just released a new early release SBT plugin (July 17, 2013) - which can import SBT projects without the need for sbt gen-idea



blog comments powered by Disqus