SBT
Tomer Ben David
Contents
Brief overview of SBT
SBT maven plugin for integration with MAVEN!
So we can reuse Maven Infrastructure
SBT - scala build tool
More teams are joining scala coding
Spark development is mostly done in scala
SBT got much inspired by maven
Modelling is good in maven, behaviour is somewhat harder (xml)
SBT can join forces with maven model in maven behaviour in scala
With richness comes complexity
You can run SBT with no code
Main object
run sbt run
it compile and runs
expected maven style directory structure
src/main/scala or java
define a project
build.sbt in root folder
lazy val root = (project in file(“.”)).
settings(
name := “hello”,
version := “1.0
project dir
can contain scala files
ie
Build.scala
Interactive mode
sbt console
Continuous build
~ compile
will compile upon changes
History commands
!
!!
Build definition
each project associated with immutable map key value pairs
keys have a method named :=
This method returns back Settings
build does not change map
it create settings which is a set of transformations on key value pairs
Three flavour of keys
SettingKey
TaskKey - computed each time potentially with side effects
compile, package they can have return value or no return value
InputKey
Built in keys
sbt.Keys._
Create example taskKey
lazy val hello = taskKey[Unit](“An example key”)
Assign value - operation to a task
task key
lazy val hello = taskKey[Unit](“an example task key”)
task value (operation)
lazy val root = (project in file(“.”)).
settings(
hello := { println(“hello”) }
)
“compile” is a task key
dependencies
val derby = “org.apache.derby” % “derby” % “10.4.1.3”
later on: libraryDependencies += derby
+= appends to the keys old value rather than replace
Scopes
in truth a key can have different value in different scopes
a key can have different value for each project
compile key can have different values for source and test source if you want to
compile them differently
packageOptions can have different values when packging class files or source
code
There is a single value for a given scoped key
Setting key in scopes
name in (Compile, packageBin) := “hello”
Set the source directory for compile
sourceDirectory in Compile += new File(“source”)
Refer to key’s value
organization := name.value
name := baseDirectory.value.getName
Examples
name := "99-scala-solutions"
version := "0.1"
description := "99 scala solutions"
startYear := Some(2015)
licenses += "Apache-2.0" -> url("http://www.apache.org/licenses/LICENSE-
2.0.html")
Examples
val commonScalacOptions = Seq(
"-encoding", "UTF-8" // Specify character encoding used by source files
, "-target:jvm-1.8" // Target platform for object files
, "-Xexperimental" // Enable experimental extensions
, "-Xfuture" // Turn on future language features
, "-Ybackend:GenBCode" // Choice of bytecode emitter
)
scalacOptions ++= commonScalacOptions ++ Seq(
"-deprecation" // Emit warning and location for usages of deprecated APIs
, "-feature" // Emit warning and location for usages of features that should be imported explicitly
, "-g:vars"
Examples
Examples
settings(
startServer := {
println(“starting”)
Thread.sleep(500)
then from sbt commandline
sbt> startServer
SBT Maven Plugin

Sbt

  • 1.
  • 2.
    Contents Brief overview ofSBT SBT maven plugin for integration with MAVEN! So we can reuse Maven Infrastructure
  • 3.
    SBT - scalabuild tool More teams are joining scala coding Spark development is mostly done in scala SBT got much inspired by maven Modelling is good in maven, behaviour is somewhat harder (xml) SBT can join forces with maven model in maven behaviour in scala With richness comes complexity
  • 4.
    You can runSBT with no code Main object run sbt run it compile and runs expected maven style directory structure src/main/scala or java
  • 5.
    define a project build.sbtin root folder lazy val root = (project in file(“.”)). settings( name := “hello”, version := “1.0
  • 6.
    project dir can containscala files ie Build.scala
  • 7.
  • 8.
    Continuous build ~ compile willcompile upon changes
  • 9.
  • 10.
    Build definition each projectassociated with immutable map key value pairs keys have a method named := This method returns back Settings build does not change map it create settings which is a set of transformations on key value pairs
  • 11.
    Three flavour ofkeys SettingKey TaskKey - computed each time potentially with side effects compile, package they can have return value or no return value InputKey
  • 12.
  • 13.
    Create example taskKey lazyval hello = taskKey[Unit](“An example key”)
  • 14.
    Assign value -operation to a task task key lazy val hello = taskKey[Unit](“an example task key”) task value (operation) lazy val root = (project in file(“.”)). settings( hello := { println(“hello”) } )
  • 15.
  • 16.
    dependencies val derby =“org.apache.derby” % “derby” % “10.4.1.3” later on: libraryDependencies += derby += appends to the keys old value rather than replace
  • 17.
    Scopes in truth akey can have different value in different scopes a key can have different value for each project compile key can have different values for source and test source if you want to compile them differently packageOptions can have different values when packging class files or source code There is a single value for a given scoped key
  • 18.
    Setting key inscopes name in (Compile, packageBin) := “hello”
  • 19.
    Set the sourcedirectory for compile sourceDirectory in Compile += new File(“source”)
  • 20.
    Refer to key’svalue organization := name.value name := baseDirectory.value.getName
  • 21.
    Examples name := "99-scala-solutions" version:= "0.1" description := "99 scala solutions" startYear := Some(2015) licenses += "Apache-2.0" -> url("http://www.apache.org/licenses/LICENSE- 2.0.html")
  • 22.
    Examples val commonScalacOptions =Seq( "-encoding", "UTF-8" // Specify character encoding used by source files , "-target:jvm-1.8" // Target platform for object files , "-Xexperimental" // Enable experimental extensions , "-Xfuture" // Turn on future language features , "-Ybackend:GenBCode" // Choice of bytecode emitter ) scalacOptions ++= commonScalacOptions ++ Seq( "-deprecation" // Emit warning and location for usages of deprecated APIs , "-feature" // Emit warning and location for usages of features that should be imported explicitly , "-g:vars"
  • 23.
  • 24.
  • 25.