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-app. I 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
- activator new module_sample_project
- select play-java
- cd module_sample_project
- activator new sample_module
- select play-java
- mv sample_module to module
- cd module
- 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(".")) .enablePlugins(PlayJava) scalaVersion := "2.11.6" libraryDependencies ++= Seq( javaJdbc, cache, javaWs ) // 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/Application.java 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
#module/conf/sample_module.routes # Home page GET /sample_module/ @sample_module.controllers.Application.index()
- cd ../
- edit your build.sbt to depend on the new module
lazy val module = (project in file("module")) .enablePlugins(PlayJava) lazy val root = (project in file(".")) .enablePlugins(PlayJava) .aggregate(module) .dependsOn(module)
- Update sample-project reference to make new routing reference
#conf/routes # Routes # This file defines all application routes (Higher priority routes first) # ~~~~ # Home page GET / @controllers.Application.index() #Add sample_module routes -> / sample_module.Routes
- run your app
- 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 :
- mkdir module_sample_app
- activator new module_sample_app
- select play-java
- mv module_sample_app code
- cd code
- Follow step 8 from module/sub-project
- cd ../
- 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.
- activator new sample-app
- update your sbt to make reference of the module/sub-project module
- follow step 10 from module/sub-project
The source code for the play-module-sample can be found here.