Writing a Play module

prakash vadrevu
confest
Published in
3 min readNov 25, 2016

Hi there! In this post, I am going to explain you how to write “modules” in Play 2.5.

Since, Play 2.4, the framework started supporting Dependency Injection (DI) out of the box with “Google Guice”. For those of you who do not know what Guice is, it is a very simple and light weight framework for injecting dependencies into your classes. Its fun and easy to use and I personally like it very much.

Okay, lets get to the point, period.

The first thing is — “Play Plugins” have been deprecated in 2.5. Why? Because — since the Google Guice DI is supported out of the box, the “Modules” that we write can fully embrace this and hence, much easier management. So, practically, there is no need to have a Plugin model and manage them in our application.

What are we going to do?

We are going to write a simple module app, which just prints “Hello world from ConFest!”. Then, we are going to include it in our app. Thats it! Simple and to the point. Lets get started!

Diving In!

Lets start by creating a folder play-module-java-example. Now, use these:

  1. Hit command activator new
  2. Choose play-java from the options
  3. Give your module name
  4. You are done!

Here is how it goes:

Awesome, we have got our basic structure ready. Wait, but this is for a regular Play! app not for a module. We need to alter this structure a bit to make it module-ready. Lets do it.

Alterations

  1. Delete the conf/routes.conf. Why? Because, when this module gets imported, it overrides the routes of the actual app.
  2. Also, delete the conf/applications.conf. The configurations come from the actual app’s config file.
  3. Wipe out the app folder completely. But keep the folder. We’ll add code soon in there.
  4. For this module even the public folder is not required. Remove it too.

At this point, your folder structure should look something like this

Code

Create your module’s package under app. I am creating it as in.confest.examplemodule. Now, we have to create our Guice Module to be included in the actual project.

Now, create the classes mentioned in the configure() method above. Pretty Simple classes.

We are done with the coding part. Simple, isnt it?

Configuration

The coding part is done, but the piece to link it to the actual Play app is missing. We need to tell the world that a module called ExampleModule exists. For this, create conf/reference.conf file and include the following line in it.

Now, lets configure the module’s properties like its name, version, etc… For this open your build.sbt and make sure it looks like this

We are pretty much done! Lets get to building this now

Build

Use the command activator publish-local to build it. This is how your terminal output should look like:

Great job! we have built our module. Yay!

Including module in app

Now, its time to use this module. For this, you have to include this module as a dependency in your build.sbt file of the target app like this:

Now, you can use your new module in your app.

Source code of this module has been hosted on GitHub.

Thats the end of the story folks. Stay tuned for more posts!

Please do leave your feed back below. Thank you.

HAPPY CODING…!!!

--

--