Tips and tricks for setting up a Play 2 project

4,322 views
4,117 views

Published on

A small set of tips and tricks for setting up a vanilla Play 2 project. Presentation given at Devoxx 2013 in Antwerp.

Published in: Technology, Art & Photos
1 Comment
12 Likes
Statistics
Notes
  • What about doing a template in Activator that implement these tips ?
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total views
4,322
On SlideShare
0
From Embeds
0
Number of Embeds
1,554
Actions
Shares
0
Downloads
31
Comments
1
Likes
12
Embeds 0
No embeds

No notes for slide

Tips and tricks for setting up a Play 2 project

  1. 1. Tips and tricks for setting up a Play 2 project ! ! Manuel Bernhardt #DV13PlayTricks @elmanu
  2. 2. play new hello-play Let’s get started #DV13PlayTricks @elmanu
  3. 3. Hello world - build.sbt name := "hello-play"! ! version := "1.0-SNAPSHOT"! ! libraryDependencies ++= Seq(! jdbc,! anorm,! cache! ) ! ! play.Project.playScalaSettings! #DV13PlayTricks @elmanu
  4. 4. Hello world - plugins.sbt // Comment to get more information during initialization! logLevel := Level.Warn! ! // The Typesafe repository ! resolvers += "Typesafe repository" at "http:// repo.typesafe.com/typesafe/releases/"! ! // Use the Play sbt plugin for Play projects! addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.2.1")! #DV13PlayTricks @elmanu
  5. 5. First things first: Scalariform ! Code indentation is good for your health #DV13PlayTricks @elmanu
  6. 6. Scalariform! - plugins.sbt // Comment to get more information during initialization! logLevel := Level.Warn! ! // The Typesafe repository ! resolvers += "Typesafe repository" at "http:// repo.typesafe.com/typesafe/releases/"! ! // Use the Play sbt plugin for Play projects! addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.2.0")! ! addSbtPlugin("com.typesafe.sbt" % "sbt-scalariform" % "1.2.0")! #DV13PlayTricks @elmanu
  7. 7. Scalariform! - build.sbt name := "hello-play"! ! version := "1.0-SNAPSHOT"! ! libraryDependencies ++= Seq(cache)! ! play.Project.playScalaSettings! ! scalariformSettings #DV13PlayTricks @elmanu
  8. 8. #DV13PlayTricks @elmanu
  9. 9. #DV13PlayTricks @elmanu
  10. 10. Scalastyle Keep it clean #DV13PlayTricks @elmanu
  11. 11. Scalastyle! - plugins.sbt // Comment to get more information during initialization! logLevel := Level.Warn! ! // The Typesafe repository ! resolvers += "Typesafe repository" at "http:// repo.typesafe.com/typesafe/releases/"! ! // Use the Play sbt plugin for Play projects! addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.2.0")! ! addSbtPlugin("com.typesafe.sbt" % "sbt-scalariform" % "1.2.0")! ! addSbtPlugin("org.scalastyle" %% "scalastyle-sbt-plugin" % "0.3.2") #DV13PlayTricks @elmanu
  12. 12. Scalastyle! - build.sbt name := "hello-play"! ! version := "1.0-SNAPSHOT"! ! libraryDependencies ++= Seq(cache)! ! play.Project.playScalaSettings! ! scalariformSettings! ! org.scalastyle.sbt.ScalastylePlugin.Settings #DV13PlayTricks @elmanu
  13. 13. Scalastyle! - scalastyle-config.xml <scalastyle>! <name>Scalastyle sample configuration</name>! <check level=“warning”! class=“org.scalastyle.file.FileLineLengthChecker"! enabled="true">! <parameters>! <parameter name="maxLineLength"><![CDATA[100]]></parameter>! <parameter name="tabSize"><![CDATA[2]]></parameter>! </parameters>! </check>! </scalastyle> #DV13PlayTricks @elmanu
  14. 14. Scalastyle! - Output <?xml version="1.0" encoding="US-ASCII"?>! <checkstyle version=“5.0”>! <file name=“/Users/manu/w/hello-play/app/controllers/Application.scala">! <error line=“12"! source=“org.scalastyle.file.FileLineLengthChecker"! severity=“warning"! message="File line length exceeds 100 characters”>! </error>! </file>! </checkstyle>! #DV13PlayTricks @elmanu
  15. 15. Sub-projects ! Keeping things fast #DV13PlayTricks @elmanu
  16. 16. Root Module 1 Module 2 Core #DV13PlayTricks @elmanu
  17. 17. Sub-projects - build.sbt play.Project.playScalaSettings! ! def playProject(name: String) = play.Project(! name = name,! path = file("modules/" + name)! ).! settings(scalariformSettings :_*).! settings(org.scalastyle.sbt.ScalastylePlugin.Settings :_*)! ! lazy val core = playProject("core")! ! lazy val module1 = playProject("module1").dependsOn(core)! ! lazy val module2 = playProject("module2").dependsOn(core)! ! lazy val root = playProject("hello-play").in(file(".")).! ! ! dependsOn(module1, module2).! aggregate(module1, module2) #DV13PlayTricks @elmanu
  18. 18. Sub-projects - build.sbt play.Project.playScalaSettings! ! def playProject(name: String) = play.Project(! name = name,! path = file("modules/" + name)! ).! settings(scalariformSettings :_*).! settings(org.scalastyle.sbt.ScalastylePlugin.Settings :_*)! ! lazy val core = playProject("core")! ! lazy val module1 = playProject("module1").dependsOn(core)! ! lazy val module2 = playProject("module2").dependsOn(core)! ! lazy val root = playProject("hello-play").in(file(".")).! ! ! dependsOn(module1, module2).! aggregate(module1, module2) #DV13PlayTricks @elmanu
  19. 19. Sub-projects - build.sbt play.Project.playScalaSettings! ! def playProject(name: String) = play.Project(! name = name,! path = file("modules/" + name)! ).! settings(scalariformSettings :_*).! settings(org.scalastyle.sbt.ScalastylePlugin.Settings :_*)! ! lazy val core = playProject("core")! ! lazy val module1 = playProject("module1").dependsOn(core)! ! lazy val module2 = playProject("module2").dependsOn(core)! ! lazy val root = playProject("hello-play").in(file(".")).! ! ! dependsOn(module1, module2).! aggregate(module1, module2) #DV13PlayTricks @elmanu
  20. 20. Sub-projects - build.sbt play.Project.playScalaSettings! ! def playProject(name: String) = play.Project(! name = name,! path = file("modules/" + name)! ).! settings(scalariformSettings :_*).! settings(org.scalastyle.sbt.ScalastylePlugin.Settings :_*)! ! lazy val core = playProject("core")! ! lazy val module1 = playProject("module1").dependsOn(core)! ! lazy val module2 = playProject("module2").dependsOn(core)! ! lazy val root = playProject("hello-play").in(file(".")).! ! ! dependsOn(module1, module2).! aggregate(module1, module2) #DV13PlayTricks @elmanu
  21. 21. Sub-projects - build.sbt play.Project.playScalaSettings! ! def playProject(name: String) = play.Project(! name = name,! path = file("modules/" + name)! ).! settings(scalariformSettings :_*).! settings(org.scalastyle.sbt.ScalastylePlugin.Settings :_*)! ! lazy val core = playProject("core")! ! lazy val module1 = playProject("module1").dependsOn(core)! ! lazy val module2 = playProject("module2").dependsOn(core)! ! lazy val root = playProject("hello-play").in(file(".")).! ! ! dependsOn(module1, module2).! aggregate(module1, module2) #DV13PlayTricks @elmanu
  22. 22. #DV13PlayTricks @elmanu
  23. 23. application.conf #DV13PlayTricks @elmanu
  24. 24. routes application.conf module1.routes module2.routes #DV13PlayTricks @elmanu
  25. 25. Snapshot dependencies and multi-module projects Workarounds #DV13PlayTricks @elmanu
  26. 26. Snapshot dependencies • https://github.com/sbt/sbt/issues/413 #DV13PlayTricks @elmanu
  27. 27. Snapshot dependencies • https://github.com/sbt/sbt/issues/413 #DV13PlayTricks @elmanu
  28. 28. Snapshot dependencies - workarounds • Don’t use snapshot dependencies • Convince library authors to make releases • Use a cache, e.g. Squid • OS X: http://squidman.net/squidman -­‐Dhttp.proxyHost=localhost  -­‐Dhttp.proxyPort=8090 #DV13PlayTricks @elmanu
  29. 29. Tools for Chrome After all, we’re building web-applications #DV13PlayTricks @elmanu
  30. 30. https://chrome.google.com/webstore/detail/play-framework-tools/dchhggpgbommpcjpogaploblnpldbmen #DV13PlayTricks @elmanu
  31. 31. Play Auto Refresh - plugins.sbt // Comment to get more information during initialization! logLevel := Level.Warn! ! // The Typesafe repository ! resolvers += "Typesafe repository" at "http://repo.typesafe.com/ typesafe/releases/"! ! // Use the Play sbt plugin for Play projects! addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.2.0")! ! addSbtPlugin("com.typesafe.sbt" % "sbt-scalariform" % "1.2.0")! ! addSbtPlugin("org.scalastyle" %% "scalastyle-sbt-plugin" % "0.3.2")! ! addSbtPlugin("com.jamesward" %% "play-auto-refresh" % "0.0.7") #DV13PlayTricks @elmanu
  32. 32. Play Auto Refresh - build.sbt name := "hello-play"! ! version := "1.0-SNAPSHOT"! ! libraryDependencies ++= Seq(! jdbc,! anorm,! cache! ) ! ! play.Project.playScalaSettings! ! scalariformSettings! ! org.scalastyle.sbt.ScalastylePlugin.Settings! ! com.jamesward.play.BrowserNotifierPlugin.livereload #DV13PlayTricks @elmanu
  33. 33. Chrome Your IDE play ~ run No more ⌘+R, yeah! #DV13PlayTricks @elmanu
  34. 34. Open errors in IDE Click to open in editor Instructions at https://github.com/jamesward/play-framework-chrome-tools #DV13PlayTricks @elmanu
  35. 35. Use dependency injection right away A poor man’s solution #DV13PlayTricks @elmanu
  36. 36. DI - Users controller package controllers! ! class Users(greeting: String) extends BaseController {! ! def hello = Action { implicit request =>! ! Ok(greeting)! }! ! } #DV13PlayTricks @elmanu
  37. 37. DI - Routes GET #DV13PlayTricks /users @controllers.Users.hello! @elmanu
  38. 38. DI - Global.scala import controllers.Users! import play.api.GlobalSettings! ! object Global extends GlobalSettings {! ! override def getControllerInstance[A](controllerClass: Class[A]): A = {! ! val USERS = classOf[Users]! ! val instance = controllerClass match {! case USERS => new Users("Hello users")! case _ => super.getControllerInstance(controllerClass)! }! ! instance.asInstanceOf[A]! }! } #DV13PlayTricks @elmanu
  39. 39. That’s it! Questions? ! https://github.com/manuelbernhardt/hello-play-dv13 ! http://manuel.bernhardt.io #DV13PlayTricks @elmanu

×