Playing with Play. In Java

I am working with Spring Framework for a few years now. It is quite good and does its job. However, I came to a conclusion I need to leave the Framework and see what other ways are there to build web applications. I have been looking for something interesting and Play Framework grabbed my attention. Why? I can use it with Java or Scala and it is supported by Lightbend (previously known as Typesafe). On the framework site, it is mentioned Play is scalable, lightweight and a couple of other things. So why not to check what this marketing is worth? The newest version of the framework at the moment of writing is version 2.5.5 and this one I am going to base on.

 

play_full_color_small

I know, there is a lot of tutorials and examples for Play. However, I would like to go through the framework with a small posts series, starting from the basics and moving to more advanced things. Those will be my notes on learning Play.

Right, how to start the adventure with Play? The easiest way is to use Activator tool. This is a simple build tool provided by Lightened. It simplifies the creation of Scala and Java projects and offers plenty of reusable templates and tutorials. You can grab its distribution from the Play’s page.

 

So before we will go further, download the tool to your file system and add it to system’s PATH.

Now you can create a project in two ways: using Activator UI or with the command line.

To go with the former approach type activator ui in the command line and you will be redirected to a browser. Here you can select a template in Seeds and provide a folder where the project should be created. This is quite trivial.

I prefer, however, the latter method, focused on the command line. With this one, go to a folder where the project will be created. Then just type activator new [project-name] [project-template]. You need to provide a name of your project and a template it will base on. To check what templates are available you can use activator list-templates.

To create a Play project for Java a command like the following one has to be run from a shell: activator new playing-with-java-play play-java.

So type it, press enter and voilà! We have a fresh Play project. It is based on SBT. Its structure differs from the standard one known from Maven and SBT, which is widely in JVM projects. Instead of src folder containing almost everything we have here couple of folders and some other additional stuff:

  • app – here is the place for code of the application and views templates
  • public – this folder contains static resources required by views like CSS, JavaScript and images
  • test – as the name suggests all tests of our application lay here
  • conf – yet another folder with a clear purpose: all configuration files required by our project are here
  • build.sbt and project – here we have a build script of our project and the folder containing configuration for SBT (the tool version, plugins used during a build, etc.),
  • bin and libexec – these folders provide the runtime for Activator tool: the former contains shell scripts, the latter the tool’s jar.

When a new project for Play with Java is created by Activator it contains some code examples already. I have removed them from my project I pushed to Github since I want to move step by step with Play offerings. So at the moment of writing the project is empty in terms of any code and resources is copied from a template.

Project’s directory layout can be changed to the one used by SBT by default. Just follow instructions from here to do so.

With application.conf file sitting in conf folder you can set up a plenty of aspects in your project like database connectivity, AKKA, HTTP and many, many others. The routes file from the same directory, on the other hand, is the place to configure HTTP endpoints of an application. I will look closely at both during the Play series.

You can edit code with Activator UI. It provides Build/Test/Run actions and a simplistic version of a code editor. There is no support for refactoring, code completion and debugging tools to mention just some of the most important ones. But does all of this make the UI worthless? Not at all! It is a nice application making easy to try various templates. I see it as the application of the first contact with a project or given template. You can read project’s tutorial and play with it.

 

activator_ui

 

If you think seriously about developing the project then you can use the option of creating Eclipse or IntelliJ project files (where is NetBeans option by the way?!). Of course, you can still simply import a project using option provided by IDE.

The created project can be started from a command line with run task. You can use Activator or SBT to trigger the command. In fact, it does not matter which one you will use since Activator utilises tasks from SBT build script. To see what tasks are available use set|activator tasks command. It is worth to run the build tool in interactive mode so every time you trigger some task the tool’s console will not be started.

I have pushed the empty project to GitHub. Some useful content will show up there as new parts of Play series appear. Stay tuned.

  1 comment for “Playing with Play. In Java

  1. August 31, 2016 at 8:10 am

    Very good point mentioning the project’s directory layout can be changed to match default SBT Maven standard.

    I moved from a Maven JEE JBOSS background to SBT Scala Play some years ago and found anything changing, from my previous world, without bringing value was a possible stopper to newcomers.

Leave a Reply