Play 2 Web Application Deployment Tutorial

Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInEmail this to someone

We have received multiple requests from our users to add support of the Play 2 web framework for Java and Scala applications. To be more precise, almost 2000 of our users voted for this feature. We are happy to share with you that it is now available! Thanks to all of our users who supported this feature and thanks to Typesafe for creating such a fantastic platform!

In this tutorial you’ll see how to deploy your Play 2 web app to the cloud without any code changes. As you know Play 2 is a Java and Scala framework, which can integrate all the components and APIs that are needed for modern web application development. In the heart of Play is a stateless, lightweight, web-friendly architecture and minimal resource consumption – all you need for the creation of highly-scalable Java and Scala web apps.

Let’s see how you can get all the above advantages into the cloud!

Firstly, create a new environment for your Play 2 web application deployment using the available software stacks from the Jelastic dashboard. Find more details in our dashboard guide.

Install Play framework

1. Navigate to playframework.com and download Play 2 framework.

play 2 framework download

2. Unzip the package you have just downloaded.

3. Add the framework installation directory to your system path.

For Linux:

export PATH=$PATH:/relativePath/to/play

For Windows:

Update the path in the environment variables (don’t use a path with spaces).

4. Check if the Play framework commands are available. Enter the next command in the command line:

>play help

You’ll see the basic help.

play 2 web framework installation

Create Play 2 web application

Use play new command to create your application.

play new jelasticapp

This command requires some additional info:

  • application name 
  • the template for this application (Scala or Java)

play 2 java scala application

Install Play2War plugin for Play 2.2.1 version

You need Play2War plugin to package your application into a WAR file for further deployment.

1. Navigate to your application directory project/plugins.sbt and add one more repository after the default Typesafe one:

resolvers += "Play2war plugins release" at "http://repository-play-war.forge.cloudbees.com/release/"

and the Play2War plugin at the end of the code:

addSbtPlugin("com.github.play2war" % "play2-war-plugin" % "1.2-beta2")

All the needed artifacts are accessible on Maven central.

Note: you should leave a blank line after every configuration string, otherwise you can receive errors during further file building.

2. In the same project folder create one more Build.scala file. Import the following Play2War SBT settings there:

import sbt._
import Keys._
import play.Project._
import com.github.play2war.plugin._

object ApplicationBuild extends Build {

val appName = "y"
val appVersion = "1.0-SNAPSHOT"

val appDependencies = Seq(
// Add your project dependencies here,
javaCore,
javaJdbc,
javaEbean
)

val main = play.Project(appName, appVersion, appDependencies)
.settings(Play2WarPlugin.play2WarSettings: _*)

.settings(
Play2WarKeys.servletVersion := "3.0"
)
// Add your own project settings here
}

Note: depending on the app server your application will be deployed, specify the corresponding servlet container configuration.

  • Select Servlet 2.5 for Tomcat 6
.settings(
    // ... Your own settings here
    Play2WarKeys.servletVersion := "2.5"
)
  • Select Servlet 3.0 for Tomcat 7 or GlassFish
.settings(
    // ... Your own settings here
    Play2WarKeys.servletVersion := "3.0"
)

Play2War will generate a default web.xml file for your application, which will be embedded in the generated WAR package.

Install Play2War plugin for Play 2.1.1 version

1. Navigate to your application directory project/plugins.sbt and add the Play2War plugin in there:

addSbtPlugin("com.github.play2war" % "play2-war-plugin" % "1.0.1")

All the needed artifacts are accessible on Maven central.

2. Navigate to project/Build.scala file and import Play2War SBT settings.

import com.github.play2war.plugin._

3. In the same file specify Play2War plugin settings before your project configuration:

val main = PlayProject(appName, appVersion, appDependencies, mainLang = JAVA)
  .settings(Play2WarPlugin.play2WarSettings: _*)

4. Choose which app server your application will be deployed and specify the corresponding servlet container configuration:

  • Select Servlet 2.5 for Tomcat 6
.settings(
    // ... Your own settings here
    Play2WarKeys.servletVersion := "2.5"
)
  • Select Servlet 3.0 for Tomcat 7 or GlassFish
.settings(
    // ... Your own settings here
    Play2WarKeys.servletVersion := "3.0"
)

Finally your Build.scala file should look like this:

import sbt._
import Keys._
import play.Project._
import com.github.play2war.plugin._

object ApplicationBuild extends Build {

val appName = "y"
val appVersion = "1.0-SNAPSHOT"

val appDependencies = Seq(
// Add your project dependencies here,
javaCore,
javaJdbc,
javaEbean
)

val main = PlayProject(appName, appVersion, appDependencies, mainLang = JAVA)
.settings(Play2WarPlugin.play2WarSettings: _*)

.settings(
Play2WarKeys.servletVersion := "3.0"
)
// Add your own project settings here
}

Play2War will generate a default web.xml file for your application, which will be embedded in the generated WAR package.

Generate WAR file

Finally you can generate your project WAR file for further deployment using the following command:

>play war

play 2 java war generating

The generated WAR will be stored in your_app_name/target folder.

Now, you can easily upload the package with Play 2 web app to the Jelastic Deployment manager and deploy it to the environment you created earlier.

We hope this tutorial proved useful. Stay tuned to our blog and don’t forget to support new features for the Jelastic Cloud Platform here.

Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInEmail this to someone

11 Comments

  • [...] Marina Sprava hat einen neuen interessanten Artikel in unserem englischen Blog geschrieben, den wir Euch nicht vorenthalten wollen. Play 2 Web Application Deployment Tutorial [...]

  • This is all well and good but not supporting Netty directly and insisting on using the Playtowar you lose the asynchronous goodness (if not using servlet 3) and performance takes a hit.

    • Ruslan says:

      Hi Justin, you are right. It is much better to use the most native approach. We will take your note into account and I think we will be to add support of Netty in the future. Anyway PlayToWar is quite standard approach and it can be used in many cases while Netty is absent.

  • Keuller Magalhaes says:

    Hi guys, first all I would like to congratulate Marina Sprava for this nice tutorial. Second all, I agree with Justin’s thinking, but several companies has deployment policies that requires you create WAR file, because you does not have access to production servers and there is some internal processes. Otherwise, lets think how many applications uses asynchronous support ? In fact how many developers create their application focusing on performance ? I’m just talking that Play is a awesome framework, but developers must use it in better way according with your companies rules.

  • MArtin says:

    Hi when I do the changes you recommend and then try and run ‘play war’ I get an error saying ‘Not a valid key: war’….so it looks like it has not been included.

    Is there a step I missed?
    updated plugins, build.scala and build.sbt….

    Any help appreciated…

    • Tetiana Fydorenchyk says:

      Hi, Martin. To understand why you have this problem we need more details on what steps you’ve done and how. So please contact your hoster’s support and describe them the issue you’ve faced. They’ll help you to find the solution. Thank you for using Jelastic.

  • Anver Bogatov says:

    Hi guys! Support of Play! framework is a really great news, but… Is there any manual about how to add jelastic support to existent Play! 2 framework application?

    • Tetiana Fydorenchyk says:

      Hi, please contact your hoster’s support and provide more details on what problem you face while deploying your application. They will assist you in solving this issue.

  • Deepa says:

    play war is not working these days. I tried the new command activator war aswell I am getting the following error

    [error] Expected ID character
    [error] Not a valid command: activator
    [error] Expected project ID
    [error] Expected configuration
    [error] Expected ‘:’ (if selecting a configuration)
    [error] Expected key
    [error] Not a valid key: activator (similar: artifact, artifacts)
    [error] activator war
    [error] ^

    Really appreciate any help since I am a beginner to Play and Scala

    • Tetiana Fydorenchyk says:

      We rechecked the instruction and it is working.
      Please contact your hosting provider support providing your registered email and the details, they will support you with the required settings.

Leave a Reply

Contact Jelastic for More Information CONTACT
%d bloggers like this: