sbt常用命令

命令补全

跟其他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

Mac下,更新的库保存在~/.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

二是用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�会打印所有配置项的含义。

编译并进入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 clean
> 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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,716评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,558评论 1 294
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,431评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,127评论 0 209
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,511评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,692评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,915评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,664评论 0 202
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,412评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,616评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,105评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,424评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,098评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,096评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,869评论 0 197
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,748评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,641评论 2 271

推荐阅读更多精彩内容

  • 帮助 help命令可以查询�指定命令的帮助信息,也可以查询某个配置/任务的描述信息。 命令补全 跟其他unix-l...
    typesafe阅读 1,760评论 0 1
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 46,367评论 6 343
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,100评论 18 139
  • Ubuntu的发音 Ubuntu,源于非洲祖鲁人和科萨人的语言,发作 oo-boon-too 的音。了解发音是有意...
    萤火虫de梦阅读 98,523评论 9 468
  • linux资料总章2.1 1.0写的不好抱歉 但是2.0已经改了很多 但是错误还是无法避免 以后资料会慢慢更新 大...
    数据革命阅读 12,019评论 2 34