How to create a module with Play Framework

There are two approaches on how to create a Play Framework 2.3.x / 2.4.x module . The first is the module/sub-project the other is the code/sample-appI guess both are good depending on how you want to approach the build of your project.

How to create a module the module/sub-project approach :

The idea behind this approach to follow the sbt multi-project approach to treat the module as a subproject. To instruct play how the build for the existing project will be executed, a build.sbt file is dropped within the submodule project.

Here are the steps on how to build your module/sub-project 

  1. activator new module_sample_project
  2. select play-java
  3. cd module_sample_project
  4. activator new sample_module
  5. select play-java
  6. mv sample_module to module
  7. cd module
  8. From within the module_sample_project/module/ folder
    • rm -r activator activator*.jar project test public
    • mv conf/routes conf/sample_module.routes
    • #rename the root project name to module in your build.sbt
              name := """sample_module"""
              version := "1.0-SNAPSHOT"
              lazy val module = (project in file("."))
              scalaVersion := "2.11.6"
              libraryDependencies ++= Seq(
              // Play provides two styles of routers, one expects its actions to be injected, the
              // other, legacy style, accesses its actions statically.
              routesGenerator := InjectedRoutesGenerator
    • modify your folder structure so it can now make reference of the new module
      • mkdir -p app/sample_module/controllers app/sample_module/views
      • mv app/controllers app/sample_module/controllers
      • mv app/views app/sample_module/views
    • modify default controllers and views to make reference of the new structure
      • app/sample_module/controllers/ should belong to the sample_module.controllers package
      • mv app/sample_module/views/index.scala.html app/sample_module/views/sample_module_index.scala.html
      • mv app/sample_module/views/main.scala.html app/sample_module/views/sample_module_main.scala.html
      • update your views to make reference of the views.
    • Update routes to make new routing reference
      # Home page
      GET     /sample_module/    @sample_module.controllers.Application.index()
    • cd ../
  9. edit your build.sbt to depend on the new module
        lazy val module = (project in file("module"))
        lazy val root = (project in file("."))
  10. Update sample-project reference to make new routing reference
     # Routes
    # This file defines all application routes (Higher priority routes first)
    # ~~~~
    # Home page
    GET        /           @controllers.Application.index()
    #Add sample_module routes
    ->         /           sample_module.Routes
  11. run your app
    1. activator ~run

How to create a module with the code/sample-app approach :

This approach is very similar to the one described above by module/sub-project instructions. The main difference is that instead of pointing to a sub-project, the project is a code folder in the root of your git repo. The sample-app depends on the code code project. The instructions are the following :

  1. mkdir module_sample_app
  2. activator new module_sample_app
  3. select play-java
  4. mv module_sample_app code
  5. cd code
  6. Follow step 8 from module/sub-project 
  7. cd ../
  8. You will need to Publish Your Play Module to a Maven Repo repo your module so a dependency can be defined from a parallel project.
  9. activator new sample-app
  10. update your sbt to make reference of the module/sub-project module
  11. follow step 10 from module/sub-project 

The source code for the play-module-sample can be found here.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: