Productively Distracted
Posted Monday February 13, 2012 around 11:28 PM

Edit 8/5/2012: The original intention when I wrote this, was to have a 3 part series working up to a WebSockets server. However over the last few months I dropped my use of scala in favor of clojure and haven't looked back. So this series will probably not be finished unless for some reason I start working in scala again. Sorry.


This series is a quick overview of getting a very basic scala application running with SBT and the netty library. A basic working knowledge of Scala and SBT is assumed. Part 1 covers project setup. Part 2 will cover a very basic HTTP server and part 3 will be a simple WebSockets server.

As always when creating an SBT project, make a new directory and a basic build.sbt file:

$ mkdir nettify
$ cd nettify
$ touch build.sbt
Download

In my build.sbt I have put the following:

organization := "com.bigjason"

name := "nettify"

version := "0.1.0-SNAPSHOT"
Download

As a good practice I also like to specify explicitly the SBT version. So make a build.properties file as well:

$ mkdir project
$ echo 'sbt.version=0.11.2' > project/build.properties
Download

You should now be able to compile your new application (although there is nothing in it yet).

$ sbt compile
[info] Loading project definition from /nettify/project
[info] Set current project to nettify (in build file:/nettify/)
[info] Updating {file:/nettify/}default-7e1bad...
[info] Resolving org.scala-lang#scala-library;2.9.1 ...
[info] Done updating.
[success] Total time: 0 s, completed Feb 13, 2012 11:40:43 PM
Download

With that we are ready to add a dependency to the netty library. Edit your build.sbt and add the following lines (being sure to include a blank line between each line):

libraryDependencies ++= Seq("org.jboss.netty" % "netty" % "3.2.7.Final")

resolvers ++= Seq("jboss repo" at "http://repository.jboss.org/nexus/content/groups/public-jboss/")
Download

Now run sbt update from the command prompt and you should see something like this:

$ sbt update
[info] Loading project definition from /nettify/project
[info] Set current project to nettify (in build file:/nettify/)
[info] Updating {file:/nettify/}default-7e1bad...
[info] Resolving org.scala-lang#scala-library;2.9.1 ...
[info] Resolving org.jboss.netty#netty;3.2.7.Final ...
[info] Done updating.
[success] Total time: 0 s, completed Feb 13, 2012 11:45:52 PM
Download

You are now ready to start using the netty library. To prove the library is indeed installed you could make a quick version dump with something like this in /nettify/src/main/scala/com/bigjason/main.scala:

package com.bigjason

object Main extends App {
    println("Netty Version: " + org.jboss.netty.util.Version.ID.toString)
}
Download

Run it from the command line with sbt run and you will see something like:

sbt run
[info] Loading project definition from /nettify/project
[info] Set current project to nettify (in build file:/nettify/)
[info] Compiling 1 Scala source to /nettify/target/scala-2.9.1/classes...
[info] Running com.bigjason.Main
Netty Version: 3.2.7.Final-0b5abec
[success] Total time: 4 s, completed Feb 13, 2012 11:54:23 PM
Download

This is a relatively basic process. In part 2 we will write a very basic HTTP server. The working code for this tutorial can be found on github under the tag part1.

blog comments powered by Disqus