How to create your first Play Framework 2 web application and deploy it to Heroku

It’s easy and fast to get started with Play 2, and this is how you get going.
For this example I’ll be using Eclipse.

Play framework

Set up the Play Framework on your computer

  1. Download the latest release of Play from www.playframework.org/download
  2. Add Play to the path (mac) or environment variables (win).
    (Mac) Add the following line to your .bash_profie:
    export PATH=/Users/user1/Documents/play-2.0/:$PATH
    (Win) Add the following line to your environment variable:
    c:play-2.0;
  3. Make sure Play works by typing:
    $ play help
           _            _
     _ __ | | __ _ _  _| |
    | '_ | |/ _' | || |_|
    |  __/|_|\____|\__ (_)
    |_|            |__/ 
    
    play! 2.0, http://www.playframework.org
    
    Welcome to Play 2.0!
    
    These commands are available:
    -----------------------------
    license            Display licensing informations.
    new [directory]    Create a new Play application in the specified directory.
    
    You can also browse the complete documentation at http://www.playframework.org.

Create your first Play application

  1. To create your project, type the following command:
    $ play new kth-test-app
           _            _
     _ __ | | __ _ _  _| |
    | '_ | |/ _' | || |_|
    |  __/|_|\____|\__ (_)
    |_|            |__/ 
    
    play! 2.0, http://www.playframework.org
    
    The new application will be created in /Users/hoyce/repos/kth-test-app
    
    What is the application name?
    > kth-test-app
    
    Which template do you want to use for this new application? 
    
      1 - Create a simple Scala application
      2 - Create a simple Java application
      3 - Create an empty project
    
    > 2
    
    OK, application howto is created.
    
    Have fun!
  2. We can now test the application by running these commands in your application directory: (in my case /Users/hoyce/repos/kth-test-app) :
    $ cd kth-test-app/
    $ play
    [info] Loading project definition from /Users/hoyce/repos/kth-test-app/project
    [info] Set current project to kth-test-app (in build file:/Users/hoyce/repos/kth-test-app/)
           _            _
     _ __ | | __ _ _  _| |
    | '_ | |/ _' | || |_|
    |  __/|_|\____|\__ (_)
    |_|            |__/ 
    
    play! 2.0, http://www.playframework.org
    
    > Type "help play" or "license" for more information.
    > Type "exit" or use Ctrl+D to leave this console.
    
    [kth-test-app] $
    [kth-test-app] $ run
    
    --- (Running the application from SBT, auto-reloading is enabled) ---
    
    [info] play - Listening for HTTP on port 9000...
    
    (Server started, use Ctrl+D to stop and go back to the console...)
  3. Then go to http://localhost:9000 to make sure the application is up and running. Port 9000 is the default port for Play but you can select another port by adding the port number to the run command.
    [kth-test-app] $ run 9010

Set up your Play project in Eclipse

  1. Eclipse IDETo start coding with Eclipse we need to create some Eclipse specific files and create the actual project inside Eclipse. Run the following command in your application directory: (in my case /Users/hoyce/repos/kth-test-app)
  2. $ play eclipsify
  3. Start Eclipse and choose:
    File -> New -> Java Project
    Uncheck "Use default location"
    Select your project folder (/Users/hoyce/repos/kth-test-app) and click "Finish"

Now we have our application and the project in Eclipse up and running.

Heroku

Deploy the application to Heroku

  1. To be able to deploy your application to Heroku you need to create an account.
    Create your Heroku account here: https://api.heroku.com/signup
  2. Download and install Heroku Toolbelt which gives you the tools you need to create and managing Heroku applications.
  3. Login and set up your keys with the following command:
    $ heroku login
    Enter your Heroku credentials.
    Email: yourmail@mail.com
    Password (typing will be hidden):
    Authentication successful.
  4. Initialize your application with Git by using the following commands:
    $ git init
    $ git add .
    $ git commit -m "Initialize project"
  5. Create your application on heroku:
    $ heroku create --stack cedar
    
    Creating high-water-7567... done, stack is cedar
    http://high-water-7567.herokuapp.com/ | git@heroku.com:high-water-7567.git
  6. Now that you have your application environment on Heroku you can push your changes using this command:
    $ git push heroku master
  7. Your Play application is now live!
    Open your Play! application on Heroku in a web browser:

    $ heroku open

Other useful commands when working with Heroku:

$ heroku logs		// View the logs
$ heroku ps 		// Process
$ heroku config 	// Config parameters used in the Procfile

Useful plugins when working with Heroku:

Heroku accounts plugin: https://github.com/ddollar/heroku-accounts
Heroku SQL console plugin: https://github.com/ddollar/heroku-sql-console

Profilbild
Jag heter Niklas och arbetar som tema lead i vårt webbteam som är ansvarigt för bl.a. www.kth.se, bloggplattformar, den centrala söktjänsten, profilsidor m.m. Jag använder denna blogg för att dela med mig av information som handlar om nya projekt och system som vi utvecklar för våra användare på universitetet.

Lämna ett svar

E-postadressen publiceras inte. Obligatoriska fält är märkta *