Monday, January 21, 2013

[Java Play! / Heroku] Setup Java Play! + Heroku + Eclipse

I'm not a very good developer, I have to admit it.
I always forget, if I don't "play HTML" for a while, how to define a CSS style class, so you can imagine what kind of memory I have (the worst kind), and what kind of problems I have to set an environment to make a new project, if that implies that I have to follow more than 3 steps!.
This post is dedicated to myself of the next month, and I want to show me how to set up a Java Play! Proejct using Heroku and Eclipse, and some other little things.

First, create a new Play! project:
 $ play new my-new-app
              _            _
  _ __ | | __ _ _  _| |
 | '_ \| |/ _' | || |_|
 |  __/|_|\____|\__ (_)
 |_|            |__/

 play! 2.0.3, http://www.playframework.org

 The new application will be created in \playTest\my-new-app

 What is the application name?
 > my-new-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 my-new-app is created.

 Have fun!

Now you have a simple Java Play! project (with a simple controller that says that your new application is ready.
The next step is to tell Heroku that your application is a Play application, by creating the /conf/dependencies.yml
 # Application dependencies

 require:
  - play 2.0.3

And that this application will be a web application (that needs the "play run" script) with the file Procfile ( in the root of the app, and without extension):
 web:    play run --http.port=$PORT $PLAY_OPTS

Now it's time to create a new GIT local repository (that will be pushed to Heroku):
   $ cd my-new-app
   $ git init
   $ git add .
   $ git commit -m "init"

Now it's time of Heroku.
You have to install the Heroku toolbelt (go here for instructions): if you have troubles with SSH keys follow this article).
Create a new Heroku application (it will be available on "http://my-new-app.herokuapp.com"):
   $ heroku create my-new-app

And push your local repository to git:
   $ git push heroku master
   

To open the app directly from the command line (via the browser)
       $ heroku open

If you don't like the name of your Heroku application, simply login to Heroku and change it in the preference panel.

Now install the Eclipse Heroku plugin (see details here).

If you need to set a specific GIT directory (the place where GIT, and Heroku, projects are stored in your local filesystem):
Import your Heroku project using the plugin just installed (Import... -> Heroku application): the project is not properly a Java project (packages are not highligthed with the usual icon, for example). To do this digit:
       $ play ecplipsify
Refresh the project on Eclipse and you see that now the project is correctly "divided" into packages.

To run the project locally, just use play run (http://localhost:9000)).

I haven't found a way to automatically compile the Play project in eclipse, so if I add a new Scala HTML template, I "eclipsify" one more time, to have a reference to the new view classes on Eclipse (the view.html.* classes created from the HTML files). This is also helpfull if you add new referenced libraries (I'll explain how to do this using GitHub in the next article).


Now that you are ready, it's the final countdown for your new killer app!