在命令行中输入sbt
可以进入sbt shell,在此shell中可以执行其内置的命令,这里就对常用的命令、批处理模式及持续构建进行介绍。详细可参考sbt官网
常用命令
compile
Compiles sources ( in src/main/scala and src/main/java directories)
clean
Deletes files produced by the build, such as generated sources, compiled classes, and task caches (in the target directory).
run [argument]
Runs a main class, passing along arguments provided on the command line.
test
Compiles and Executes all tests.
package
Produces the main artifact, such as a binary jar. Created jar file containing the files in src/main/resoureces and classes compiled from src/main/scala and src/main/java.
console
Starts the Scala interpreter with the project classes(including the compiled sources and all dependencies) on the classpath. 若要返回sbt shell则可以输入“:quit”, “Ctrl + D(Unix)”或”Ctrl + Z(Windows)”.
reload
Reloads the build definintion(build.sbt, project/.scala, project/.sbt files), needed if you change the build definition. Which can loads the project in the current direcory.
reload plugins
loads the plugins project (under project directory).
reload return
loads the root project (and leaves the plugins project).
exit
Terminates the build. 也可以使用”Ctrl + D(Unix)”或”Ctrl + Z(Windows)”.
help [command]
Displays detailed help for the specified command. If no command is provided, displays brief description s of all commands.
help <regular expression>
Searches the help according to the provided regular expression.
!
History command help. Lists and describes all history commands.
批处理模式、持续构建和测试
批处理模式:将一些以空格分隔的sbt命令作为参数,如果其中的某些参数本身还具有参数则需要使用双引号将其包裹。如:
sbt clean compile “testOnly TestA TestB”
批处理模式的缺点:由于每次执行都需要重启JVM和JIT,故而运行速度较慢。推荐使用sbt shell或sbt提供的持续构建和测试特性。
持续构建和测试:当保存文件后自动重新编译或运行测试,这样就可以加速edit-compile-test周期。
如果想要在有文件改变后运行一个命令,可以在此命令(sbt shell or batch mode)前添加波浪线”~”,如
~testQuick
如果想要停止文件改变监测特性,可以在命令行中输入”Enter”。