onsdag 5 januari 2011

Setting up a SBT Scala project for IntelliJ IDEA

I have previously set up Scala projects using maven and found it to be quite a hassle the first time I set up a project. My main problem was to find a good up-to-date archetype to create my project from, but since then I just copy my stub project any time I want to try out something new.

Since then I have been reading more about Scala and find that SBT seem to be a quite popular tool for handling your project dependencies and builds, so I wanted to try to use SBT instead to see if I find to be a more suitable build/dependency management tool.

My first step was to download (http://code.google.com/p/simple-build-tool/downloads/list), install and setup SBT, where I followed the setup instructions found in http://code.google.com/p/simple-build-tool/wiki/Setup

My main problem with the guide, being a windows user mainly (but running Ubuntu now), was that I was expected to create my own shell script named sbt, and the instructions for it didn't make sense to me. The following was the instructions of what to put in the shell script according to the guide:
java -Xmx512M -jar `dirname $0`/sbt-launch.jar "$@"
But I didn't know what the $0 actually did and didn't get it to work, so I just ended up using the syntax I know for starting jar-files in java
java -Xmx512M -jar /home/myuser/dev/app/sbt/sbt-launch.jar "$@"
So assuming that you also use linux, the sbt shell script should be put somewhere on your path. Myself I have a directory setup under my user folder in /home which is simply called "bin", where I put all scripts I want to be able to run in the shell. This directory I have mapped in my path, so I can write "sbt" independent of where I am in the shell and it will run. Next step is to run the following on your sbt shell script:
chmod u+x ~/bin/sbt

Congratulations, you now have sbt installed and should be able to run it from the shell. But before doing that we are going to create a folder for our project. Open the Shell and change directory to where you want to store your project, then do the following:
mkdir ScalaExperiment
cd ScalaExperiment
sbt
After entering sbt you are asked if you want to create a project and if you answer yes you are asked for information about the project name etc. Just enter the same data as I did in the log below:
Project does not exist, create new project? (y/N/s) y
Name: ScalaExperiment
Organization: org.thoughtmeld
Version [1.0]: 
Scala version [2.8.1]: 
sbt version [0.7.5.RC0]:

Your project is created and ready to be used? Not entirely, unless you want manually set up your IntelliJ IDEA project with this project stub. Which isn't necessary anymore thanks to the sbt-idea plugin found at https://github.com/mpeltonen/sbt-idea. All you have to do is write the following lines in sbt
*sbtIdeaRepo at http://mpeltonen.github.com/maven/
*idea is com.github.mpeltonen sbt-idea-processor 0.2.0
This will define a new processor repository and download the sbt-idea processor. So now all you need to do is run the update action and then the idea action to create the idea project files.
update
idea
Now you have a project stub setup using SBT together with some generated IntelliJ IDEA project files, now you can just boot up your IntelliJ IDEA with the scala and sbt plugin installed and open the project. Happy coding!

Inga kommentarer:

Skicka en kommentar