Sbt
lastmod 2020-09-11created 2019-01-149 edits
SBT is one of the modern build tools. It is written in Scala and offers many features that are convenient for Scala, but SBT itself is a general-purpose build tool.
Categories
- java
- sbt:Basic - sbt basics
- sbt:Example:StartupScript - A bash script for starting sbt.
- com.typesafe.config.ConfigFactory
Why use SBT?
- Dependency management
- Uses Ivy for dependency management
- Only-update-on-request model
Commands
-
sonatypeList: List staging repositories -
sonatypeOpen: Create a staging repository and set publishTo -
sonatypeClose: Close a stage and clear publishTo if it was set by sonatypeOpen -
sonatypePromote: Promote a staged repository -
sonatypeDrop: Drop a staging repository -
sonatypeRelease: Publish with sonatypeClose and sonatypePromote -
sonatypeReleaseAll: Publish all staging repositories to Maven central -
sonatypeLog: Show repository activities -
coverage: ';set coverageEnabled in ThisBuild := true' -
coverageOn: ';set coverageEnabled in ThisBuild := true' -
coverageOff: ';set coverageEnabled in ThisBuild := false' -
publishC: ';reload;clean;assembly;publish' -
about: Displays basic information about sbt and the build. -
tasks: Lists the tasks defined for the current project. -
settings: Lists the settings defined for the current project. -
reload: (Re)loads the current project or changes to plugins project or returns from it. -
new: Creates a new sbt build. -
projects: Lists the names of available projects or temporarily adds/removes extra builds to the session. -
project: Displays the current project or changes to the providedproject. -
set [every]<setting>: Evaluates a Setting and applies it to the current project. -
session: Manipulates session settings.</code>: For details, run 'help session'. -
inspect [tree|uses|definitions|actual]<key>: Prints the value for 'key', the defining scope, delegates, related definitions, and dependencies. - <log-level>: Sets the logging level to 'log-level'.</code>: Valid levels: debug, info, warn, error
-
plugins: Lists currently available plugins. -
--addPluginSbtFile=<file>: Adds the given *.sbt file to the plugin build. -
last: Displays output from a previous command or the output from a specific task. -
last-grep: Shows lines from the last output for 'key' that match 'pattern'. -
lastGrep: Shows lines from the last output for 'key' that match 'pattern'. -
export<tasks>+: Executes tasks and displays the equivalent command lines. -
show<key>: Displays the result of evaluating the setting or task associated with 'key'. - <code>all<task>
+</code>: Executes all of the specified tasks concurrently.
-
help: Displays this help message or prints detailed help on requested commands (run 'help <command>'). -
completions: Displays a list of completions for the given argument string (run 'completions <string>'). -
;<command>(;<command>)*: Runs the provided semicolon-separated commands. -
early(<command>): Schedules a command to run before other commands on startup. -
exit: Terminates the build. -
~<command>: Executes the specified command whenever source files change.
More command help available using 'help<command>' for: !, +, ++, +-, <, ^, ^^, alias, append, apply, client, eval, iflast, java+, java++, onFailure, reboot, shell, startServer
Tasks
This is a list of tasks defined for the current project. It does not list the scopes the tasks are defined in; use the 'inspect' command for that. Tasks produce values. Use the 'show' command to run the task and print the resulting value.
-
bgRun: Start an application's default main class as a background job -
bgRunMain: Start a provided main class as a background job -
checkPgpSignatures: Checks the signatures of artifacts to see if they are trusted. -
clean: Deletes files produced by the build, such as generated sources, compiled classes, and task caches. -
compile: Compiles sources. -
console: Starts the Scala interpreter with the project classes on the classpath. -
consoleProject: Starts the Scala interpreter with the sbt and the build definition on the classpath and useful imports. -
consoleQuick: Starts the Scala interpreter with the project dependencies on the classpath. -
copyResources: Copies resources to the output directory. -
doc: Generates API documentation. -
fgRun: Start an application's default main class as a foreground job -
fgRunMain: Start a provided main class as a foreground job -
packInstall: pack and install -
package: Produces the main artifact, such as a binary jar.</code>: This is typically an alias for the task that actually does the packaging. -
packageBin: Produces a main artifact, such as a binary jar. -
packageDoc: Produces a documentation artifact, such as a jar containing API documentation. -
packageSrc: Produces a source artifact, such as a jar containing sources and resources. -
publish: Publishes artifacts to a repository. -
publishDoc: Publish doc -
publishLocal: Publishes artifacts to the local Ivy repository. -
publishLocalSigned: Publishing all artifacts to a local repository, but SIGNED using PGP. -
publishM2: Publishes artifacts to the local Maven repository. -
publishSigned: Publishing all artifacts, but SIGNED using PGP. -
publishTM: Publish jar -
run: Runs a main class, passing along arguments provided on the command line. -
runMain: Runs the main class selected by the first argument, passing the remaining arguments to the main method. -
test: Executes all tests. -
testOnly: Executes the tests provided as arguments or all tests if no arguments are provided. -
testQuick: Executes the tests that either failed before, were not run or whose transitive dependencies changed, among those provided as arguments. -
update: Resolves and optionally retrieves dependencies, producing a report.
More tasks may be viewed by increasing verbosity. See 'help tasks'
Getting started
Write a shell script that invokes the downloaded jar, as in the example below.
Make sure the script you just created is on your execution path. Run sbt to start the project.
If you don't have a project, create a new one as follows.
[local ~/projects]$ sbt
Project does not exist, create new project? (y/N/s) y
Name: sample
Organization: com.twitter
Version [1.0]: 1.0-SNAPSHOT
Scala version [2.7.7]: 2.8.1
sbt version [0.7.4]:
Getting Scala 2.7.7 ...
:: retrieving :: org.scala-tools.sbt#boot-scala
confs: [default]
2 artifacts copied, 0 already retrieved (9911kB/221ms)
Getting org.scala-tools.sbt sbt_2.7.7 0.7.4 ...
:: retrieving :: org.scala-tools.sbt#boot-app
confs: [default]
15 artifacts copied, 0 already retrieved (4096kB/167ms)
[success] Successfully initialized directory structure.
Getting Scala 2.8.1 ...
:: retrieving :: org.scala-tools.sbt#boot-scala
confs: [default]
2 artifacts copied, 0 already retrieved (15118kB/386ms)
[info] Building project sample 1.0-SNAPSHOT against Scala 2.8.1
[info] using sbt.DefaultProject with sbt 0.7.4 and Scala 2.7.7
Always start a project with a SNAPSHOT version.
Project structure
- project – project definition files
- project/build/.scala – main project definition file
- project/build.properties – defines the project, sbt, and Scala versions
sbt shell command
-
compile: Compiles. -
~compile: Automatically compiles whenever the code changes. (Probably good to leave it running while coding) -
run: Runs.
See also
Favorite site
Documentation
Tutorials
References
-
Scala_School_-Build_Tool-_SBT.pdf ↩