Simple build tool


Published on

Simple Build Tool (sbt) is an open source build tool , It is a best choice for Scala projects that aims to do the basics well. It requires Java 1.6 or later.

Published in: Health & Medicine
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Simple build tool

  1. 1.    Simple Build Tool Anuj Pratap Singh Software Consultant Knoldus Software LLP
  2. 2. AGENDA What is Simple Build Tool (SBT) Installation Of SBT Features of SBT Creating a simple project with SBT Multi project build with SBT
  3. 3. What is SBT Simple Build Tool (sbt) is an open source build tool , It is abest choice for Scala projects that aims to do the basics well. Itrequires Java 1.6 or later.➢ SBT uses a small number of concepts to support flexible andpowerful build definitions.➢ An sbt build definition can contain files ending in .sbt.
  4. 4. Installation of SBT➢ Sbt comes pre-built with several available packages for different operating systems.➢ Mac : Use below command to install Sbt. $ port install sbt➢ Linux : Download sbt-launch.jar and place it in ~/bin and create a script for run above jar. java -Xms512M -Xmx1536M -Xss1M -XX:+CMSClassUnloadingEnabled -XX:MaxPermSize=384M -jar `dirname $0`/sbt-launch.jar "$@“ Windows : Create a batch file sbt.bat which contains following script $ set SCRIPT_DIR=%~dp0 $ java -Xmx512M -jar "%SCRIPT_DIR %sbt-launch.jar" %*
  5. 5. Features Of SBT Native support for compiling Scala code and integrating with manyScala test frameworks . Build descriptions written in Scala using a DSL (Domain SpecificLanguage). Support for mixed Java/Scala projects . Multi project build support Does dependency management Interactive shell
  6. 6. Creating SBT ProjectProjects will need some manual setup. Basic build settings go in a filecalled build.sbt, located in the projects base directory.For example, my project is in the directory sbtDemo, insbtDemo/build.sbt I have to write:D:>mkdir sbtDemoD:>cd sbtDemoD:sbtDemo>sbt[info] Set current project to default-d0025a (in buildfile:/D:/sbtDemo/)>
  7. 7. Build DefnitionBasic build setting giving as below on sbt console :D:sbtDemo>sbt[info] Set current project to default-d0025a (in build file:/D:/sbtDemo/)> set name :="hello"[info] Reapplying settings...[info] Set current project to hello (in build file:/D:/sbtDemo/)> set version :="1.0"[info] Reapplying settings...[info] Set current project to hello (in build file:/D:/sbtDemo/)> set scalaVersion :="2.9.2"[info] Reapplying settings...[info] Set current project to hello (in build file:/D:/sbtDemo/)> session save[info] Reapplying settings...[info] Set current project to hello (in build file:/D:/sbtDemo/)> exitD:sbtDemo>
  8. 8. ….....continueIn previous setting, On the left, name, version, and scalaVersion arekeys. A key is an instance of SettingKey[T], TaskKey[T], or InputKey[T]where T is the expected value type.Note : we should also be add resolver in build.sbt file .resolvers += "Typesafe Repo" at "" Settings are separated by blank linesIf we open build.sbt file then we noticed the blank line between everyitem. This isnt just for show; theyre actually required in order toseparate each item.sbt needs some kind of delimiter to tell where one expression stopsand the next begins.Settings are key initialization pair , for ex. name := “sbtDemo” Key Initialization
  9. 9. Types Of KeysThe built-in keys are just fields in an object called Keys.A build.sbt implicitly has an import sbt.Keys._, so can bereferred to as name.There are three flavors of key: Setting Key[T]: a key with a value computed once (the value is computedone time when loading the project, and kept around). Task Key[T]: a key with a value that has to be recomputed each time,potentially creating side effects and its said to define a task. Input Key[T]: a task key which has command line arguments as input.
  10. 10. Adding library dependenciesTo depend on third-party libraries, there are two options. The first is todrop jars in lib/ (unmanaged dependencies) and the other is to add manageddependencies, which will look like this in build.sbt :// Add a single dependency, libraryDependencies += "junit" % "junit" % "4.8" % "test"// Add multiple dependencies. libraryDependencies ++= Seq( "net.databinder" %% "dispatch-google" %"0.7.8", "net.databinder" %% "dispatch-meetup" % "0.7.8"
  11. 11. Adding PluginsA plugin extends the build definition, most commonly by adding newsettings. The new settings could be new tasks.My project is in directory sbtDemo, add a plugins.sbt file undersbtDemo/project/plugins.sbt and add the plugin location as a resolver,then call addSbtPluginaddSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "2.0.0")Note : If the plugin isn’t located in one of the default repositories, wewould have to add a resolver ,For ex.resolvers += Classpaths.typesafeResolver
  12. 12. .....continueWe add plugin for eclipse, now execute command :> sbt eclipse[info] Loading project definition from D:sbtDemoproject[info] Updating {file:/D:/sbtDemo/project/}default-8bffc2...[info] Resolving com.typesafe.sbteclipse#sbteclipse-plugin;2.0.0 ...[info] Resolving com.typesafe.sbteclipse#sbteclipse-core;2.0.0 ...[info] Resolving org.scalaz#scalaz-core_2.9.1;6.0.3 ...[info] Resolving org.scala-lang#scala-library;2.9.1 ………All eclipse dependencies are resolved , now my project is ready toimport on eclipse.
  13. 13. Directory Structure of Sbt ProjectBase Directory : In sbts terminology, the "base directory" is the directory containing theproject. So if we created a project sbtDemo , its containing sbtDemo/build.sbt file andother directories.Source code : Source code can be placed in the projects base directory aswith sbtDemo/hello.scala.
  14. 14. Multi Project BuildIt can be useful to keep multiple related projects in a single build,especially if they depend on one another and you tend to modify themtogether.Each sub-project in a build has its own src/main/scala, generates its ownjar file when you run package, and in general works like any otherproject.
  15. 15. Defining projects in a .scala fileTo have multiple projects, you must declare each project and how they relatein a Build.scala file; theres no way to do it in a .sbt file. However, you candefine settings for each project in .sbt files. Heres an example of aBuild.scala file which defines a root project parent, where the root projectaggregates two sub-projects, child1 and child2 :import sbt._import Keys._object SbtMultiBuild extends Build { lazy val parent = Project(id = "sbtmDemo", base = file(".")) aggregate(ch1, ch2) lazy val ch1 = Project(id = "c1", base = file("child1")) lazy val ch2 = Project(id = "c2", base = file("child2"))}
  16. 16. …. continuesbt finds the list of Project objects using reflection, looking for fields withtype Project in the Build object.For Ex. : 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 thefollowing commands in the sbt session.D:parent>sbt>set name :="parent“>set scalaVersion :="2.9.2“>Set version :="4.0“>session save> exit
  17. 17. ………ContinueProjects in the build can be completely independent of one another. For MultiProject Builds we call a method aggregate(child1, child2).Note : If we execute sbt clean compile command on console then it create twosub-directory child1 and child2 by default.It is not contain any project specificsetting.If we want project specific setting then executing following commands in thesbt session for sub-project.D:parentchild1>sbt>set name :="child1”>set version :=“1.0”>set scalaVersion :="2.9.2”> session save> exit
  18. 18. ……ContinueD:parentchild2>sbt> set name :="child2"> set version :=“2.0"> set scalaVersion :="2.9.2"> session save> exit
  19. 19. …… ContinueIf 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 takesmore time in compilation. So for resolving this problem we can develop ourproject under Multi Project Builds. By executing sbt compile command onconsole we can see updating all projects at a time.D:parent>sbt clean compile eclipse[info] Loading project definition from D:parentproject[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] Done updating.[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
  20. 20. ……ContinueMy whole project is in parent and I defined different version inparentbuild.sbt ,parentchild1build.sbt and parentchild2build.sbt. Nowwe can check project version by executing show version command on sbtsession :D:sbtmDemo>sbt[info] Loading project definition from D:sbtmDemoproject[info] Set current project to parent (in build file:/D:/sbtmDemo/)> show version[info] c1/*:version[info] 1.0[info] c2/*:version[info] 2.0[info] sbtmDemo/*:version[info] 4.0>
  21. 21. AggregationAggregation means that running a task on the aggregate project ,aggregation will run the aggregated tasks in parallel and with no definedordering. Start up sbt with two subprojects as in the example, andtry compile. We can see that all three projects are compiled at sametime.In the previous example, we can see the methodcall aggregate(ch1,ch2). This aggregates ch1 and ch2 underneath theparent project.aggregation will run the aggregated tasks in parallel and with no definedordering.
  22. 22. Classpath dependenciesA project may depend on code in another project. This is done by addinga dependsOn method call. For example, if child1 needed child3 on itsclasspath, then we would write in Build.scala:lazy val ch1 = Project(id = “c1", base = file(“child1"))dependsOn(child3)