sbt常用命令汇总

帮助

> help
help Displays this help message or prints detailed help on requested commands (run 'help ').
completions Displays a list of completions for the given argument string (run 'completions ').
about Displays basic information about sbt and the build.
...
> help last
last
Prints the logging for the previous command, typically at a more verbose level.
last 
Prints the logging associated with the provided key. The key typically refers to a task (for example, test:compile). The logging that is displayed is restricted to the logging for that particular task.
See also 'last-grep'.
> help name
Project name.

help命令可以查询�指定命令的帮助信息,也可以查询某个配置/任务的描述信息。

命令补全

跟其他unix-like的命令一样,用tab键补全命令,如果匹配多个命令,则会显示命令列表,以供选择。

查看版本信息

> about
[info] This is sbt 0.13.15
[info] The current project is {file:/Users/yangjia/sources/learning_scala/sbt/helloworld/}root 0.1.0-SNAPSHOT
[info] The current project is built against Scala 2.12.1
[info] Available Plugins: sbt.plugins.IvyPlugin, sbt.plugins.JvmPlugin, sbt.plugins.CorePlugin, sbt.plugins.JUnitXmlReportPlugin, sbt.plugins.Giter8TemplatePlugin
[info] sbt, sbt plugins, and build definitions are using Scala 2.10.6

列出所有任务(tasks)

> 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.
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.
package Produces the main artifact, such as a binary jar. 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.
publishLocal Publishes artifacts to the local Ivy repository.
publishM2 Publishes artifacts to the local Maven repository.
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.

�删除target目录下�编译生成的文件

> clean
[success] Total time: 0 s, completed 2017-7-15 19:45:58

编译

注意:编译是增量编译

> compile
[info] Updating {file:/Users/yangjia/sources/learning_scala/sbt/helloworld/}root...
[info] Resolving jline#jline;2.14.1 ...
[info] Done updating.
[info] Compiling 1 Scala source to /Users/yangjia/sources/learning_scala/sbt/helloworld/target/scala-2.12/classes...
[success] Total time: 1 s, completed 2017-7-15 19:46:04

�运行

> run
[info] Updating {file:/Users/yangjia/sources/learning_scala/sbt/helloworld/}root...
[info] Resolving jline#jline;2.14.1 ...
[info] Done updating.
[info] Compiling 1 Scala source to /Users/yangjia/sources/learning_scala/sbt/helloworld/target/scala-2.12/classes...
[info] Running example.Hello 
hello
[success] Total time: 2 s, completed 2017-7-15 19:49:28

run依赖于compile,所以会先执行compile,然后执行main方法(如果有多个,会�提示要求选择一个执行)。

运行测试用例

> test
[info] Compiling 1 Scala source to /Users/yangjia/sources/learning_scala/sbt/helloworld/target/scala-2.12/test-classes...
[info] HelloSpec:
[info] The Hello object
[info] - should say hello
[info] Run completed in 589 milliseconds.
[info] Total number of tests run: 1
[info] Suites: completed 1, aborted 0
[info] Tests: succeeded 1, failed 0, canceled 0, ignored 0, pending 0
[info] All tests passed.
[success] Total time: 4 s, completed 2017-7-15 19:52:15

更新外部依赖

> update
[info] Updating {file:/Users/yangjia/sources/learning_scala/sbt/helloworld/}root...
[info] Resolving jline#jline;2.14.1 ...
[info] Done updating.
[success] Total time: 0 s, completed 2017-7-15 19:54:20

�更新的库保存在~/.ivy2/cache目录下。

更新工程配置

> reload
[info] Loading project definition from /Users/yangjia/sources/learning_scala/sbt/helloworld/project
[info] Set current project to Hello (in build file:/Users/yangjia/sources/learning_scala/sbt/helloworld/)

查看工程配置

有三个方法,一是直接输入配置名,显示简要信息

> name
[info] Hello
> version
[info] 0.1.0-SNAPSHOT

二是用show 配置名,这跟第一种等价

> show scalaSource
[info] /Users/yangjia/sources/learning_scala/sbt/helloworld/src/main/scala

三是用inspect,显示详细信息

> inspect version
[info] Setting: java.lang.String = 0.1.0-SNAPSHOT
[info] Description:
[info] The version/revision of the current module.
[info] Provided by:
[info] {file:/Users/yangjia/sources/learning_scala/sbt/helloworld/}/*:version
[info] Defined at:
[info] /Users/yangjia/sources/learning_scala/sbt/helloworld/build.sbt:8
...
> inspect libraryDependencies
[info] Setting: scala.collection.Seq[sbt.ModuleID] = List(org.scala-lang:scala-library:2.12.1, org.scalatest:scalatest:3.0.1:test)
[info] Description:
[info] Declares managed dependencies.
[info] Provided by:
[info] {file:/Users/yangjia/sources/learning_scala/sbt/helloworld/}root/*:libraryDependencies
[info] Defined at:
[info] (sbt.Classpaths) Defaults.scala:1300
[info] /Users/yangjia/sources/learning_scala/sbt/helloworld/build.sbt:11
...

如果不太明白某个配置项的含义,settings�会打印所有配置项的含义。
Provided by输出的信息是键的作用域。sbt有三个作用域轴:子项目轴、配置轴和任务轴。形如:
{}/config:intask::key
例如,versionProvided by信息里面,子项目轴为{file:/Users/yangjia/sources/learning_scala/sbt/helloworld/}root,即为在/Users/yangjia/sources/learning_scala/sbt/helloworld目录下的root工程。配置轴为*,表示全局配置。任务轴被忽略了,会被自动解析(也是全局的)。

编译并进入REPL

> console
[info] Compiling 1 Scala source to /Users/yangjia/sources/learning_scala/sbt/helloworld/target/scala-2.12/classes...
[info] Starting scala interpreter...
[info] 
Welcome to Scala 2.12.1 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_101).
Type in expressions for evaluation. Or try :help.
scala> :type example.Test
example.Test.type
scala> :quit
[success] Total time: 99 s, completed 2017-7-15 19:43:13

自动编译

~compile�:当源代码变化时,自动编译。这其实是个组合命令~+compile。�也可以自动执行用例:~test

> ~compile
[success] Total time: 0 s, completed 2017-7-15 20:01:04
1. Waiting for source changes... (press enter to interrupt)
[info] Compiling 1 Scala source to /Users/yangjia/sources/learning_scala/sbt/helloworld/target/scala-2.12/classes...
[success] Total time: 1 s, completed 2017-7-15 20:01:27
2. Waiting for source changes... (press enter to interrupt)

查询最后一次执行XX命令的信息

> last run
[info] Running example.Hello 
[debug] Waiting for threads to exit or System.exit to be called.
[debug] Classpath:
[debug] /Users/yangjia/sources/learning_scala/sbt/helloworld/target/scala-2.12/classes
[debug] /Users/yangjia/.ivy2/cache/org.scala-lang/scala-library/jars/scala-library-2.12.1.jar
[debug] Waiting for thread run-main-3 to terminate.
[debug] Thread run-main-3 exited.
[debug] Interrupting remaining threads (should be all daemons).
[debug] Sandboxed run complete..
[debug] Exited with code 0
> last test
[debug] Running TaskDef(example.HelloSpec, org.scalatest.tools.Framework$$anon$1@4382b430, false, [SuiteSelector])
[info] Run completed in 208 milliseconds.
[info] Total number of tests run: 1
[info] Suites: completed 1, aborted 0
[info] Tests: succeeded 1, failed 0, canceled 0, ignored 0, pending 0
[info] All tests passed.
[debug] Passed tests:
[debug] example.HelloSpec

退出shell

> exit
$ 

退出还有快捷键: ctrl+d(Mac), ctrl+c(Mac/Linux), ctrl+z(Windows)

组合命令

如下所示,先执行test,如果执行成功,则执行exit

> ; test ; exit
[info] Updating {file:/Users/yangjia/sources/learning_scala/sbt/helloworld/}root...
[info] Resolving jline#jline;2.14.1 ...
[info] Done updating.
[info] Compiling 1 Scala source to /Users/yangjia/sources/learning_scala/sbt/helloworld/target/scala-2.12/classes...
[info] Compiling 1 Scala source to /Users/yangjia/sources/learning_scala/sbt/helloworld/target/scala-2.12/test-classes...
[info] HelloSpec:
[info] The Hello object
[info] - should say hello
[info] Run completed in 441 milliseconds.
[info] Total number of tests run: 1
[info] Suites: completed 1, aborted 0
[info] Tests: succeeded 1, failed 0, canceled 0, ignored 0, pending 0
[info] All tests passed.
[success] Total time: 8 s, completed 2017-7-15 22:22:45
banxia:helloworld yangjia$ 

如果test执行失败,则不会执行exit

> ; test ; exit
[info] Compiling 1 Scala source to /Users/yangjia/sources/learning_scala/sbt/helloworld/target/scala-2.12/test-classes...
[info] HelloSpec:
[info] The Hello object
[info] - should say hello *** FAILED ***
[info] "hello[]" did not equal "hello[z]" (HelloSpec.scala:7)
[info] Run completed in 447 milliseconds.
[info] Total number of tests run: 1
[info] Suites: completed 1, aborted 0
[info] Tests: succeeded 0, failed 1, canceled 0, ignored 0, pending 0
[info] *** 1 TEST FAILED ***
[error] Failed tests:
[error] example.HelloSpec
[error] (test:test) sbt.TestsFailedException: Tests unsuccessful
[error] Total time: 8 s, completed 2017-7-15 22:24:48
>

执行scala表达式

> eval println("foo")
foo
[info] ans: Unit = null
> eval "pwd" !
/Users/yangjia/sources/learning_scala/sbt/helloworld
[info] ans: Int = 0
> eval "pwd" !!
[info] ans: String = /Users/yangjia/sources/learning_scala/sbt/helloworld

!!!scala.sys.process包的方法,都是执行外部命令(pwd)。两者的差异是,前者得到命令的退出码,后者得到命令的输出。

以树状结构输出依赖关系

> inspect tree compile:sources
[info] compile:sources = Task[scala.collection.Seq[java.io.File]]
[info] +-compile:managedSources = Task[scala.collection.Seq[java.io.File]]
[info] | +-compile:sourceGenerators = List()
[info] | 
[info] +-compile:unmanagedSources = Task[scala.collection.Seq[java.io.File]]
[info] +-*:baseDirectory = /Users/yangjia/sources/learning_scala/sbt/helloworld
[info] +-*/*:sourcesInBase = true
[info] +-compile:unmanagedSourceDirectories = List(/Users/yangjia/sources/learning_scala/sbt/helloworld/src/main/scala-2.12, /Users/yangjia/sources/learning_scala/sbt/hellowo..
[info] | +-*/*:crossPaths = true
[info] | +-compile:javaSource = src/main/java
[info] | | +-compile:sourceDirectory = src/main
[info] | | +-*:sourceDirectory = src
[info] | | | +-*:baseDirectory = /Users/yangjia/sources/learning_scala/sbt/helloworld
[info] | | | +-*:thisProject = Project(id root, base: /Users/yangjia/sources/learning_scala/sbt/helloworld, configurations: List(compile, runtime, test, provided, optiona..
[info] | | | 
[info] | | +-compile:configuration = compile
[info] | | 
[info] | +-{.}/*:scalaBinaryVersion = 2.12
[info] | +-compile:scalaSource = src/main/scala
[info] | +-compile:sourceDirectory = src/main
[info] | +-*:sourceDirectory = src
[info] | | +-*:baseDirectory = /Users/yangjia/sources/learning_scala/sbt/helloworld
[info] | | +-*:thisProject = Project(id root, base: /Users/yangjia/sources/learning_scala/sbt/helloworld, configurations: List(compile, runtime, test, provided, optiona..
[info] | | 
[info] | +-compile:configuration = compile
[info] | 
[info] +-*/*:excludeFilter = sbt.HiddenFileFilter$@7994a0d1
[info] +-*/*:unmanagedSources::includeFilter = sbt.SimpleFilter@7a66c35a
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 206,378评论 6 481
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 88,356评论 2 382
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 152,702评论 0 342
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 55,259评论 1 279
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 64,263评论 5 371
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,036评论 1 285
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,349评论 3 400
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,979评论 0 259
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 43,469评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,938评论 2 323
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,059评论 1 333
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,703评论 4 323
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,257评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,262评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,485评论 1 262
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,501评论 2 354
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,792评论 2 345

推荐阅读更多精彩内容