Slide 1

Slide 1 text

@elmanu #DV13PlayTricks Tips and tricks for setting up a Play 2 project ! ! Manuel Bernhardt

Slide 2

Slide 2 text

@elmanu #DV13PlayTricks play new hello-play Let’s get started

Slide 3

Slide 3 text

@elmanu #DV13PlayTricks name := "hello-play"! ! version := "1.0-SNAPSHOT"! ! libraryDependencies ++= Seq(! jdbc,! anorm,! cache! ) ! ! play.Project.playScalaSettings! Hello world - build.sbt

Slide 4

Slide 4 text

@elmanu #DV13PlayTricks // 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")! Hello world - plugins.sbt

Slide 5

Slide 5 text

@elmanu #DV13PlayTricks First things first: Scalariform ! Code indentation is good for your health

Slide 6

Slide 6 text

@elmanu #DV13PlayTricks // 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")! Scalariform! - plugins.sbt

Slide 7

Slide 7 text

@elmanu #DV13PlayTricks name := "hello-play"! ! version := "1.0-SNAPSHOT"! ! libraryDependencies ++= Seq(cache)! ! play.Project.playScalaSettings! ! scalariformSettings Scalariform! - build.sbt

Slide 8

Slide 8 text

@elmanu #DV13PlayTricks

Slide 9

Slide 9 text

@elmanu #DV13PlayTricks

Slide 10

Slide 10 text

@elmanu #DV13PlayTricks Scalastyle Keep it clean

Slide 11

Slide 11 text

@elmanu #DV13PlayTricks // 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") Scalastyle! - plugins.sbt

Slide 12

Slide 12 text

@elmanu #DV13PlayTricks name := "hello-play"! ! version := "1.0-SNAPSHOT"! ! libraryDependencies ++= Seq(cache)! ! play.Project.playScalaSettings! ! scalariformSettings! ! org.scalastyle.sbt.ScalastylePlugin.Settings Scalastyle! - build.sbt

Slide 13

Slide 13 text

@elmanu #DV13PlayTricks ! Scalastyle sample configuration! ! ! ! ! ! ! Scalastyle! - scalastyle-config.xml

Slide 14

Slide 14 text

@elmanu #DV13PlayTricks ! ! !

Slide 15

Slide 15 text

@elmanu #DV13PlayTricks Sub-projects ! Keeping things fast

Slide 16

Slide 16 text

@elmanu #DV13PlayTricks Core Module 1 Module 2 Root

Slide 17

Slide 17 text

@elmanu #DV13PlayTricks 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) Sub-projects - build.sbt

Slide 18

Slide 18 text

@elmanu #DV13PlayTricks 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) Sub-projects - build.sbt

Slide 19

Slide 19 text

@elmanu #DV13PlayTricks 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) Sub-projects - build.sbt

Slide 20

Slide 20 text

@elmanu #DV13PlayTricks 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) Sub-projects - build.sbt

Slide 21

Slide 21 text

@elmanu #DV13PlayTricks 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) Sub-projects - build.sbt

Slide 22

Slide 22 text

@elmanu #DV13PlayTricks

Slide 23

Slide 23 text

@elmanu #DV13PlayTricks application.conf

Slide 24

Slide 24 text

@elmanu #DV13PlayTricks routes module2.routes module1.routes application.conf

Slide 25

Slide 25 text

@elmanu #DV13PlayTricks Snapshot dependencies and multi-module projects Workarounds

Slide 26

Slide 26 text

@elmanu #DV13PlayTricks Snapshot dependencies • https://github.com/sbt/sbt/issues/413

Slide 27

Slide 27 text

@elmanu #DV13PlayTricks Snapshot dependencies • https://github.com/sbt/sbt/issues/413

Slide 28

Slide 28 text

@elmanu #DV13PlayTricks 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

Slide 29

Slide 29 text

@elmanu #DV13PlayTricks Tools for Chrome After all, we’re building web-applications

Slide 30

Slide 30 text

@elmanu #DV13PlayTricks https://chrome.google.com/webstore/detail/play-framework-tools/dchhggpgbommpcjpogaploblnpldbmen

Slide 31

Slide 31 text

@elmanu #DV13PlayTricks // 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") Play Auto Refresh - plugins.sbt

Slide 32

Slide 32 text

@elmanu #DV13PlayTricks 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 Play Auto Refresh - build.sbt

Slide 33

Slide 33 text

@elmanu #DV13PlayTricks No more ⌘+R, yeah! play ~ run Chrome Your IDE

Slide 34

Slide 34 text

@elmanu #DV13PlayTricks Open errors in IDE Instructions at https://github.com/jamesward/play-framework-chrome-tools Click to open in editor

Slide 35

Slide 35 text

@elmanu #DV13PlayTricks Use dependency injection right away A poor man’s solution

Slide 36

Slide 36 text

@elmanu #DV13PlayTricks package controllers! ! class Users(greeting: String) extends BaseController {! ! def hello = Action { implicit request =>! ! Ok(greeting)! }! ! } DI - Users controller

Slide 37

Slide 37 text

@elmanu #DV13PlayTricks GET /users @controllers.Users.hello! DI - Routes

Slide 38

Slide 38 text

@elmanu #DV13PlayTricks 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]! }! } DI - Global.scala

Slide 39

Slide 39 text

@elmanu #DV13PlayTricks That’s it! Questions? ! https://github.com/manuelbernhardt/hello-play-dv13 ! http://manuel.bernhardt.io