sbt - the interactive build tool
TRANSCRIPT
sbtThe interactive build tool
Bruno Grasselli, Fyber, Zulu Team
What is sbt?● sbt is a build tool for Scala, Java, and more● Written in scala● Many Scala conveniences
Build automationBuild automation is the act of scripting or automating tasks that software developers do in their day-to-day activities
Examples of tasks● compiling● packing● running automated tests● console
For ruby developerssbt = rake + bundler + rvm + ...
How to install?● Download zip or tgz and expand it● RPM and DEB packages are officially
supported● brew install sbt
Hello worldA valid sbt project can be a directory containing a single source file.
hello/hello.scalaobject Hi { def main(args: Array[String]) = println("Hi!")}
$ sbt…> run...Hi!
Hello world
hello/build.sbtname := "hello"
version := "1.0"
scalaVersion := "2.10.3"
Installing scalasbt will install a different version of scala if needed
hello/project/build.properties
sbt.version=0.13.6
Directory structuresrc/ main/ resources/ <files to include in main jar here> scala/ <main Scala sources> java/ <main Java sources> test/ resources <files to include in test jar here> scala/ <test Scala sources> java/ <test Java sources>
Using sbt● Interactive mode● Batch mode
Interactive mode$ sbt...> compile[info] Compiling 1 Scala source to /Users/grasselli/projects/scala/hello/target/scala-2.10/classes...[success] Total time: 3 s, completed Oct 26, 2014 3:01:55 PM
Interactive mode$ sbt...> ~compile…1. Waiting for source changes... (press enter to interrupt)
Batch mode$ sbt compile[info] Set current project to hello (in build file:/Users/grasselli/projects/scala/hello/)[success] Total time: 1 s, completed Oct 26, 2014 3:07:39 PM
Batch mode$ sbt ~compile…1. Waiting for source changes... (press enter to interrupt)
Common commands● clean● compile● test● console
● run● package● reload● help
History commands> !History commands: !! Execute the last command again !: Show all previous commands !:n Show the last n commands !n Execute the command with index n, as shown by the !: command !-n Execute the nth command before this one !string Execute the most recent command starting with 'string' !?string Execute the most recent command containing 'string'
Build definitionAfter examining a project and processing build definition files, sbt ends up with an immutable map (set of key-value pairs) describing the build.
Build definition● .sbt files located in the base directory● .scala files located in project/
build.sbtname := "hello"
Getting it back$ sbt> name[info] hello
Types of keys● SettingKey● TaskKey● InputKey
SettingKeyIt is a key for a value computed once
TaskKeyIt is a key for a value, called a task, that has to be recomputed each time
InputKeyIt is a key for a task that has command line arguments as input
Task vs Setting keysA TaskKey[T] is said to define a task. Tasks are operations such as compile or package.
Custom keyslazy val hello = taskKey[Unit]("An example task")
hello := { println("Hello!") }
Calling hello$ sbt...> helloHello![success] Total time: 0 s, completed Oct 26, 2014 4:12:03 PM
ScopesEach key can have an associated value in more than one context, called a “scope.”
Scope axes● Project● Configurations
o Compileo Testo Runtime
● Task
Display formatOn the command line and in interactive mode, sbt displays (and parses) scoped keys like this:
{<build-uri>}<project-id>/config:intask::key
fullClasspath> inspect fullClasspath...[info] Provided by:{file:/Users/grasselli/projects/scala/hello/}hello/compile:fullClasspath...
test:fullClasspath> inspect test:fullClasspath...[info] Provided by:{file:/Users/grasselli/projects/scala/hello/}hello/test:fullClasspath...
Referring to scopesIf you create a setting in build.sbt with a bare key, it will be scoped to the current project, configuration Global and task Global:
name := "hello"
Referring to scopesname in Compile := "hello"
name in packageBin := "hello"
name in (Compile, packageBin) := "hello"
name in Global := "hello"
Library dependencies● Unmanaged● Managed
Unmanaged dependenciesJust add jars to lib/
Managed dependenciessbt uses Apache Ivy to implement managed dependencies:
https://ant.apache.org/ivy/
Adding dependencieslibraryDependencies += groupID % artifactID % revision
libraryDependencies += "org.apache.derby" % "derby" % "10.4.1.3"
Downloading...It will download and install the dependency as soon as you run your project for the first time after the change.
Adding more than onelibraryDependencies ++= Seq( groupID % artifactID % revision, groupID % otherID % otherRevision)
Scala version with %%libraryDependencies += "org.scala-tools" % "scala-stm_2.11.1" % "0.3"
libraryDependencies += "org.scala-tools" %% "scala-stm" % "0.3"
Ivy revisions"latest.integration", "2.9.+"
https://ant.apache.org/ivy/history/2.3.0/ivyfile/dependency.html#revision
ResolversThe dependency might not be in the central repositories server.
resolvers += name at location
Resolver for Maven2 repositories:
resolvers += "Sonatype OSS Snapshots" at "https://oss.sonatype.org/content/repositories/snapshots"
Per-configuration dependencieslibraryDependencies += "org.apache.derby" % "derby" % "10.4.1.3" % "test"
libraryDependencies += "org.apache.derby" % "derby" % "10.4.1.3" % Test
PluginsA plugin extends the build definition, most commonly by adding new settings.
Adding a pluginIf you’re adding sbt-assembly, create hello/project/assembly.sbt with the following:
addSbtPlugin("com.timushev.sbt" % "sbt-updates" % "0.1.6")
Global plugins~/.sbt/0.13/plugins/ is added to the path of all projects
For example ~/.sbt/0.13/plugins/plugins.sbt
addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "2.5.0")
Available pluginsThere’s a list of available plugins:
http://www.scala-sbt.org/0.13/docs/Community-Plugins.html
Questions?
CreditsThis presentation was heavily based on the sbt tutorial:
http://www.scala-sbt.org/0.13/tutorial/index.html
Thanks!