Top Banner
sbt And all that jazz
23
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: SBT by Aform Research, Saulius Valatka

sbtAnd all that jazz

Page 2: SBT by Aform Research, Saulius Valatka

Building scala/Java projects

• Option #1: scalac/javac

scalac HelloWorld.scala // outputs .class files

• Option #2: build system• Ant (old and lame)

• Maven (too much XML)

• Ivy (limited)

• sbt (powerful)

Page 3: SBT by Aform Research, Saulius Valatka

End result

• .class files contain bytecode (directory structure <-> packages)

• .jar files are zipped up .class files

• MANIFEST files can specify the main class in a .jar

• run with java jar yourfile.jar

Page 4: SBT by Aform Research, Saulius Valatka

Maven – the bad part

Page 5: SBT by Aform Research, Saulius Valatka

Maven – the good parts

• Source code structure:

• src

• main• java

• com/adform/dspr

• Processor.Java• test

• java

• com/adform/dsrp• ProcessorTest.Java

• target

• pom.xml• README.md

Page 6: SBT by Aform Research, Saulius Valatka

Ivy/Maven – the good parts

• Dependency management

<dependency>

<groupId>com.twitter</groupId>

<artifactId>scalding-core_2.9.2</artifactId>

<version>0.8.4</version>

</dependency>

• Libraries make graphs of dependencies

• JARs are published to repositories ( http://search.maven.org/ )

• Maven resolves/downloads everything!

Page 7: SBT by Aform Research, Saulius Valatka

Library dependencies

Page 8: SBT by Aform Research, Saulius Valatka

sbt

• Simple (haha!) Build Tool

• Can compile both Java and Scala projects

• Build definitions are scala programs

• Uses Ivy dependency management

Page 9: SBT by Aform Research, Saulius Valatka

sbt – project layout

• src• main

• scala• com/adform/dspr

• Processor.scala• test

• scala• com/adform/dsrp

• ProcessorTest.scala

• target

• project• target• Build.scala• plugins.sbt• build.properties

• build.sbt

• version.sbt

• README.md

Page 10: SBT by Aform Research, Saulius Valatka

Minimal build.sbt

organization := "com.adform.dspr"

name := "vertica-udfs"

scalaVersion := "2.11.2"

resolvers ++= Seq(

"Concurrent Maven Repo" at "http://conjars.org/repo",

)

libraryDependencies ++= Seq(

"io.argonaut" % "argonaut_2.10" % "6.0.4"

)

Page 11: SBT by Aform Research, Saulius Valatka

Usage

• sbt test/compile/run/package

• sbt ~compile/~test

• sbtreload

compile

run

Page 12: SBT by Aform Research, Saulius Valatka

Some (very simplified) Theory

• A build is a Seq[Setting[T]]

• A Setting[T] is either a property or a Task

• e.g. name = SettingKey[String]("name", "Project name.")

• e.g. compile = TaskKey[Analysis]("compile", "Compiles sources.")

• A build definition transforms the Settings

• e.g. name := "vertica-udfs” [def :=(v: T): Setting[T]]

Page 13: SBT by Aform Research, Saulius Valatka

Some (very simplified) Theory

• You can define your own tasks/settings

• e.g. • lazy val hello = taskKey[Unit]("An example task")

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

• sbt hello

• Settings/Tasks have scopes

• e.g. name in Compile := "hello"

Page 14: SBT by Aform Research, Saulius Valatka

Some (very simplified) Theory

• Settings can also be modified with other operators:• libraryDependencies ++= Seq(…)

• resolvers += “Repository” at “http://repo.lt/”

• publishTo <<= version { v => “repository/” + v }

Page 15: SBT by Aform Research, Saulius Valatka

More details

• A build consists of project/*.scala + *.sbt files

• Sample project/Build.scala:

import sbt._

import Keys._

object HelloBuild extends Build {

val sampleKeyB = settingKey[String]("demo key B")

lazy val root = Project(id = "hello",

base = file("."),

settings = Seq(

sampleKeyB := "B: in the root project settings in Build.scala"

))

}

Page 16: SBT by Aform Research, Saulius Valatka

More details

• .scala builds useful for multi-project builds

• Also useful for shared code across .sbt files

• .sbt take precedence over project/.scala!

• project/build.properties used to specify sbt version

Page 17: SBT by Aform Research, Saulius Valatka

Plot twist!

• How do you build the build project ?

Page 18: SBT by Aform Research, Saulius Valatka

Plot twist!

• How do you build the build project ?

• With sbt!

• project is the root folder of the build application

• plugins.sbt is the definition of the build of the build!

Page 19: SBT by Aform Research, Saulius Valatka

Plugins

• They are just library dependencies for the build project!

• Useful plugins: assembly, dependency-graph

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

Page 20: SBT by Aform Research, Saulius Valatka

Library dependencies

libraryDependencies ++= Seq(

"org.scalatest" %% "scalatest" % "2.2.2" % "test“

)

%% === % “scalatest_2.10”

% “test” === use only in test scope

Page 21: SBT by Aform Research, Saulius Valatka

Library dependencies

resolvers ++= Seq(

"Twitter Maven" at "http://maven.twttr.com",

"Adform Repo" at "http://ec2-54-74-8-112.eu-west-1.compute.amazonaws.com/repository/releases“

)

Dependencies are also cached in ~/.ivy2 – clear cache if something is off!

Page 22: SBT by Aform Research, Saulius Valatka

DEMO TIME !!!

Page 23: SBT by Aform Research, Saulius Valatka

QUESTIONS !!?!11