Starting Akka Project With SBT 0.10


I was starting with Akka project with SBT but found that the latest SBT is quite different from before.

I tried to create AKKA project with latest SBT but got stuck. Old SBT used to ask to create a new project in case it did not find any in the directory. With new SBT it is not the case. If you want to know how to go about creating new Akka project with SBT read on.

After installing SBT if we type in sbt in the command prompt in an empty directory, this is what we are likely to see.

In order to create project execute the following commands in the sbt session.

> set name := "AkkaQuickStart"
> set version := "1.0"
> set scalaVersion := "2.9.0-1"
> session save
> exit

We should get the following output if we type the above mentioned commands in sbt session.

SBT creates files and directories when we executed the commands. It creates build.sbt and it contains the same values we typed in sbt session. Other directories like target and project are of little consequence to us.

Project directory will become important later when we will try to add sbteclipse plugin. My project directory contains the following subdirectories and files.

This is how my my build.sbt files looks like after executing the commands in sbt session

name := "AkkaQuickStart"

version := "1.0"

scalaVersion := "2.9.0-1"

One thing that has also changed from the past is that the src directories are not created by default. I used the sbteclipse plugin to do it for me.

Create directory plugins inside project and add build.sbt to it. Add following contents to it:

resolvers += {
  val typesafeRepoUrl = new java.net.URL("http://repo.typesafe.com/typesafe/releases")
  val pattern = Patterns(false, "[organisation]/[module]/[sbtversion]/[revision]/[type]s/[module](-[classifier])-[revision].[ext]")
  Resolver.url("Typesafe Repository", typesafeRepoUrl)(pattern)
}

libraryDependencies <<= (libraryDependencies, sbtVersion) { (deps, version) => 
  deps :+ ("com.typesafe.sbteclipse" %% "sbteclipse" % "1.3-RC1" extra("sbtversion" -> version))
}

Execute the following command.

sbt "eclipse create-src"

It creates common source directories like src/main/scala, src/main/test, etc and also eclipse project files.

The project is ready to imported in Eclipse IDE. We also require Akka dependencies in our project. This can be done by adding resolvers and library dependencies to build.sbt at the top level. Add the following entries in build.sbt:

name := "AkkaQuickStart"

version := "1.0"

scalaVersion := "2.9.0-1"

resolvers += "Typesafe Repository" at "http://repo.typesafe.com/typesafe/releases/"

libraryDependencies ++= Seq(
  "se.scalablesolutions.akka" % "akka-actor" % "1.1.3",
  "se.scalablesolutions.akka" % "akka-typed-actor" % "1.1.3",
  "se.scalablesolutions.akka" % "akka-amqp" % "1.1.3",
  "se.scalablesolutions.akka" % "akka-testkit" % "1.1.3"
)

Whenever we add new dependencies we must execute “sbt eclipse” to regenerate the eclipse project configuration files. Now in order that code for Akka in our projects compiles correctly in Eclipse we execute the following command:

sbt eclipse

We have a skeleton project with all dependencies and build scripts ready for an AKKA project. Lets create a simple Echo Akka Actor to validate that our configurations work correctly.

The object Application starts EchoActor passes message to it and then stops it. EchoActor prints the message received from Application.

package com.meetu.akka

import akka.actor.Actor.actorOf
import akka.actor.Actor

object Application extends App {

  val actor = actorOf(new EchoActor)
  actor.start
  actor ! "Hello Akka Echo Actor"
  actor.stop
}

/**
 * An Actor that echoes everything you send to it
 */
class EchoActor extends Actor {
  def receive = {
    case msg => {
      println(msg)
    }
  }
}

If all is fine the code will compile without any issues but before we run this Application as scala application we need to download Akka from here and set AKKA_HOME in PATH variable.

After AKKA_HOME points to the right directory we can run the application either from Eclipse or from SBT. For Eclipse run it as Scala Application. For SBT use the command provided below. You should see the “Hello Akka Echo Actor” message in the console.

sbt "run-main com.meetu.akka.Application"

That is all we need for creating Akka project using the latest SBT. We now know how to create an Akka project using SBT and also to add dependencies to it. We can also generate Eclipse IDE specific project files using sbteclipse.

I have created a sample project AkkaExperiments which has the code for starting Akka project using SBT.

This entry was posted in Scala and tagged , , . Bookmark the permalink.

2 Responses to Starting Akka Project With SBT 0.10

  1. For your resolver, I think you should use

    resolvers += “Akka Repository” at “http://akka.io/repository”
    instead of
    resolvers += “Typesafe Repository” at “http://repo.typesafe.com/typesafe/releases/”

    however both work.

    • Sounds good. Thanks for the feedback. I see that your company too is working on Scala in HK. Good to be connected. Send me a ping whenever you feel that we can work together. Regards | Vikas

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s