Wednesday, August 6, 2014

How To Create a Play HelloWorld Application and Deploy to Tomcat

If you want yo try out deploying a Hello World Play application to Tom Cat it can be done in 15 minutes or less.

What you need in your System :

Play installed. - I have tested this in Play 2.2.3
Tomcat downloaded and configured. - Tomcat version where I tested is 7.0.55

Here are steps you need to follow -

1. At the command prompt go to the folder under which you are planning to create the play application.
2. Issue the command - play new helloworld
3. You will be prompted for more inputs.  Select Java as the template.  Although Scala is just as fine.
4. At the play prompt issue - play idea (if using intellij) or play eclipse (for eclipse users).  Now open the application in your editor.
5. Create a new file Build.scala in the project directory with following content.

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,
    cache
  )

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

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

6. Open plugins.sbt in the project directory and overwrite the content with the following content -

// Comment to get more information during initialization
logLevel := Level.Warn

// The Typesafe repository
resolvers += "Typesafe repository" at "http://repo.typesafe.com/typesafe/releases/"

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

// Use the Play sbt plugin for Play projects
addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.2.3")

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

7. Open the build.sbt in the root directory and overwrite the content with content below.

import com.github.play2war.plugin._

name := "helloPlay"

version := "1.0-SNAPSHOT"

Play2WarPlugin.play2WarSettings

Play2WarKeys.servletVersion := "3.1"


libraryDependencies ++= Seq(
  javaJdbc,
  javaEbean,
  cache
)     

play.Project.playJavaSettings

8. Save the changes and go back to command prompt.

9. In the helloworld directory issue the command - play war

10. War will be generated in the target directory with the name helloplay-1.0-SNAPSHOT.war

11. Create another copy of the above war as helloplay.war

12. Copy the war to <tomcat installation directory>/webapps folder.

13. Start tomcat with the command startup.bat inside the bin directory which is inside tomcat installation directory.

14. You should see in the console that helloplay application is being deployed.

15.  You can access the application with the url - http://localhost:8080/helloplay/

16. You will see the following -


No comments:

Post a Comment