Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Tips and tricks for setting up a Play 2 project

Tips and tricks for setting up a Play 2 project

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

Manuel Bernhardt

April 27, 2022
Tweet

More Decks by Manuel Bernhardt

Other Decks in Programming

Transcript

  1. @elmanu #DV13PlayTricks name := "hello-play"! ! version := "1.0-SNAPSHOT"! !

    libraryDependencies ++= Seq(! jdbc,! anorm,! cache! ) ! ! play.Project.playScalaSettings! Hello world - build.sbt
  2. @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
  3. @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
  4. @elmanu #DV13PlayTricks name := "hello-play"! ! version := "1.0-SNAPSHOT"! !

    libraryDependencies ++= Seq(cache)! ! play.Project.playScalaSettings! ! scalariformSettings Scalariform! - build.sbt
  5. @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
  6. @elmanu #DV13PlayTricks name := "hello-play"! ! version := "1.0-SNAPSHOT"! !

    libraryDependencies ++= Seq(cache)! ! play.Project.playScalaSettings! ! scalariformSettings! ! org.scalastyle.sbt.ScalastylePlugin.Settings Scalastyle! - build.sbt
  7. @elmanu #DV13PlayTricks <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> Scalastyle! - scalastyle-config.xml
  8. @elmanu #DV13PlayTricks <?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>! Scalastyle! - Output
  9. @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
  10. @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
  11. @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
  12. @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
  13. @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
  14. @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
  15. @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
  16. @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
  17. @elmanu #DV13PlayTricks package controllers! ! class Users(greeting: String) extends BaseController

    {! ! def hello = Action { implicit request =>! ! Ok(greeting)! }! ! } DI - Users controller
  18. @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