Book Image

Learning Play! Framework 2

By : Andy Petrella
Book Image

Learning Play! Framework 2

By: Andy Petrella

Overview of this book

<p>The Learning Play! Framework 2 has been created for web developers that are building web applications. The core idea is to focus on the HTTP features and to enable them through a simplification lens. Building a web application no longer requires a configuration phase, an environment setup, or a long development lifecycle - it's integrated!<br /><br />Learning Play! Framework 2 will enable any web developers to create amazing web applications taking advantage of the coolest features. It's the fastest way to dive into Play!, focusing on the capabilities by using them in a sample application. Although essentially Java based code, a Scala version is presented as well – giving an opportunity to see some Scala in action.<br /><br />After setting up the machine and learning some Scala, you will construct an application which builds from static to dynamic, before introducing a database. <br /><br />Then we'll focus on how data can be consumed and rendered in several ways. This will enable some real time communication through WebSocket and Server-Sent Event – on both server and client sides.</p> <p>The book will end with testing and deployment, which completes any web development project.</p>
Table of Contents (20 chapters)
Learning Play! Framework 2
Credits
About the Author
Acknowledgement
About the Reviewers
www.packtpub.com
Preface
Materials
Index

Simple Build Tool


In the earlier sections, we used the play! console a lot to access the tasks related to our project. Actually, this command-line tool is a customization of Simple Build Tool (SBT).

SBT is a powerful and easily extensible build tool like Maven or Ant. But, where the latter rely exclusively on the external DSLs to manage their configuration, SBT uses an internal Scala DSL for that. Of course, this isn't its only advantage.

What is interesting at this point is that SBT doesn't need any specific integration with IDEs because it's simply Scala code. As one isn't required to know Scala in order to create or update an SBT configuration, let's cover how to deal with its common tasks.

Looking into the project folder, we'll find a Build.scala file, which contains the basic configuration of our build. It briefly defines play.Project: an extension of a regular SBT Project. The following screenshot shows what it contains:

Adding a third-party dependency

Even if Play! 2 already integrates a lot of libraries that are usually sufficient, it often happens that we need to add new dependencies to our projects to access new features (such as a statistics library) or provided one with a different vision (such as a new logging library).

As an example, we'll add the latest version of Guava (http://code.google.com/p/guava-libraries/) to our project.

As Scala is powerful enough to create DSLs, SBT took the opportunity to provide a DSL to define Ła project. Let's see an example of adding a dependency using this DSL.

For that, the Build.scala file already defines a sequence (appDependencies) that can be seen as an immutable java.util.List in Scala. This sequence is meant to contain all the extra dependencies that we'll need to be added to our project.

As SBT can use the Maven or Ivy repositories, and is configured to check the common public ones, what we'll do is add Guava using its Maven groupId, artifactId, and the required version.

Let's see the syntax:

Later on, this sequence will be used in the play.Project configuration as a parameter.

Repositories

In the previous section, we saw how to add new dependencies to our projects; but this method will only work for the libraries that have been deployed on public repositories. However, as developers, we'll face two other cases:

  • Locally built libraries (either open source or owned) that are placed in our local repository

  • A library that is not available in the common public repositories

The way to go for such cases is to tell the play.Project configuration to look into the other repositories that we have configured, shown as follows:

A DSL is meant to be code-readable by expressing and using business-specific concepts for a specific field. Let's check if it is, by reviewing the repositories' declaration.

A repository is nothing more than a folder that is accessible using a path, and which has a structure that follows some convention. So, a declaration is composed of three things:

  • A name (Local Maven Repository)

  • A protocol or an access method (file is a function that takes a path and declares it as a filesystem resource)

  • A path: the location of the repository

For convenience, we store these definitions in val (which are immutable variables) in order to use them in the play.Project declaration. This declaration is done by adding the existing resolvers (or repositories) to our new sequence of repositories (or resolvers) using the ++= operator.