Thursday, April 12, 2012

Get started with Play! framework + scala + deploy to heroku


This is for windows 7 but will probably work on others. There a lot of instructions and tutorials out there including on the official sites but I had to combine them and improvise a little bit to make it work.

Step 1: Install the Typesafe Stack

http://typesafe.com/stack/download

The Typesafe Stack is a modern software platform that makes it easy for developers to build scalable software applications in Java and Scala. It combines the Scala programming language, Akka middleware, Play web framework, and robust developer tools in a simple package that integrates seamlessly with existing Java infrastructure. --typesafe site

Basically everything you need to start developing.

Step 2: Install Heroku toolbelt

https://toolbelt.heroku.com

The toolbelt includes 3 main tools you need to deploy heroku:

  • Foreman - an easy option for running your apps locally
  • Git - revision control and pushing to Heroku
  • Heroku client - CLI tool for creating and managing Heroku apps

--heroku site

Step 3: Create your app

To do this you open the command line and type:
g8 typesafehub/play-scala

At this point you can also follow the instructions on the typesafe site - http://typesafe.com/resources/getting-started/tutorials/getting-started-with-play-scala.html

The first time you run g8 it has to download all kinds of libraries.

Note: if it looks like it's hanging, it may be because your antivirus or firewall  or proxy is blocking java.exe from hitting the internet. I basically turned everything off to get it to work.

After all the downloading and getting ready you should get a dialog like the one below. What worked for me was:

verbatim: press enter
application_secret: whatever
application_name: whatever
play_version: press enter to stay on 2.0

It'll take about 10-15 seconds and then let you know that it worked.

Step 4: Run your app

In command line:
cd someapp
someapp>sbt

Now, if this it's first time running sbt it will again do all kinds of downloading stuff from places and have the same firewall problems as before.

Then you'll see:
[someapp]$

To run the app type "run"

After it finishes loading go to localhost:9000 to see it's up. Hopefully it's up.

Note: i have experienced problems with e-texteditor (the windows textmate). It somehow interferes with port 9000 under certain conditions. I think the thing is to only open it after the app is already running.

Ok, you can turn it off now by going ctrl+d in the command window.

Step 5: Add to GIT

In command:
cd someapp
someapp>git init (will initialize the directory to wrok with git)
someapp>git add . (add all files not ignored by .gitignore)
someapp>git commit -a -m "Initial commit" (commits all files so that they get pushed up to the repository later on. -a=all files, -m=message)

Step 6: Deploy to Heroku

The steps here are:

  • Create a new heroku app
  • Add some files to your project so heroku knows how to deploy it
  • Build your app locally
  • Push up to heroku
  • Check it works

Here we go:

1. Create a new heroku app
From CMD run -> heroku create --stack cedar --buildpack git@github.com:orrgal1/heroku-buildpack-scala.git

I use this buildpack because it removes the ivy cache once it finishes building and reduces the slug size by about 30 MB. Heroku will now create a new app and tell its name. Something Japanese sounding like shiny-mountain-5817. It will also tell you the git repo address for the app. Copy it aside so you can use it later. It'll be git@heroku.com:shiny-mountain-5817 .git.

2. Add some files to your project so heroku knows how to deploy it

A. Make sure your \project folder contains build.properties and that its contents are something like:
sbt.version=0.11.2
Using g8 to create the app should take of that should it should be there.

B. Add a file call build.sbt to you root dir. Copy this content into it:
import com.typesafe.startscript.StartScriptPlugin
seq(StartScriptPlugin.startScriptForClassesSettings: _*)
name := "bix"
version := "1.0"
scalaVersion := "2.9.1"
resolvers += "twitter-repo" at "http://maven.twttr.com"
libraryDependencies ++= Seq("com.twitter" % "finagle-core" % "1.9.0", "com.twitter" % "finagle-http" % "1.9.0")

C. Add another file call build.sbt to you \project dir. Copy this content into it:
resolvers += Classpaths.typesafeResolver
addSbtPlugin("com.typesafe.startscript" % "xsbt-start-script-plugin" % "0.5.1")

D. Add a file called Procfile to your root dir. Copy this content into it:
web: target/start -Dhttp.port=${PORT}

E. Add a file called  dependencies.yml to your \conf dir. Copy this content into it:
# Application dependencies
require:
    - play 2.0

3. Build your app locally
In command, get to your someapp dir and type
sbt clean compile stage

sbt will now download a bunch of jars and stuff to the folder target\staged so you gotta make sure you don't have any firewall problems or it will hang.
And then it'll compile your app.

4.  Push up to heroku
In command, in your someapp dir:
git push git@heroku.com:afternoon-fire-2693.git master

It'll take a few minutes.

5. Now let's test it.
At the end of the push it'll give you the url for your newly deployed app.
Browse to it.

Good luck!