幫助
> 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
命令可以查詢?指定命令的幫助信息,也可以查詢某個(gè)配置/任務(wù)的描述信息。
命令補(bǔ)全
跟其他unix-like
的命令一樣,用tab
鍵補(bǔ)全命令,如果匹配多個(gè)命令,則會(huì)顯示命令列表,以供選擇。
查看版本信息
> 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
列出所有任務(wù)(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
?運(yùn)行
> 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
,所以會(huì)先執(zhí)行compile
,然后執(zhí)行main方法(如果有多個(gè),會(huì)?提示要求選擇一個(gè)執(zhí)行)。
運(yùn)行測試用例
> 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/)
查看工程配置
有三個(gè)方法,一是直接輸入配置名,顯示簡要信息
> name
[info] Hello
> version
[info] 0.1.0-SNAPSHOT
二是用show 配置名
,這跟第一種等價(jià)
> show scalaSource
[info] /Users/yangjia/sources/learning_scala/sbt/helloworld/src/main/scala
三是用inspect
,顯示詳細(xì)信息
> 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
...
如果不太明白某個(gè)配置項(xiàng)的含義,settings?
會(huì)打印所有配置項(xiàng)的含義。
Provided by
輸出的信息是鍵的作用域。sbt
有三個(gè)作用域軸:子項(xiàng)目軸、配置軸和任務(wù)軸。形如:
{}/config:intask::key
。
例如,version
的Provided by
信息里面,子項(xiàng)目軸為{file:/Users/yangjia/sources/learning_scala/sbt/helloworld/}root
,即為在/Users/yangjia/sources/learning_scala/sbt/helloworld
目錄下的root工程。配置軸為*
,表示全局配置。任務(wù)軸被忽略了,會(huì)被自動(dòng)解析(也是全局的)。
編譯并進(jìn)入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
自動(dòng)編譯
~compile
?:當(dāng)源代碼變化時(shí),自動(dòng)編譯。這其實(shí)是個(gè)組合命令~
+compile
。?也可以自動(dòng)執(zhí)行用例:~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)
查詢最后一次執(zhí)行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)
組合命令
如下所示,先執(zhí)行test
,如果執(zhí)行成功,則執(zhí)行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
執(zhí)行失敗,則不會(huì)執(zhí)行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
>
執(zhí)行scala表達(dá)式
> 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
包的方法,都是執(zhí)行外部命令(pwd
)。兩者的差異是,前者得到命令的退出碼,后者得到命令的輸出。
以樹狀結(jié)構(gòu)輸出依賴關(guān)系
> 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