Simple Build Tool / Scala Build Tool
- Key Points
- Build File -
build.sbt
- Usage
- Create jar
- Create self-contained 'uber jar' with all dependencies included using Assembly plugin
- Interactive Console - REPL
- Watch
src/
+ Auto-Trigger - Use the Ivy cache
- Compile the main sources in
src/main/scala
andsrc/main/java
- Compile and run all tests
- Install jar to local Ivy repository
- Install jar to local Maven repository
- Push jar to remote repo (if configured)
- Pull down dependencies to
lib_managed/
- Add task
- Eclipse
- IntelliJ
- Build.sbt
-
incremental compilation
-
interactive shell
-
uses StackOverflow.com/tags/sbt for Q&A
-
tasks are Scala functions (see further below to define your own)
-
slow to start, optimizes as much as it can at start as Scala can be slow to compile so it minimizes recompilation
-
first run will prompt to create a project, ask for name, organization, version, scala version etc to initialize project
The build configuration file of what dependencies to include.
HariSekhon/Nagios-Plugin-Kafka - build.sbt
HariSekhon/lib-java - build.sbt
HariSekhon/Templates - build.sbt
wget -nc https://raw.githubusercontent.com/HariSekhon/Templates/master/build.sbt
sbt help
sbt tasks
sbt clean
sbt package
Useful for both CLIs and distributed computing programs running on big data clusters.
project/assembly.sbt
:
// for SBT 0.13.6+
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.13.0")
// for SBT < 0.13.6
//addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.11.2")
sbt assembly
Launches Scala repl with all deps loaded, compiles all src/
.
REPL = Read–Eval–Print Loop
sbt console
Use ~
to watch the src/
dir and run the given <command>
every time source code changes - fast, nice.
In REPL console:
~ <command>
Automatically run unit-tests every time src/
code changes:
~ test
Only runs unit tests for the bits of code that changed:
~ testQuick
sbt "run <args>"
sbt -D sbt.ivy.home=sbt/ivy package
Jar deps stored in:
~/.ivy2/cache/org.apache.spark/spark-core_2.10/jars/spark-core_2.10-1.3.1.jar
sbt compile
sbt test
sbt publishLocal
sbt publishLocal
sbt publish
sbt update
lazy val print = task {
log.info("testing")
None
}
sbteclipse
plugin to download deps and build project's top level .classpath file for Eclipse IDE.
~/.sbt/plugins/plugins.sbt:
~/.sbt/0.13/plugins/plugins.sbt:
addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "3.0.0")
Downloads dependencies to ~/.ivy2/cache
and writes project's top level .classpath file for Eclipse pointing to ~/.ivy/cache
.
Then go to Eclipse -> Right click project -> Refresh
to pick up the new .classpath
:
sbt eclipse
Not sure this is actually needed in new IntelliJ versions.
~/.sbt/0.13/plugins/plugins.sbt
addSbtPlugin("com.github.mpeltonen" % "sbt-idea" % "1.6.0")
Run after each build.sbt
change:
sbt gen-idea
name := "MyApp"
version := "0.1"
scalaVersion := "2.10.4"
libraryDependencies ++= Seq(
// %% appends scala version to spark-core
"org.apache.spark" %% "spark-core" % "1.3.1"
"org.apache.hadoop" % "hadoop-client" % "2.6.0"
"org.elasticsearch" % "elasticsearch" % "1.4.1"
)
On Windows, set options like this:
setx SBT_OPTS "-Dsbt.global.base=C:/Users/hari/.sbt/0.13/ -Dsbt.ivy.home=C:/Quarantine/ivy-cache/ -Dsbt.boot.directory=C:/Quarantine/sbt/boot/ -Dsbt.override.build.repos=true -Dsbt.repository.config=C:/Users/hari/.sbt/repositories"
setx SBT_CREDENTIALS "C:/Users/hari/.sbt/.credentials"
realm=Sonatype Nexus Repository Manager
host=myFQDN
user=myUser
password=myPass
credentials += Credentials(Path.userHome / ".sbt" / ".credentials")
[repositories]
local
<name>-sbt: http://host:8081/nexus/content/groups/sbt/, [organization]/[module]/(scala_[scalaVersion]/)(sbt_[sbtVersion]/[type]s/[artifact])(-[classifier]).[ext]
<name>: http://host:8081/nexus/content/groups/public/
Ported from private Knowledge Base page 2014+