Tutorial #1: Getting started with Play Framework 2.3.4


In this blog we would set up the development environment with Play and Eclipse which would drive us through rest of the tutorial series.  We would be running this blog as a series and we would be looking at various aspects of play with this blog.

In this tutorial we will discuss about the following topics of Play Framework:

  • Installing Play
  • New application
  • Layout of Play application
  • Play console
  • Setting-up Eclipse

∗ Installing Play

Prerequisites: To run the Play framework, you need JDK 6 or later

Step-1: Download Activator from here

Step-2: Go to the ZIP file path and UNZIP it

anand@anand-singh:~$ cd softwares/
anand@anand-singh:~/softwares$ unzip typesafe-activator-1.2.10-minimal.zip
Archive:  typesafe-activator-1.2.10-minimal.zip
   creating: activator-1.2.10-minimal/
  inflating: activator-1.2.10-minimal/activator.bat  
  inflating: activator-1.2.10-minimal/activator  
  inflating: activator-1.2.10-minimal/activator-launch-1.2.10.jar  
anand@anand-singh:~/softwares$

Step-3: Set the activator to your path

anand@anand-singh:~$ vi .bashrc
export TYPESAFE_ACTIVATOR=/home/anand/softwares/activator-1.2.10-minimal
export PATH=$PATH:$TYPESAFE_ACTIVATOR

To check
anand@anand-singh:~$ which activator
/home/anand/softwares/activator-1.2.10-minimal/activator

Step-4: Check the activator command

anand@anand-singh:~$ activator -help

playingscala-tutorial-1

Now your system is ready to create a new Play application.

NOTE: If you don’t want to setup the activator into your path then just unzip and go to the activator script path and use it.

anand@anand-singh:~$ cd softwares/activator-1.2.10-minimal/
anand@anand-singh:~/softwares/activator-1.2.10-minimal$ ./activator -help

The result of "./activator -help" command is same like above screen.

∗ New application

There are several way to create new application using activator command:

$ activator new play-scala-tutorial-one play-scala

new_app

$ activator new play-scala-tutorial-one

new_app-2

∗ Layout of Play application

app                       Application sources
  assets                 Compiled asset sources
     stylesheets         Typically LESS CSS sources
     javascripts         Typically CoffeeScript sources
  controllers            Application controllers
  models                 Application business layer
  views                  Templates
build.sbt                 Application build script
conf                      Configurations files
  application.conf       Main configuration file
  routes                 Routes definition
public                    Public assets
  stylesheets            CSS files
  javascripts            Javascript files
  images                 Image files
project                   sbt configuration files
  build.properties       Marker for sbt project
  plugins.sbt            sbt plugins including play declaration
lib                       Unmanaged libraries dependencies
logs                      Standard logs folder
  application.log        Default log file
target                    Generated stuff
  scala-2.11.1            
     cache              
     classes             Compiled class files
     classes_managed     Managed class files (templates, ...)
     resource_managed    Managed resources (less, ...)
     src_managed         Generated sources (templates, ...)
test                      source folder for unit or functional tests

∗ Play Console

$ cd play-scala-tutorial-one
$ activator
[info] Loading project definition from /home/anand/projects/knoldus/play-scala-tutorial-one/project
[info] Set current project to play-scala-tutorial-one (in build file:/home/anand/projects/knoldus/play-scala-tutorial-one/)
[play-scala-tutorial-one] $ 

To Help:
[play-scala-tutorial-one] $ help run
Runs a main class, passing along arguments provided on the command line.
[play-scala-tutorial-one] $ help compile
Compiles sources.
[play-scala-tutorial-one] $ 


To Compile:
[play-scala-tutorial-one] $ compile
[success] Total time: 0 s, completed 3 Oct, 2014 12:11:34 PM
[play-scala-tutorial-one] $

The Play console is just a normal sbt console, so you can use sbt features such as triggered execution.

For example, using ~ compile

[play-scala-tutorial-one] $ ~ compile

The compilation will be triggered each time you change a source file.

If you are using ~ run

[play-scala-tutorial-one] $ ~ run

The triggered compilation will be enabled while a development server is running.

You can also do the same for ~ test, to continuously test your project each time you modify a source file:

[play-scala-tutorial-one] $ ~ test

 

∗ Setting-up Eclipse

[play-scala-tutorial-one] $ eclipse

Import the application into your Workspace with the File/Import/General/Existing project… menu (compile your project first).

eclipse

This is the start of tutorial series, from next week onwards we would be working on this tutorial blog to make it grow. We would look at how we can make it more usable and readable, then we would be adding next tutorial of “Using WebJars, jQuery, Bootstrap & Bootswatch with Play application”. If you have any question then feel free to comment on the same 🙂 Stay tuned.

Next : Tutorial #2

About Anand Kumar Singh

Hi, I’m Anand Singh and I specialize in Computer Software. I’m passionate about what I do, and I love to help people. Nothing is more fulfilling than being part of a team with similar interests, and an organization that values its employees. I'm Lead Software Engineer at Sapient. Having 5+ years of experience in Java, Grails/Groovy and Scala language. Anand also knows about Javascript, jQuery, CSS and other UI design technologies. Certified developer in: * Principles of Reactive Programming – Coursera * Functional Programming Principles in Scala – Coursera * Functional Program Design in Scala – Coursera * Introduction to Big Data with Apache Spark – edX * Oracle Certified Associate (OCA) * Sun Certified Java Programmer (SCJP) I'm passionate to work with upcoming technologies and bleeding platforms providing the new way to programming & development. He is actively involved in development & maintenance of several software projects. He has been enjoying Scala for more than a year now. Anand completed his MCA from IGNOU. As a Developer, I'm continuing my journey by actively collaborating with other Developers in my field. I'm actively contributing on the TechMonad & Playing Scala platform. I likes to spend his free time in Cooking, watching Discovery for new Technology and visiting new places with friends.
This entry was posted in Play Framework, Scala, Web and tagged , , , , . Bookmark the permalink.

14 Responses to Tutorial #1: Getting started with Play Framework 2.3.4

  1. Michael says:

    Hi,

    Thanks for your post. I am just getting started with Play 2.3.7. I am using Eclipse Juno with Scala IDE 3.0.4 on Mac OS 10.9

    I realised that the IDE does not recognise the syntax in the scala templates in the app/views folder. I have searched every where for the play plugin for eclipse, the one i found (https://github.com/scala-ide/scala-ide-play2) seems to be released for Play 2.1. I installed it but it failed to work.

    Also the IDE does not recognise the view templates on the controller (e.g index.render(“Hello World”) …index cannot be resolved).

    Please let me know where I can get the Play plugin or which free IDE I can use.

    Thanks

  2. There is a new version of Scala IDE 4.0.0 available now http://scala-ide.org/download/sdk.html . You should try that. Make sure to include the classes_managed folder in your libraries. This has all the templates that you would need for compilation. In my case the folder is under
    ./play-scala-tutorials/target/scala-2.11/classes_managed

    Go to project → properties → java build path → libraries (it’s a tab) → add external class folder

    and include that folder and you should be good. For more details refer to the discussion here http://stackoverflow.com/questions/10910215/play2-framework-my-template-is-not-seen-package-views-html-doesn-not-exist

  3. stephen wright says:

    hi
    Thanks a bunch for the tutorial!!!
    Question: When working with eclipse, do we always want to leave activator running in ‘notice changed files mode’ (activator ~run) or can Eclipse handle this internally? The best development practice for Play 2 via Eclipse will always include ‘activator’ running in the background.
    FWIW, I have have followed the advice to have nightly IDE builds installed for Eclipse. help -> add new software -> add -> http://download.scala-ide.org/nightly-scala-ide-luna-211x
    For Kepler use http://download.scala-ide.org/sdk/lithium/e38/scala211/dev/site/

  4. stephen wright says:

    https://docs.google.com/document/d/1gjH5TaQDKxbsrD4hzsMyUlHMq5LGFA0pletpdUwId7I/edit#heading=h.ucivqn1l6uit>Google Docs

    Eclipse preferences for Play 2 from Google Docs.
    =====================================
    Build automatically: disable (important!)
    Play performs byte code changes during compilation. If your class files are generated by Eclipse those will be missing and Play gets utterly confused.
    Refresh using native hooks: enable
    I believe this is required to constantly import the class files generated by Play.

  5. Pingback: Tutorial #2: Using WebJars, jQuery, Bootstrap & Bootswatch with Play application | Knoldus

  6. Pingback: Tutorial #3: Getting started with HTTP Programming in Play Framework | Knoldus

  7. mintu says:

    great post dear
    thanks for describing play framework in details

  8. Mayur Thube says:

    Hi,
    I’m trying to start java play 2.5 project with eclipse in Linus Mint, but i am not able to start. i got following error

    [play-java-demo] $ compile
    [success] Total time: 1 s, completed Apr 19, 2016 11:36:41 AM
    [play-java-demo] $ eclipse
    [error] Not a valid command: eclipse (similar: help, alias)
    [error] Not a valid project ID: eclipse
    [error] Expected ‘:’ (if selecting a configuration)
    [error] Not a valid key: eclipse (similar: deliver, licenses, clean)
    [error] eclipse
    [error] ^
    [play-java-demo] $

    Kindly help me on this regards.

  9. Mayur Thube says:

    Hi ,
    I’m trying to start java play 2.5 project in Linux Mint ,but i got an error .i used following steps

    ayur@kailash ~/ArvanProjects/TestPlayWork/play-java-demo $ activator
    [info] Loading project definition from /home/mayur/ArvanProjects/TestPlayWork/play-java-demo/project
    [info] Set current project to play-java-demo (in build file:/home/mayur/ArvanProjects/TestPlayWork/play-java-demo/)
    [play-java-demo] $ compile
    [success] Total time: 1 s, completed Apr 19, 2016 11:42:35 AM
    [play-java-demo] $ eclipse
    [error] Not a valid command: eclipse (similar: help, alias)
    [error] Not a valid project ID: eclipse
    [error] Expected ‘:’ (if selecting a configuration)
    [error] Not a valid key: eclipse (similar: deliver, licenses, clean)
    [error] eclipse
    [error] ^

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s