Table of contents
1.
Introduction🤓
2.
Adding a library sub-projects📢
3.
Splitting the Web App😃
3.1.
Splitting the Route File
3.1.1.
Build Configuration
3.1.2.
Project Structure
3.1.3.
Assets and controller classes
3.1.4.
Reverse Routing 🔁
4.
Sharing common codes and variables🔗🤝🏻
5.
Advantages of using the Play framework😯
6.
Frequently Asked Questions💡
6.1.
What is an activator in the Play framework?
6.2.
What is the Play framework in Scala?
6.3.
What is sbt in the Play framework?
7.
Conclusion🎉
Last Updated: Mar 27, 2024
Easy

Working with Sub-Projects

Author Sagar Mishra
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction🤓

Play Framework is an Open Source web app that follows the MVC architectural pattern. It is written in Scala and useable from other programming languages compiled to JVM bytecode, such as Java. 

Working with sub-projects

Play can create and compile a project that is based on Java. But sometimes, it complicates the project in a single Play app. To tackle this problem, we must learn how Working with sub-projects takes place. So, today's topic is "Working with sub-projects." Let's begin without wasting time!

Adding a library sub-projects📢

The first topic of the "Working with sub-projects" series is Adding library sub-projects. This will help you to learn how to add a simple library sub-project.

 

Adding a library sub-projects

The whole functionality mainly depends on the library in any project or app. To add a simple library in the sub-projects, add another sbt project definition in your build.sbt file:

name := "my-first-app"
version := "1.0"


lazy val myFirstApp = (project in file("."))
    .enablePlugins(PlayScala)
    .aggregate(myLibrary)
    .dependsOn(myLibrary)
lazy val myLibrary = project 

 

Here, the project that is in lowercase in the last line is Scala Macro. It will use the name of val to determine the project's name and folder. And the base project is myFirstApp in the above example. 

A user can easily compile, test, or run a sub-project individually by using the projects command in the Play console:

[my-first-application] $ projects
[info] In file:/Volumes/Data/gbo/myFirstApp/
[info]  * my-first-app
[info]    my-library

 

The project with the variable name that appears first in the alphabet is the default project. Your main project can be created by giving it's variable the name aaaMain. Use the project command to modify the current project:

[my-first-app] $ project my-library
[info] Set current project to my-library
>

 

The dependent projects are automatically recompiled when you run your Play app in dev mode. And if something fails to compile, you will see the below message in your browser:
 

compilation error

Splitting the Web App😃

In the "Working with sub-projects" series, our next topic is Splitting the web app. Here, we will discuss the steps like build config, project structure, etc.

Splitting the Web App

A Play app can depend on another Play app as it is simply a regular sbt project with a default setup. Depending on whether the project is a Java or Scala project, you can turn any sub-module into a Play app by including the PlayJava or PlayScala plugins in its corresponding build.sbt file.

Splitting the Route File

If the user wants to create a robust, reusable Multi module Play app, then they can split the route file into smaller pieces.

To split the route file, we have to follow the below process:

Build Configuration

Follow the configuration in the build.sbt folder.

name := "myproject"


lazy val admin = (project in file("modules/admin")).enablePlugins(PlayScala)


lazy val main = (project in file("."))
    .enablePlugins(PlayScala).dependsOn(admin).aggregate(admin)


And do the same in modules/admin/build.sbt: 


name := "myadmin"


libraryDependencies ++= Seq(
  "mysql" % "mysql-connector-java" % "5.1.41",
  jdbc,
  anorm
)

 

Project Structure

The next step is to learn the project structure. Follow the below map for this:

 

build.sbt

app

  └ controllers

  └ models

  └ views

conf

  └ application.conf

  └ routes

modules

  └ admin

    └ build.sbt

    └ conf

      └ admin.routes

    └ app

      └ controllers

      └ models

      └ views

project

  └ build.properties

  └ plugins.sbt
 

The name of the Configuration and route file must be unique in the whole project. "admin.routes" is the name of the admin route file. Using a specific set of settings in development mode for a sub-project in the build file would be better. For example: PlayKeys.devSettings += ("play.http.router", "admin.Routes").

Use the below command in the conf/routes file:

GET /index                  controllers.HomeController.index()
->  /admin admin.Routes
GET     /assets/*file       controllers.Assets.at(path="/public", file)

 

Similarly for the modules/admin/conf/admin.routes:

GET /index                  controllers.admin.HomeController.index()
GET /assets/*file           controllers.Assets.at(path="/public/lib/myadmin", file)

 

Assets and controller classes

It is necessary that all Assets and controller classes should be defined in the controllers.admin package. 

Make a note that all the codes are in Java language. 

 

For Assets:

package controllers.admin;


import play.api.mvc.*;
import controllers.AssetsMetadata;
import play.api.http.HttpErrorHandler;


import javax.inject.Inject;


public class Assets extends controllers.Assets {


  @Inject
  public Assets(HttpErrorHandler errorHandler, AssetsMetadata meta) {
    super(errorHandler, meta);
  }


  public Action<AnyContent> at(String path, String file) {
    boolean aggressiveCaching = true;
    return super.at(path, file, aggressiveCaching);
  }
}

 

For Controller:

package controllers.admin;


import play.mvc.Controller;
import play.mvc.Result;


public class HomeController extends Controller {
  public Result index() {
    return ok("admin");
  }
}

 

Reverse Routing 🔁

We can reverse the routing in both Assets and Controller in the admin.

For Assets:

controllers.admin.routes.Assets.at("...")

 

For Controller:

controllers.admin.routes.HomeController.index

Sharing common codes and variables🔗🤝🏻

Sharing common codes and variables

 

We will now discuss how to share codes and variables in the  "Working with sub-projects" series.

You can put these in a Scala file in the project directory of the root project if you want your sub-projects and root projects to share specific standard settings or code. For instance, project/Common.scala could have the following:

import sbt._
import Keys._


object Common {
  val settings: Seq[Setting[_]] = Seq(
    organization := "com.example",
    version := "1.2.3-SNAPSHOT"
  )
  val fooDependency = "com.foo" %% "foo" % "2.4"
}

Advantages of using the Play framework😯

Using the Play framework has a lot of advantages like:
 

  • High productivity web development
  • Reactive programming
  • Satisfies enterprise needs
  • Scalable 
  • Large team application
  • Security
  • Traditional data access

Frequently Asked Questions💡

faqs

What is an activator in the Play framework?

A new Play app can be made with the activator command. You can choose a template in Activator to base your new app. These templates are available for plain Play projects under the names play-scala for Scala-based Play apps and play-java for Java-based Play apps.

What is the Play framework in Scala?

Scala & Java dev can create web apps quickly and easily with Play Framework. Play's architecture is lightweight, stateless, and suitable for the web. Play, based on Akka, gives highly scalable apps predictable and low resource consumption like CPU, memory, and threads.

What is sbt in the Play framework?

sbt files specify project settings. Also, as stated in the sbt manual, you can create your own unique custom parameters for your project. Knowing the sbt settings, in particular, is helpful.

Conclusion🎉

We have discussed the topic of Working with sub-projects. We have seen how to add a library, share common codes and variables, and steps to split the web app. 

We hope this blog has helped you enhance your knowledge of "Working with sub-projects." If you want to learn more, check out our articles Aggregating Reverse RoutersAnatomy of a Play applicationCoordinated Shutdown, and many more on our platform Coding Ninjas Studio.

But suppose you have just started your learning process and are looking for questions from tech giants like Amazon, Microsoft, Uber, etc. In that case, you must look at the problemsinterview experiences, and interview bundle for placement preparations.

However, you may consider our paid courses to give your career an edge over others!

Happy Learning!

thankyou

Live masterclass