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

你可能感兴趣的:(sbt常用命令汇总)