Saturday, October 11, 2014

Scala Simple Build Tool

So, recently as I was working through my scala book, we got to the first chapter where we used 3rd party dependencies - the testing chapter. Now, for the simple examples in the chapter I just downloaded the jars needed and put them on my classpath in Eclipse and got everything working just fine. However, I was interested in looking for a dependency manager for scala and that's when I stumbled up the scala sbt project. With this finding, I decided to mimic the tests done in the testing chapter, but this time use scala sbt to manage my dependencies and put them on the classpath for me (I've pushed this small project to my github account).

Setting Up First Project In Eclipse

After I installed sbt, geting my first project up and running was not trivial so I wanted to walk through the steps I did to get a project from initial commit in github to fully working.
Note: I copied a lot of steps from a SO answer, however it was not exactly the same so I wanted to type up the steps I did.
  • Create your new repo on Github and clone it somewhere locally
  • Add the sbt eclipse plugin to your list of global sbt plugins
    • Now this step was a little confusing for me because the instructions were for *NIX style setups. They say to just add the line:
    •  addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "2.5.0")  
      To the file:
    •  ~/.sbt/0.13/plugins/plugins.sbt  
    • However, I'm working on Windows so obviously this path doesn't exist. Though, eventually I figured it out and needed to add it to the local user's file. So, in my case the file was located at:
    •  C:\Users\Dan\.sbt\0.13\plugins\plugins.sbt  
  • Once you add this plugin, open a command prompt to where you cloned your repo and type the following:
  •  sbt eclipse  
    • This will generate all of the eclipse project, classpath, and other various files needed to work properly in Eclipse
  • Import the project into Eclipse and start coding:
    • File --> Import --> General --> Existing Projects into Workspace
And that's it, now you can start working on your scala project inside of eclipse and get all of the helpfulness from the scala Eclipse plugin.


Once you have your project initialized and ready to go in Eclipse, it's time to make our build.sbt file. This file serves the same purpose as the file "pom.xml" for maven projects - it defines your jar name, version, dependencies, etc etc. Now, my build.sbt is very simple, however there a lot of different things you can do in this file and they are explained on the sbt site.

Adding Dependencies

Now, adding dependencies to your project is clearly defined on the sbt site, however there was one issue I ran into that I wanted to write down. By default, Eclipse will not reload the dependencies when you add them to your build.sbt file. Instead, you need to reload the Eclipse definition with the sbt plugin we added earlier. This is done by just typing a couple of commands into a command prompt and refreshing the Eclipse project - the steps to do this are listed on this SO answer.

Change SBT Download Location

By default, sbt will download jars to folder in the user's directory, in my case it was:
However, I did not want my downloaded jars to reside there because that meant each user would have their own copies of all of the scala jars used. So, I found yet another SO answer that showed me how to change the download location. I simply had to edit my sbtconfig.txt file, which was located at - C:\Program Files (x86)\sbt\conf\sbtconfig.txt - and had to add the following 2 lines:
So now all jars will download to the folder - S:\SBTRepo - for all users and not to each user's individual local files.

Now, all of these steps might seem like a lot, but it really didn't take that long - there was definitely more investigate than implementation. It's just that this was my first time building a dependency-managed scala project so inevitably took the longest to get properly setup. And once all of these steps were done, my scala project was up and running perfectly in Eclipse and I was able to test everything properly on the command line.