Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Getting started with sbt

311 views

Published on

An introductory tutorial to SBT

Published in: Software
  • Be the first to comment

Getting started with sbt

  1. 1. Ikenna Consulting ikenna@ikenna.net Twitter: @ikennacn Getting Started with SBT Ikenna Nwaiwu
  2. 2. Ikenna Consulting ikenna@ikenna.net Twitter: @ikennacn What is SBT ?
  3. 3. Ikenna Consulting ikenna@ikenna.net Twitter: @ikennacn Objectives • Compile and run code with SBT • Add managed dependencies to your project • Run tests • Publish to a binary repository • Use a plugin • Assemble an run a project jar • Open an SBT project in Intellij • Write and SBT task
  4. 4. Ikenna Consulting ikenna@ikenna.net Twitter: @ikennacn Task 1:Write a HelloWorld file • Create a HelloWorld programe in file “HelloWorld.scala” • Compile it with >scalac • Run it with >scala HelloWorld
  5. 5. Ikenna Consulting ikenna@ikenna.net Twitter: @ikennacn Task 2: Now lets use SBT for building • Create a file build.sbt • Add the following to your sbt file • name := "Helloworld project” • version := "1.0" • - Double space between lines • Now start sbt in the directory and run >compile and >run • >exit
  6. 6. Ikenna Consulting ikenna@ikenna.net Twitter: @ikennacn Task 2: SBT for building – ignoring the target directory • SBT puts in generated classes and files in the target directory • You can clean this directory : >clean • If you use version control, you probably don’t want these files versioned. Ignore them with .gitignore $>’echo target/ ‘ > .gitignore $>git init $>git status
  7. 7. Ikenna Consulting ikenna@ikenna.net Twitter: @ikennacn Task 3: Lets create the project directory structure • Move your HelloWorlds.scala file to the src/main/scala folder lib/ src/ main/ resources/ scala/ java/ test/ resources/ scala/ java/
  8. 8. Ikenna Consulting ikenna@ikenna.net Twitter: @ikennacn Task 4: Add a project dependency • Got to http://www.scalatest.org/install and add the scalatest project dependency. • Run > reload • Run > update • Run > test
  9. 9. Ikenna Consulting ikenna@ikenna.net Twitter: @ikennacn Task 5: Write a test and run it import org.scalatest.{FunSuite, Matchers} class HelloWorldTest extends FunSuite with Matchers { test("Multiplication"){ val result = HelloWorld.multiply(3, 4) result shouldBe 12 } } • Create a file HelloWorldTest in src/test/scala/HelloWorldTest.scala • Now run > test • What do you get ? • Implement the missing method in HelloWorld.scala • Now run tests again
  10. 10. Ikenna Consulting ikenna@ikenna.net Twitter: @ikennacn Task 6: Using the console and help • The SBT console loads your project and allows you invoke your methods and functions from the command line • >console • >scala>:quit • >help • >help reload
  11. 11. Ikenna Consulting ikenna@ikenna.net Twitter: @ikennacn Task 6: Publishing your project locally • Publishing a project: uploading a descriptor (Ivy file or Maven POM) and jar , to a repository so that other projects can specify your project as a dependency. • Add the following to your build.sbt organization := ”com.lagosscala" • >publishLocal • You can now create another project and include your HelloWorld code as a dependency with libraryDependencies += ”com.lagosscala” %% "helloworld-project" % “1.0”
  12. 12. Ikenna Consulting ikenna@ikenna.net Twitter: @ikennacn Task 7: Adding a plugin to your project • A plugin can be used to add more tasks to your project • Lets add a plugin that packages our project with all the dependencies required for executing it • sbt assembly - https://github.com/sbt/sbt-assembly • Create file project/assembly.sbt and add the following addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.14.3”) • Now reload, update, and run >assembly • Note the path of the created assembly jar. Can you run it with java –jar /path/to/project-assembly.jar ?
  13. 13. Ikenna Consulting ikenna@ikenna.net Twitter: @ikennacn Task 8: Open the project in Intellij • File > Open > Project • Use the ‘rebuild’ in the sbt pane to reload the project when you need to. ‘Auto import’ detects file changes and does this automatically as well.
  14. 14. Ikenna Consulting ikenna@ikenna.net Twitter: @ikennacn Sbt Concepts: Settings • An SBT project consists of an immutable map of key- value pairs • An sbt build definition file, lists Settings (objects of Setting[T]).These are a list of transformations that can be applied to the sbt project map. For example, they can add an entry to a map or transform an existing entry. • The sbt build definition is a list of ‘Setting’s. • A Setting is created by calling methods :=, +=, or ++= on a SettingKey and assigning a value. • There are 3 kinds of Keys = SettingKey[T], InputKey[T] andTaskKey[T]. • SettingKeys – key for values computed once • TaskKey – key for value recomputed each time • InputKey – key for task that takes command line arguments as input • There are built in keys that come with sbt and these are defined in sbt.Keys._ • You can create a custom key and use that for custom Settings e.g: • lazy val helloWorld = taskKey[Unit](“My Hello World set task“) • hello := { println("Hello World!”) }

×