Play 2 Scala Console on Heroku

I’ve been working on a Play 2 application that I’ll be using for the Grails vs Play Smackdown at ÜberConf next week. The app is running in production on Heroku but since I don’t have an admin UI yet, I needed a quick and easy way to create a new entity. I could have gone straight to the database but thought it would be better to run Play 2’s Scala Console on Heroku and then just run some arbitrary Scala code. The Scala Console in Play 2 is really just the Scala REPL in SBT but it allows you to interact with a Play application.

I’ll walk you through how to use the Play 2 Scala Console on Heroku. If you’d like to follow along, grab the play2bars app by running:

git clone https://github.com/jamesward/play2bars.git
cd play2bars
git checkout java-ebean

To run the console locally, run:

play -DapplyEvolutions.default=true console

You will see something like:

[info] Loading project definition from /home/jamesw/Desktop/play2bars/project
[info] Set current project to play2bars-java (in build file:/home/jamesw/Desktop/play2bars/)
[info] Updating {file:/home/jamesw/Desktop/play2bars/}play2bars-java...
[info] Resolving org.hibernate.javax.persistence#hibernate-jpa-2.0-api;1.0.1.Fin                                                                                [info] Done updating.                                                        
[info] Compiling 4 Scala sources and 3 Java sources to /home/jamesw/Desktop/play2bars/target/scala-2.9.1/classes...
[warn] Note: /home/jamesw/Desktop/play2bars/app/models/Bar.java uses unchecked or unsafe operations.
[warn] Note: Recompile with -Xlint:unchecked for details.
[info] Starting scala interpreter...
[info] 
Welcome to Scala version 2.9.1.final (OpenJDK 64-Bit Server VM, Java 1.7.0_03).
Type in expressions to have them evaluated.
Type :help for more information.
 
scala>

Start a Play app from the Scala console:

new play.core.StaticApplication(new java.io.File("."))

You will see something like:

[info] play - database [default] connected at jdbc:h2:mem:play
[info] play - Application started (Prod)
res0: play.core.StaticApplication = play.core.StaticApplication@2e338c56

Now you can interact with the Play application. Lets create a new “Bar” entity:

var bar = new models.Bar()
bar.name = "foo bar"
bar.save

And now you can query the “Bar” entities:

import scala.collection.JavaConversions._
models.Bar.find.all.foreach(bar => println(bar.name))

So that’s pretty cool, right?

If you want to run this example on Heroku, then install the Heroku Toolbelt and run:

heroku create -s cedar

And push the application to Heroku:

git push heroku java-ebean:master

Once the application is built and deployed on Heroku, check to make sure it works:

heroku open

To run the Scala console on Heroku, run:

heroku run bash
java -DapplyEvolutions.default=true -Ddb.default.driver=org.postgresql.Driver -Ddb.default.url=$DATABASE_URL -jar ~/.sbt_home/bin/sbt-launch-0.11.3-2.jar

SBT will resolve some dependencies and then you will be in the SBT console. From there run:

set fullClasspath in Compile += Attributed.blank(file("target/staged/*"))
console

Now you will be in the Scala REPL for your Play app on Heroku. So you can run things like:

new play.core.StaticApplication(new java.io.File("."))
var bar = new models.Bar()
bar.name = "foo bar"
bar.save

Reload your app on Heroku in the browser and you should now see the new Bar listed on the page!

Let me know if you have any questions.