Friday, June 13, 2014

Introduction to Play-Framework modules

Play framework inherently support modularization. In other words we can develop play modules and reuse them in different play applications. This article provides a good guide on how to do that. But that is little bit outdated for latest Play distributions. I will describe the changes which is needed to be done to create a play module on latest play versions.
Playframework no longer has a play console. Instead of that it uses Typesafe activator. So you need to download activator and add activator to your environment path.
You can create boilerplate code for a play app using the  template called Just play java. In my case I needed to create a authentication module. So I created a Play action called auth in controllers package. Then to publish the module go the project directory and issue clean command. Then issue publish-local command. If it is successful you will get a output like this.

[info] published ivy to /home/prabhath/.ivy2/local/authmodule/authmodule_2.10/1.0-SNAPSHOT/ivys/ivy.xml
[success] Total time: 15 s, completed Jun 13, 2014 10:49:13 AM

Then create a new application which will use the previously created auth module. To add the dependency to new project update the build.sbt file. Content of my build.sbt file is like below.

name := """Test"""

version := "1.0-SNAPSHOT"

libraryDependencies ++= Seq(
  javaCore,  // The core Java API
  "junit" % "junit" % "4.8.2",


Next you have to tell the path of your local repository to the application. To do that edit plugins.sbt file in /<your project>/project directory. I added the following line to plugins.sbt file.

resolvers += "Local Play Repository" at "file://home/prabhathp/.ivy2/local/"

Now build the project and you will be able to use the classes in your module/controllers package inside classes in your controllers. Note that you do not need to add imports when reusing module components.