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.

D:\parent>sbt
[info] Set current project to default-61ca07 (in build file:/D:/parent/)
> set name :="parent"
[info] Reapplying settings...
[info] Set current project to parent (in build file:/D:/parent/)
> set scalaVersion :="2.9.2"
[info] Reapplying settings...
[info] Set current project to parent (in build file:/D:/parent/)
> set version :="4.0"
[info] Reapplying settings...
[info] Set current project to parent (in build file:/D:/parent/)
> session save
[info] Reapplying settings...
[info] Set current project to parent (in build file:/D:/parent/)
> exit

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.

import sbt._
import Keys._
object SbtMultiBuild extends Build {
    lazy val parent = Project(id = "parent",
                            base = file(".")) aggregate(child1, child2)
    lazy val child1 = Project(id = "child1",
                           base = file("child1"))
    lazy val child2 = Project(id = "child2",
                           base = file("child2"))
}

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.

D:\parent\child1>sbt
[info] Set current project to default-947bc6 (in build file:/D:/parent/child1/)
> set name :="child1"
[info] Reapplying settings...
[info] Set current project to child1 (in build file:/D:/parent/child1/)
> set version :="2.0"
[info] Reapplying settings...
[info] Set current project to child1 (in build file:/D:/parent/child1/)
> set scalaVersion :="2.9.2"
[info] Reapplying settings...
[info] Set current project to child1 (in build file:/D:/parent/child1/)
> session save
[info] Reapplying settings...
[info] Set current project to child1 (in build file:/D:/parent/child1/)
> exit
D:\parent\child2>sbt
[info] Set current project to default-aa845c (in build file:/D:/parent/child2/)
> set name :="child2"
[info] Reapplying settings...
[info] Set current project to child2 (in build file:/D:/parent/child2/)
> set version :="3.0"
[info] Reapplying settings...
[info] Set current project to child2 (in build file:/D:/parent/child2/)
> set scalaVersion :="2.9.2"
[info] Reapplying settings...
[info] Set current project to child2 (in build file:/D:/parent/child2/)
> session save
[info] Reapplying settings...
[info] Set current project to child2 (in build file:/D:/parent/child2/)
> exit

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.

D:\parent>sbt clean compile eclipse
[info] Loading project definition from D:\parent\project
[info] Updating {file:/D:/parent/project/}default-cb6aeb...
[info] Resolving com.typesafe.sbteclipse#sbteclipse-plugin;2.0.0 ...
[info] Resolving com.typesafe.sbteclipse#sbteclipse-core;2.0.0 ...
[info] Resolving org.scala-sbt#precompiled-2_8_1;0.11.3 ...
[info] Resolving org.scala-sbt#precompiled-2_9_2;0.11.3 ...
[info] Done updating.
[info] Set current project to parent (in build file:/D:/parent/)
[success] Total time: 0 s, completed Jan 25, 2013 2:26:59 PM
[info] Updating {file:/D:/parent/}child2...
[info] Updating {file:/D:/parent/}child1...
[info] Resolving org.scala-lang#scala-library;2.9.2 ...
[info] Done updating.
[info] Resolving org.scala-lang#scala-library;2.9.2 ...
[info] Updating {file:/D:/parent/}parent...
[info] Done updating.
[info] Resolving org.scala-lang#scala-library;2.9.2 ...
[info] Done updating.
[success] Total time: 0 s, completed Jan 25, 2013 2:26:59 PM
[success] Total time: 0 s, completed Jan 25, 2013 2:26:59 PM
[info] About to create Eclipse project files for your project(s).
[info] Successfully created Eclipse project files for project(s): child2, parent
-child1

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.

> show version
[info] child1/*:version
[info]  2.0
[info] child2/*:version
[info]  3.0
[info] parent/*:version
[info]  4.0

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.

Advertisements

About anuj2013

Software Consultant At Knoldus Software LLP
This entry was posted in Scala and tagged , , . Bookmark the permalink.

2 Responses to Multi Project Builds with SBT 0.12 for Scala Project

  1. sentalker says:

    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!

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s