Using Subprojects
Decomposing your application into subprojects can reduce build times a lot. Also, it adds structure to your source tree. The example app is a very simple two-pager, adding twitter bootstrap via WebJars as the only goodie. We want to hide the (very small) complexity of bootstrap/webjars in a sub-project called “pages”:
The sbt build
Lets start as Build.scala, where you define the sub-project.
In general, you can have all types of sbt-projects here, but we are interested in
Play-like sub-projects.
The only external dependency is twitter bootstrap, which is very conveniently added via the WebJars-repository:
1 2 3 4 | |
The more interesting part is of course the actual definition of the sub-project,
pages in this case. It looks like the usual Play Project definition in build files,
despite the path argument, that specifies its location in the source tree.
Afterwards, the sub-project is used in the dependsOn-clause of the actual main
project:
1 2 3 4 5 | |
Note how the dependencies, namely the webjars, are passed to both projects here. This is a little awkward and I’ll explain it later.
Routes
Now that we have declared the sub-project, we want to define the routes:
1 2 3 4 5 6 7 8 9 10 11 12 13 | |
Note how requests to URIs staring with /pages are delegates to the router of the
sub-project. (Note also that we are declaring a webjars asset helper, which is why we need
the dependency to webjars in the main project.)
The routes file of the sub-project, modules/pages/conf/pages.routes,
looks more or less boring:
1 2 3 4 5 6 7 8 9 10 11 | |
(Note that again, the webjars asset helper is included, which is one reason for requiring the dependency for the sub-project. We need this because we want to add bootstrap to the main template, which is defined in the sub-project.)
Controller
The controllers of sub-projects are the same as for main projects, only that they should
be put in the correct namespace, e.g. controllers.pages:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | |
View
The only interesting view is the main template in the sub project:
First, the bootstrap ressources are imported via webjar:
1 2 | |
Afterwards, the usual bootstrap code can be used. Note how a reverse route into the sub project is formulated:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | |
This template can now very easily be used from the main project:
1 2 3 4 5 | |
Joerg Viola