From zero to a Scala SBT project
with Eclipse / Intellij IDEA support
18 July 2013
Get started with a simple real world structure for a simple Scala project
Manual setup
- Install sbt from here
- Create a new folder for your project, e.g.:
mkdir myProject
andcd myProject
- Create the project folder structure:
mkdir -p src/main/scala
(Mac/Linux)mkdir src\main\scala
(Windows)
- 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)
- To allow managing dependencies, project name, Scala version etc, create a file named
build.sbt
in your project root (e.g. inmyProject/build.sbt
) for example:
name := "hello"
version := "1.0"
scalaVersion := "2.10.2"
- Type
sbt run
to comple + run the project (Should print “Hi!” to the console) - 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)
- Install Scala from here (Optional, sbt will install scala for you)
- Install sbt from here
- Install conscript from here
- Install giter8:
cs n8han/giter8
- Create a new folder for your project:
mkdir myProject
andcd myProject
- Use a templated project, e.g.:
g8 typesafehub/scala-sbt
- Answer the interactive prompt questions (use default options)
- cd to it
cd myProject\scala-project
- Run
sbt run
(should compile, then printHello, Scala Project
)
IDE support (Eclipse / Intellij)
- Open
$ ~/.sbt/plugins/build.sbt
(Mac/Linux) or%userprofile%\.sbt\plugins\build.sbt
(Windows) - 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)
- for Eclipse, in the project folder, type
sbt eclipse
, if you want to download sources addwith-source=true
e.g.sbt eclipse with-source=true
- To open the project in Eclipse: File -> Import -> Existing Projects into Workspace
- 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