Getting Started with Play Framework on Heroku

UPDATE: This tutorial is for Play 1.x (an old version). Check out my Play 2 Tutorial if you want to get started with the latest stuff.

Last week Heroku announced that you can now run Java apps on Heroku. Today Heroku announced that you can also easily run Play Framework apps on Heroku! Here’s a quick guide to getting started with Play! on Heroku:

  1. Install the heroku command line client on Linux, Mac, or Windows.
  2. Install git and setup your SSH key
  3. Install Play! version 1.2.3
  4. Login to Heroku from the command line:

    heroku auth:login
  5. Create a new Play! app:
    play new play_hello_world
    cd play_hello_world
  6. Run the app locally to test it:
    play run --%production
  7. Create a git repo, add the files, and commit:
    git init
    git add app conf lib public test
    git commit -m init
  8. Create a new app on Heroku:
    heroku create -s cedar
  9. Push the app to Heroku:
    git push heroku master
  10. Open the app in your browser:
    heroku open

That’s it! If you want to learn more about Heroku, check out the Heroku for Java Workbook and the Heroku Dev Center. And if you are at Dreamforce 2011 then check out Felipe Oliveira’s session on “Introducing Play! Framework: Painless Java and Scala Web Applications” on Wednesday at 3:30pm.

Let me know what you think and if you have any questions.

Setup Play Framework with Scala in IntelliJ

UPDATE: This is only for Play 1.x with Scala. Getting Play 2 projects (Java or Scala) working in IDEA is now as easy running “play idea” and then opening the project.
Update 2: I’ve posted a new video showing how to create and run Play2 apps in IntelliJ.

Yesterday at the Programming Summer Camp a group of us were working together to learn the Play Framework. Once we were able to get the basics working we wanted to get everything working in IntelliJ IDEA. Due to a lack of good documentation on the subject things did not go smoothly until we figured out the right “magical incantations”. We did eventually get it working so I wanted to document the steps we took.

  1. Add Scala support to Play:
    play install scala
  2. Create a new Play project with Scala support:
    play new foo --with scala
  3. Have Play create an IntelliJ Module Descriptor:
    play idealize foo
  4. Run the app:
    play run foo
  5. Access the app in a browser to generate some source files we will use later:
  6. Shutdown the Play server.
  7. Currently the Play Scala Module (version 0.9.1) only supports Scala 2.8.1 so download and extract that version.
  8. Create a new project (from scratch) in IntelliJ. The location should be the same as the directory where the Play project was created. Un-check the “Create module” option.
  9. In the Project Structure window, with “Modules” selected, click the “+” button to add a new module to the project.
  10. Select the “Import existing module” option and then point it to the generated .iml file.
  11. With the module now selected, select the content root block for the module (mine is /home/jamesw/projects/foo). Then select the “tmp/generated” directory in the tree on the right and press the “Sources” button. The HTML template pages are converted to .scala source files by Play and we need these source files to be included in the project along with the regular source files in the app directory.
  12. Select “Global Libraries” in the Platform Settings on the left and add a new Java Library named “scala-compiler-2.8.1”. Then press the “Attach Classes…” button and navigate to the “lib” directory in your Scala 2.8.1 directory. Then select the “scala-compiler.jar” and “scala-library.jar” files.
  13. Add another Global Library named “scala-library-2.8.1” containing the “scala-dbc.jar”, “scala-library.jar”, and “scala-swing.jar” files.
  14. Press the “Apply” button to save the Global Libraries configuration.
  15. Select “Modules” and then the “Dependencies” tab. Press the “Add…” button and select “Library” to add a new library to the module. Select “scala-library-2.8.1” and then press the “Add Selected” button to add it to the Dependencies.
  16. Select the “Scala” Facet in the module and set the Compiler library to the “scala-compiler-2.8.1” option.
  17. Save the Project Structure by pressing the “Ok” button. Make sure the project now builds without any errors.
  18. To run the Play server from IntelliJ a new Run Configuration must be configured. To create a new Run Configuration select “Run” from the main IntelliJ menu and then select “Edit Configurations”.
  19. Press the “+” button and select “Application” from the list of Run Configuration types.
  20. Set the name to “Play Server”.
  21. Set the Main class to “play.server.Server”.
  22. Set the VM Parameters to:
  23. De-select the “Make” option in the “Before Launch” section.
  24. Press the “Ok” button to save the configuration.
  25. Run the Play Server by selecting “Run” from the “Run” IntelliJ menu and verify that the application still works by opening the application in a browser.

It is so much easier to figure this stuff out with a group of people. That is just one of the many reasons why the Programming Summer Camp is a great event!

Let me know if you have any questions or problems.