Skip to content

Instantly share code, notes, and snippets.

@HariSekhon
Created March 22, 2024 01:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HariSekhon/85de517a5b52b24464f139d9b9ec98ae to your computer and use it in GitHub Desktop.
Save HariSekhon/85de517a5b52b24464f139d9b9ec98ae to your computer and use it in GitHub Desktop.
sbt.md from HariSekhon/Knowledge-Base repo: https://github.com/HariSekhon/Knowlege-Base

SBT

Simple Build Tool / Scala Build Tool

https://www.scala-sbt.org/

  • 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

Build File - build.sbt

The build configuration file of what dependencies to include.

Build Configs

HariSekhon/Nagios-Plugin-Kafka - build.sbt

HariSekhon/lib-java - build.sbt

HariSekhon/Templates - build.sbt

Usage

sbt help
sbt tasks
sbt clean

Create jar

sbt package

Create self-contained 'uber jar' with all dependencies included using Assembly plugin

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

Interactive Console - REPL

Launches Scala repl with all deps loaded, compiles all src/.

REPL = Read–Eval–Print Loop

sbt console

Watch src/ + Auto-Trigger

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

Use the Ivy cache

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

Compile the main sources in src/main/scala and src/main/java

sbt compile

Compile and run all tests

sbt test

Install jar to local Ivy repository

sbt publishLocal

Install jar to local Maven repository

sbt publishLocal

Push jar to remote repo (if configured)

sbt publish

Pull down dependencies to lib_managed/

sbt update

Add task

lazy val print = task {
  log.info("testing")
  None
}

Eclipse

sbteclipse plugin to download deps and build project's top level .classpath file for Eclipse IDE.

SBT <= 0.12

~/.sbt/plugins/plugins.sbt:

SBT 0.13+

~/.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

IntelliJ

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

Build.sbt

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

~/.sbt/.credentials

realm=Sonatype Nexus Repository Manager
host=myFQDN
user=myUser
password=myPass

~/.sbt/0.13/plugins/credentials.sbt

credentials += Credentials(Path.userHome / ".sbt" / ".credentials")

~/.sbt/repositories

[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+
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment