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.
Set up the Play Framework on your computer
- Download the latest release of Play from www.playframework.org/download
- Add Play to the path (mac) or environment variables (win).
(Mac) Add the following line to your .bash_profie:
(Win) Add the following line to your environment variable:
- 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
- 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!
- 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...)
- 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
- To 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)
$ play eclipsify
- 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.
Deploy the application to Heroku
- 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
- Download and install Heroku Toolbelt which gives you the tools you need to create and managing Heroku applications.
- Login and set up your keys with the following command:
$ heroku login Enter your Heroku credentials. Email: email@example.com Password (typing will be hidden): Authentication successful.
- Initialize your application with Git by using the following commands:
$ git init $ git add . $ git commit -m "Initialize project"
- Create your application on heroku:
$ heroku create --stack cedar Creating high-water-7567... done, stack is cedar http://high-water-7567.herokuapp.com/ | firstname.lastname@example.org:high-water-7567.git
- Now that you have your application environment on Heroku you can push your changes using this command:
$ git push heroku master
- 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