+ All Categories
Home > Documents > Play 2 Tricks

Play 2 Tricks

Date post: 31-Dec-2015
Category:
Upload: whiteowl48
View: 52 times
Download: 3 times
Share this document with a friend
Description:
Play 2 Web development framework Tricks
Popular Tags:
39
@elmanu #DV13PlayTricks Tips and tricks for setting up a Play 2 project Manuel Bernhardt
Transcript
Page 1: Play 2 Tricks

@elmanu#DV13PlayTricks

Tips and tricks for setting up a Play 2 project

!!

Manuel Bernhardt

Page 2: Play 2 Tricks

@elmanu#DV13PlayTricks

play new hello-playLet’s get started

Page 3: Play 2 Tricks

@elmanu#DV13PlayTricks

name := "hello-play"!!version := "1.0-SNAPSHOT"!!libraryDependencies ++= Seq(! jdbc,! anorm,! cache!) !!play.Project.playScalaSettings!

Hello world - build.sbt

Page 4: Play 2 Tricks

@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

Page 5: Play 2 Tricks

@elmanu#DV13PlayTricks

First things first: Scalariform!

Code indentation is good for your health

Page 6: Play 2 Tricks

@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

Page 7: Play 2 Tricks

@elmanu#DV13PlayTricks

name := "hello-play"!!version := "1.0-SNAPSHOT"!!libraryDependencies ++= Seq(cache)!!play.Project.playScalaSettings!!scalariformSettings

Scalariform! - build.sbt

Page 8: Play 2 Tricks

@elmanu#DV13PlayTricks

Page 9: Play 2 Tricks

@elmanu#DV13PlayTricks

Page 10: Play 2 Tricks

@elmanu#DV13PlayTricks

Scalastyle Keep it clean

Page 11: Play 2 Tricks

@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

Page 12: Play 2 Tricks

@elmanu#DV13PlayTricks

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

Scalastyle! - build.sbt

Page 13: Play 2 Tricks

@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

Page 14: Play 2 Tricks

@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

Page 15: Play 2 Tricks

@elmanu#DV13PlayTricks

Sub-projects!

Keeping things fast

Page 16: Play 2 Tricks

@elmanu#DV13PlayTricks

Core

Module 1 Module 2

Root

Page 17: Play 2 Tricks

@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

Page 18: Play 2 Tricks

@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

Page 19: Play 2 Tricks

@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

Page 20: Play 2 Tricks

@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

Page 21: Play 2 Tricks

@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

Page 22: Play 2 Tricks

@elmanu#DV13PlayTricks

Page 23: Play 2 Tricks

@elmanu#DV13PlayTricks

application.conf

Page 24: Play 2 Tricks

@elmanu#DV13PlayTricks

routes

module2.routes

module1.routes

application.conf

Page 25: Play 2 Tricks

@elmanu#DV13PlayTricks

Snapshot dependencies and multi-module projects

Workarounds

Page 26: Play 2 Tricks

@elmanu#DV13PlayTricks

Snapshot dependencies

• https://github.com/sbt/sbt/issues/413

Page 27: Play 2 Tricks

@elmanu#DV13PlayTricks

Snapshot dependencies

• https://github.com/sbt/sbt/issues/413

Page 28: Play 2 Tricks

@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

Page 29: Play 2 Tricks

@elmanu#DV13PlayTricks

Tools for ChromeAfter all, we’re building web-applications

Page 30: Play 2 Tricks

@elmanu#DV13PlayTricks

https://chrome.google.com/webstore/detail/play-framework-tools/dchhggpgbommpcjpogaploblnpldbmen

Page 31: Play 2 Tricks

@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

Page 32: Play 2 Tricks

@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

Page 33: Play 2 Tricks

@elmanu#DV13PlayTricks

No more ⌘+R, yeah!

play ~ run

Chrome

Your IDE

Page 34: Play 2 Tricks

@elmanu#DV13PlayTricks

Open errors in IDE

Instructions at https://github.com/jamesward/play-framework-chrome-tools

Click to open in editor

Page 35: Play 2 Tricks

@elmanu#DV13PlayTricks

Use dependency injection right away

A poor man’s solution

Page 36: Play 2 Tricks

@elmanu#DV13PlayTricks

package controllers!!class Users(greeting: String) extends BaseController {!! def hello = Action { implicit request =>! ! Ok(greeting)! }!!}

DI - Users controller

Page 37: Play 2 Tricks

@elmanu#DV13PlayTricks

GET /users @controllers.Users.hello!

DI - Routes

Page 38: Play 2 Tricks

@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

Page 39: Play 2 Tricks

@elmanu#DV13PlayTricks

That’s it! Questions?!

https://github.com/manuelbernhardt/hello-play-dv13 !

http://manuel.bernhardt.io


Recommended