Multi Project Builds with SBT 0.12 for Scala Project

In my last post Create Scala Project with SBT. We had seen how it was easy with SBT to create a Scala project. In this post,we will learn multi Project Builds with SBT for Scala project.

It can be useful to keep multiple related projects in a single build. If they depend on one another and you tend to modify them together. Each sub-project in a build has its own SBT default directory. It is generate own build and works like any other project.

I have a main project parent and two sub-project child1 and child2. Lets quickly started with creating parent. In order to create project execute the following commands in the sbt session.

If we have multiple projects, we must declare each project and how they relate in Build.scala file. We can define settings for each project in build.sbt file. Here’s an example of a Build.scala file which defines a root project parent, where the parent project aggregates two sub-projects, child1 and child2. Which resides under parent/project/Build.scala.

Projects in the build can be completely independent of one another. For Multi Project Builds we call a method aggregate(child1, child2). This aggregates child1 and child2 underneath the parent project.
Aggregation will run the aggregated tasks in parallel and with no defined ordering.

Note : If we execute sbt clean compile command on console then it create two sub-directory child1 and child2 by default.It is not contain any project specific setting.

If we want project specific setting then executing following commands in the sbt session for sub-project.

If we compile our project from root then we can see with main project, sub-project also compile parallely, It is help full for large project which takes more time in compilation. So for resolving this problem we can develop our project under Multi Project Builds. By executing sbt compile command on console we can see
updating all projects at a time.

My whole project is in parent and i defined different version in parent\build.sbt ,parent\child1\build.sbt and parent\child2\build.sbt. Now we can check project version by executing show version command on sbt session.

My project directory contains following sub directories :
DirImage
The project is ready to import in Eclipse IDE.That is all we need for creating Scala project using the Multi Project Buils with SBT.

2 thoughts on “Multi Project Builds with SBT 0.12 for Scala Project

  1. Thanks, this is really useful. Will try it out. I have a couple of questions though:
    * when importing to Eclipse, which project(s) would be imported? (3 projects, 2 projects, or 1 project?)
    * how would you recommend to deal with revision control? check-in the child projects independently, or check-in the whole directory hierarchy?
    Thanks!

    1. Hi Sentalker,
      * all three project gets imported if you import the parent project ,For ex. parent is main project and child1,child2 are the sub-project. When you importing parent to Eclipse then parent,child1 and child2 are imported.

      * whole directory structure needs to be committed for revision control, github is an example of revision control.
      you can find project directory from github
      https://github.com/singhanuj88/MultiProjectBuildWithSBTForScalaProjects

Leave a Reply

%d bloggers like this: