命令补全
跟其他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