This post originated from an RSS feed registered with Scala Buzz
by Heiko Seeberger.
Original Post: How to setup a Scala project with SBT and IDEA
Feed Title: Heiko Seeberger
Feed URL: http://hseeberger.github.io/atom.xml
Feed Description: About programming and other fun things
Today I helped my mate Bernd Kolb setting up a simple Scala project. He had been struggling far too long with various options like Maven, SBT, Eclipse and Intellij IDEA. If I remember correctly it only took a 30 minutes conf call to setup a simple Scala and Lift project with SBT and IDEA. But there are quite a few things to know in order to be successful which I am going to describe here in a step-by-step tutorial. I hope you find it useful. Please let me know any issues.
Why SBT and IDEA?
Why not Maven and Eclipse? Or Ant and Netbeans? Or ...? First, we have to make a choice. Second, SBT and IDEA are currently the best build tool and the best IDE for Scala. SBT is written specifically for Scala and offers very fast compilation, interactive mode for continuous compilation, testing, etc., integration for Scala test tools like specs and ScalaTest, cross compiling against different Scala versions and many more. While the Scala IDE for Eclipse recently has made nice progress and the Netbeans plugin looks good, IDEA still offers the best Scala plugin with many features known from a Java IDE, e.g. refactroings, code completion, code navigation, etc.
Preparations
Java 5 or 6 is assumed. Get the SBT launcher and follow the instructions to set it up. Install IDEA 9.0.3 and use its plugin manager to install the Scala plugin. Here we go!
1. Create a SBT project
Create a folder for your project, cd there and run SBT. You will be asked the following questions. Answer like I did or chose different values (except for the Scala version).
Project does not exist, create new project? (y/N/s) y
Name: simple
Organization: localhost
Version [1.0]:
Scala version [2.7.7]: 2.8.0
sbt version [0.7.4]:
Now SBT will download Scala 2.7.7 as well as some other libraries for internal use. And it will download Scala 2.8.0 which is used to build your project.
As soon as the downloads are finished, you are in SBT's interactive mode. At the prompt enter compile, then ~test (press enter to leave the triggered mode) and then help to make yourself familiar with the features.
Looking at your project in the file system, you will notice a layout similar to Maven: Scala sources go into src/main/scala, Scala tests go into src/test/scala and artifacts go into target. If your project looks different from the one at the right, you probably did not run compile or test. But as it is important to run your project against the same version as it was compiled against, there is a Scala_2.8.0 folder between the target and the classes, test-classes etc.
2. Create an IDEA project
Now you have to create an IDEA project on top of your SBT project.
Choose File > New Project ..., select Create project from scratch and continue.
Then pick your project folder for Project files location and continue.
Then enter src/main/scala as path for the source directory and continue.
Now it is getting a little tricky!
Select Scala from the list of technologies.
Ignore the erroneous combo for the Version.
Choose Pick files from disk and select scala-compiler.jar and scala-library.jar form project/boot/scala-2.8.0/lib.
Then change the Name to scala-2.8.0 and click Finish.
3. Adjust IDEA project settings
Now you have make some adjustments to align SBT's and IDEA's project layout.
Choose Modules on the left and navigate to the Sources tab. Add src/main/resources to the Sources (blue) and src/test/scala and src/test/resources to the Test Sources (green). Exclude (red) .idea, lib, project/boot and target.
Next go to the Paths tab and select target/scala_2.8.0/classes for the Output path and target/scala_2.8.0/test-classes for the Test output path.
Then choose the Scala facet from the mid column (below the one and only module simple) and unchek Use Scala compilers ....
That's it! Now you are ready to go.
4. Give it a try
In IDEA go to the src/main/scala folder and choose New > Scala Class from the context menu. Call it Hello and make it an Object.
In the editor delete the package statement and enter the following code:
object Hello {
def main(args: Array[String]) {
println("Hello!")
}
}
From the context menu choose Run Hello.main(). IDEA will build your project and then run this object.
After that go to SBT and enter ~run. Watch the output and look for the Hello! message. Then change the message in the code, e.g. to Hello, world!. As you have told SBT to use triggered run by prefixing whith the tilda, your project will be incrementally built and the Hello object run again: Watch the output.