sbt - the interactive build tool

55
sbt The interactive build tool Bruno Grasselli, Fyber, Zulu Team

Transcript of sbt - the interactive build tool

Page 1: sbt - the interactive build tool

sbtThe interactive build tool

Bruno Grasselli, Fyber, Zulu Team

Page 2: sbt - the interactive build tool

What is sbt?● sbt is a build tool for Scala, Java, and more● Written in scala● Many Scala conveniences

Page 3: sbt - the interactive build tool

Build automationBuild automation is the act of scripting or automating tasks that software developers do in their day-to-day activities

Page 4: sbt - the interactive build tool

Examples of tasks● compiling● packing● running automated tests● console

Page 5: sbt - the interactive build tool

For ruby developerssbt = rake + bundler + rvm + ...

Page 6: sbt - the interactive build tool

How to install?● Download zip or tgz and expand it● RPM and DEB packages are officially

supported● brew install sbt

Page 7: sbt - the interactive build tool

Hello worldA valid sbt project can be a directory containing a single source file.

Page 8: sbt - the interactive build tool

hello/hello.scalaobject Hi { def main(args: Array[String]) = println("Hi!")}

Page 9: sbt - the interactive build tool

$ sbt…> run...Hi!

Hello world

Page 10: sbt - the interactive build tool

hello/build.sbtname := "hello"

version := "1.0"

scalaVersion := "2.10.3"

Page 11: sbt - the interactive build tool

Installing scalasbt will install a different version of scala if needed

Page 12: sbt - the interactive build tool

hello/project/build.properties

sbt.version=0.13.6

Page 13: sbt - the interactive build tool

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>

Page 14: sbt - the interactive build tool

Using sbt● Interactive mode● Batch mode

Page 15: sbt - the interactive build tool

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

Page 16: sbt - the interactive build tool

Interactive mode$ sbt...> ~compile…1. Waiting for source changes... (press enter to interrupt)

Page 17: sbt - the interactive build tool

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

Page 18: sbt - the interactive build tool

Batch mode$ sbt ~compile…1. Waiting for source changes... (press enter to interrupt)

Page 19: sbt - the interactive build tool

Common commands● clean● compile● test● console

● run● package● reload● help

Page 20: sbt - the interactive build tool

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'

Page 21: sbt - the interactive build tool

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.

Page 22: sbt - the interactive build tool

Build definition● .sbt files located in the base directory● .scala files located in project/

Page 23: sbt - the interactive build tool

build.sbtname := "hello"

Page 24: sbt - the interactive build tool

Getting it back$ sbt> name[info] hello

Page 25: sbt - the interactive build tool

Types of keys● SettingKey● TaskKey● InputKey

Page 26: sbt - the interactive build tool

SettingKeyIt is a key for a value computed once

Page 27: sbt - the interactive build tool

TaskKeyIt is a key for a value, called a task, that has to be recomputed each time

Page 28: sbt - the interactive build tool

InputKeyIt is a key for a task that has command line arguments as input

Page 29: sbt - the interactive build tool

Task vs Setting keysA TaskKey[T] is said to define a task. Tasks are operations such as compile or package.

Page 30: sbt - the interactive build tool

Custom keyslazy val hello = taskKey[Unit]("An example task")

hello := { println("Hello!") }

Page 31: sbt - the interactive build tool

Calling hello$ sbt...> helloHello![success] Total time: 0 s, completed Oct 26, 2014 4:12:03 PM

Page 32: sbt - the interactive build tool

ScopesEach key can have an associated value in more than one context, called a “scope.”

Page 33: sbt - the interactive build tool

Scope axes● Project● Configurations

o Compileo Testo Runtime

● Task

Page 34: sbt - the interactive build tool

Display formatOn the command line and in interactive mode, sbt displays (and parses) scoped keys like this:

{<build-uri>}<project-id>/config:intask::key

Page 35: sbt - the interactive build tool

fullClasspath> inspect fullClasspath...[info] Provided by:{file:/Users/grasselli/projects/scala/hello/}hello/compile:fullClasspath...

Page 36: sbt - the interactive build tool

test:fullClasspath> inspect test:fullClasspath...[info] Provided by:{file:/Users/grasselli/projects/scala/hello/}hello/test:fullClasspath...

Page 37: sbt - the interactive build tool

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"

Page 38: sbt - the interactive build tool

Referring to scopesname in Compile := "hello"

name in packageBin := "hello"

name in (Compile, packageBin) := "hello"

name in Global := "hello"

Page 39: sbt - the interactive build tool

Library dependencies● Unmanaged● Managed

Page 40: sbt - the interactive build tool

Unmanaged dependenciesJust add jars to lib/

Page 41: sbt - the interactive build tool

Managed dependenciessbt uses Apache Ivy to implement managed dependencies:

https://ant.apache.org/ivy/

Page 42: sbt - the interactive build tool

Adding dependencieslibraryDependencies += groupID % artifactID % revision

libraryDependencies += "org.apache.derby" % "derby" % "10.4.1.3"

Page 43: sbt - the interactive build tool

Downloading...It will download and install the dependency as soon as you run your project for the first time after the change.

Page 44: sbt - the interactive build tool

Adding more than onelibraryDependencies ++= Seq( groupID % artifactID % revision, groupID % otherID % otherRevision)

Page 45: sbt - the interactive build tool

Scala version with %%libraryDependencies += "org.scala-tools" % "scala-stm_2.11.1" % "0.3"

libraryDependencies += "org.scala-tools" %% "scala-stm" % "0.3"

Page 46: sbt - the interactive build tool

Ivy revisions"latest.integration", "2.9.+"

https://ant.apache.org/ivy/history/2.3.0/ivyfile/dependency.html#revision

Page 47: sbt - the interactive build tool

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"

Page 48: sbt - the interactive build tool

Per-configuration dependencieslibraryDependencies += "org.apache.derby" % "derby" % "10.4.1.3" % "test"

libraryDependencies += "org.apache.derby" % "derby" % "10.4.1.3" % Test

Page 49: sbt - the interactive build tool

PluginsA plugin extends the build definition, most commonly by adding new settings.

Page 50: sbt - the interactive build tool

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")

Page 51: sbt - the interactive build tool

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")

Page 52: sbt - the interactive build tool

Available pluginsThere’s a list of available plugins:

http://www.scala-sbt.org/0.13/docs/Community-Plugins.html

Page 53: sbt - the interactive build tool

Questions?

Page 54: sbt - the interactive build tool

CreditsThis presentation was heavily based on the sbt tutorial:

http://www.scala-sbt.org/0.13/tutorial/index.html

Page 55: sbt - the interactive build tool

Thanks!