Play 2 Web Application Deployment Tutorial

By | July 2, 2013

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.

Subscribe to get the latest updates