Skip to content

luigixu/sbt-getting-started

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

53 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Scala Build Tool - Getting Started

This documentation provides the commands, the branches and the references for the course. The commands are further organized as needed in each video clip.

If you feel something is missing, please make a change, and open a pull request to contribute!

Happy Learning!

Course Artifacts

Open-Sourced Libraries on Bintray
Live API Deployed on Heroku

Git Branches for the Course

so that you can time-travel the changes

m4_0_initial_commit
m4_1_first_calculator
m5_1_currency_api
m5_2_networth_multi_currency
m6_1_packaging
m6_2_bintray_oss
m6_3_deploy_api

Module 02: Introduction to SBT

How to install JDK 8?

Follow the Link

Link

Verify the Java 8 is installed

java -version

How to install sbt?

Mac

Homebrew

Verify if Homebrew is installed
brew --version

Install sbt using Homebrew

brew install sbt
Verify if sbt is installed
sbt sbtVersion

Windows

Link

Linux

Ubuntu and other Debian-based distributions
Red Hat Enterprise Linux and other RPM-based distributions
Gentoo

How to install IntelliJ IDEA Community Edition?

Link

Module 03: SBT Fundamentals

02: Working with sbt shell

mkdir sbt101
cd sbt101

touch build.sbt
ll
sbt
scalaVersion
help
help compile
help clean
help test
cd sbt101
mkdir -p src/main/directory
// vi src/main/scala/HelloWorld.scala
object HelloWorld extends App {
 println("Hello sbt!")
}
compile
run
// vi src/main/scala/Echo.scala
object Echo extends App {
  println(s"You said: ${if (args.isEmpty) "nothing" else args.mkString(" ")}")
}
run A B C
clean
run
;clean ;run "aha! now I know"
!
clean
!!
help console
console
Echo.main(Array("I run from scala console as well"))
:q
exit
sbt clean
sbt clean compile
sbt "run A B C" 

03: Understanding the sbt directory structure

sourceDirectories
baseDirectory
scalaSource
target
source

04: Understanding Build Definition and Settings

// vi build.sbt
name := "sbt201"
name
help reload
reload
inspect name
reload
// vi build.sbt, change name
name := "sbt201"
reload
inspect package
inspect clean
inspect run
settings
settings -V
tasks
tasks -V

05: Creating your own settings

// vi build.sbt
lazy val emotion = settingKey[String]("How are you feeling")
emotion := "Fantastic!"
reload
help emotion
inspect emotion
emotion
// vi build.sbt
val randomInt = taskKey[Int]("Give me random number")
randomInt := scala.util.Random.nextInt
reload
help randomInt
randomInt
show randomInt

06: Understanding task graph in build definition

// vi build.sbt
val emotion = settingKey[String]("How are you feeling")
emotion := "Fantastic!"

val status = settingKey[String]("What is your current status?")
status := {
  val e = emotion.value
  s"Grateful and $e"
}
reload
status
// vi build.sbt
val emotion = settingKey[String]("How are you feeling")
emotion := "Fantastic!"


val randomInt = taskKey[Int]("Give me random number")
randomInt := {
  println(emotion.value)
  scala.util.Random.nextInt
}
reload
show randomInt
// vi build.sbt
val randomInt = taskKey[Int]("Give me random number")
randomInt := {
  scala.util.Random.nextInt
}

val status = settingKey[String]("What is your current status?")
status := {
  val r = randomInt.value
  s"Grateful and $r"
}
reload

Module 04: Project Lifecycle using SBT

03: Creating the project base

name

04: Creating the first calculator

;reload ;compile
projects
calculators/compile
calculators/clean
project calculators
;clean ;compile
projects
reload
projects
;clean ;compile
;reload
projects
;clean ;compile
;clean ;run
;clean ;calculators/run 100 20

05: Understanding Scopes and Executing Test

test
calculators/run
calculators/Test/run

06: Adding another Calculator

compile
calculators/runMain CompoundInterest 5000 5 10

References

Compound Interest Calculator

05: Refactoring project for bigger changes

02: Adding external libraries in sbt

unmanagedBase

References

requests-scala
scala-xml

03: Working with 3rd party libraries

api/console
val r = requests.get("https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml")
r.statusCode
r.headers
r.headers("content-type")
import scala.xml._  // (that's why we needed scala-xml)
r.text
val xmlResponse = XML.loadString(r.text)
val currencyCodes: Seq[String] = (xmlResponse \\ "@currency").map(node => node.text)
// Explain how to fetch attributes in xml in scala, refer to medium article
val euroToCurrencyMultipliers: Seq[Double] = (xmlResponse \\ "@rate").map(node => node.text.toDouble)
val currencyCodeMultipliers = (currencyCodes zip euroToCurrencyMultipliers).toMap

References

European Central Bank Currency API
Working with XML in Scala

04: Execute Tests using ScalaTest

test
Test/parallelExecution
~test

References

ScalaTest

06: Adding dependencies between sub-projects

calculators/runMain NetWorthMultiCurrency "100000 EUR,9000 USD" "59100 EUR,12200 USD"

06: Open-sourcing the project

02: Packaging the artifacts

;clean ;stage
ls calculators/target/scala-2.12/
ls api/target/scala-2.12/

calculators/target/universal/stage/bin/net-worth 100 20
calculators/target/universal/stage/bin/compound-interest 5000 5 10
api/target/universal/stage/bin/api
docker -v
docker:publishLocal
docker images
docker run -it calculators:0.1.0-SNAPSHOT 100 20

References

sbt-native-packager
Docker Desktop

03: Continuous Integration using Travis-CI

References

Travis-CI Scala

04: Publishing artifacts on Bintray

bintrayChangeCredentials
;clean ;publish
ls -ltr ~/.ivy2/cache/calculators/calculators_2.12/jars

References

Bintray: Open-source your project

05: Creating API for deployment

;reload ;api/runMain WebServer
curl -v http://localhost:8080/rates

References

akka-http

06: Deploying the API on Heroku

References

Heroku
Heroku CLI tools
Heroku Procfile

Releases

No releases published

Packages

No packages published

Languages

  • Scala 100.0%