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.

Sbt for mere mortals


Published on

  • Be the first to comment

Sbt for mere mortals

  1. 1. SBTfor mere mortals
  2. 2. Whats inside?● Where did my XML go?● How does it work?● Where do the files go● Whats up with all the operators?● Creating plugins● And now?● Wordnik services
  3. 3. Where did my XML go?Short answer:awayLong answer:I hope it stays there
  4. 4. How does it work● Keys ○ Names for properties: setting key ○ Names for tasks: task key, input task key● Containers for keys: ○ projects ○ build ○ plugin● Configurations: ○ Kind of like package names in regular code in that they provide namespacing ○ Can have different classpath and dependency information
  5. 5. How does it work?Scopes really tie the thing together.Keys get values attached to them at scopeaxesSaying: name := "awesomest-project-ever"is really the same as saying name in This := "awesomest-..."the task compile refers to:compile in (Compile, This)
  6. 6. Where do the files go./*.sbt and project/*.scala both belong to the same build.The ./*.sbt files provide settings for theproject defined with base = file(".")module/something/*.sbt provides settings fora submodule defined in project/build.scalawith base = file("modules/something")
  7. 7. Where do the files goGlobal settings for a user:~/.sbt/sbt.version/*.sbtGlobal plugins for a user:~/.sbt/sbt.version//plugins/*.sbt and ~/.sbt/sbt.version//plugin/project/*.scalaPlugins local to a project:project/*.sbt and project/project/*.scala
  8. 8. Whats up with all the operators? := set a value of a property from a value += add a value to a key that is a list++= add a list of values to a key that is a list ~= update a value with a function T => T<<= set a value of a property from a sbt value<+= add a value of a property from a sbt value<++= adds a list of values like <+=
  9. 9. Creating a projectEither use ./build.sbt directlyor create a project/build.scalaobject MyBuild extends Build { lazy val root = Project( id = "friendly-name-for-project", base = file(".") // place to look for files )}
  10. 10. Adding dependenciesRequiring full name with version modifier:"org.json4s" % "json4s-jackson_2.10" %"3.1.0"Name only, no version modifier:"org.json4s" %% "json4s-jackson" % "3.1.0"
  11. 11. DependenciesExcluding items"" % "aws-jdk" % "3.3.1" exclude("org.apache.httpcomponents", "httpclient")Intransitive"com.mongodb" %% "casbah" % "2.5.0" instransitive()Classifier"com.mongodb" %% "casbah" % "2.5.0" classifier("wordnik")
  12. 12. Creating a pluginwriting an sbt plugin or a build file are thesame thing. The plugin is reusable andsharable across many projects. Other thanthat they are the same.If you can write it in a class you can write it insbt, its a matter of seeing the translation andbreaking it apart in context + behavior, likemost other things Scala.
  13. 13. Creating a pluginclass Greeter(greeting: String) { def greet( name: String, from: String, log: String => Unit) = log( greeting + " " + name + " from " + from)}
  14. 14. Creating a pluginKeys: val objKey = SettingKey[Greeter]("greeter") val taskKey = TaskKey[Unit]("greet")Settings: objKey := new Greeter("Hello") name in taskKey := "Ivan" taskKey <<= (name in taskKey, name, objKey,streams) {(theName, from, obj, s) => obj.greet(theName, from, }
  15. 15. Creating a pluginWrap those up in an object that extendsPlugin and put those settings in a Seq[Setting[_]] and youre good to go, youve gota plugin.
  16. 16. Creating a pluginHOMEWORK:Try to create a plugin that will display thelatest tweet for a user given a user namefrom an sbt setting.or instead of a tweet, try downloading the listof open issues from jira when credentials areprovided.
  17. 17. And now?Go forth and build awesome stuff?Youll probably want to look at the sbt.Keysfile, to see what all the keys are that arecurrently defined.Read the source of many other plugins, sothat they no longer appear to do magic.