The project construction tool is a very important part of project development, and making full use of it can greatly improve the efficiency of project development. In the process of learning SCALA, I came across SBT (Simple Build Tool), SBT是SCALA 平台上标准的项目构建工具
, 当然你要用它来构建其他语言的项目也是可以的
. SBT doesn't have as many concepts and rules as Maven, but it's more flexible than IVY, 可以认为是一个精简版的Maven
right?
1. Download
https://www.scala-sbt.org/download.html
sbt-1.1.4.msi
2. Installation
Go directly to the next step, install it, and then add it to the system's environment variables by default. If you don't have it, you can add it yourself
3. IDEA creates a new Sbt project
New as shown
Then we create a new test class
package com.lcc
object hellow {
def main(args: Array[String]) {
println("Hello World!")
}
}
run the test
J:\00-IDEA\work\hellow>J:\Sbt\install\bin\sbt run
。。。。。
。。。。。。
新目录 3 J:\Sbt\install\lib\local-preloaded\org.spire-math\jawn-parser_2.12\0.10.4\ivys\
100% 新文件 2769 ivy.xml
100% 新文件 32 ivy.xml.md5
100% 新文件 40 ivy.xml.sha1
新目录 3 J:\Sbt\install\lib\local-preloaded\org.spire-math\jawn-parser_2.12\0.10.4\jars\
100% 新文件 74239 jawn-parser_2.12.jar
100% 新文件 32 jawn-parser_2.12.jar.md5
100% 新文件 40 jawn-parser_2.12.jar.sha1
------------------------------------------------------------------------------
总数 复制 跳过 不匹配 失败 其他
目录: 355 355 0 0 0 0
文件: 495 495 0 0 0 0
字节: 48.70 m 48.70 m 0 0 0 0
时间: 0:02:16 0:00:25 0:00:00 0:01:50
速度: 2019345 字节/秒。
速度: 115.547 MB/分钟。
结束时间: 2018年4月17日 10:08:58
Java HotSpot(TM) 64-Bit Server VM warning: ignoring option MaxPermSize=256m; support was removed in 8.0
[info] Loading settings from plugins.sbt ...
[info] Loading project definition from J:\00-IDEA\work\hellow\project
[info] Loading settings from build.sbt ...
[info] Set current project to hellow (in build file:/J:/00-IDEA/work/hellow/)
[info] Updating ...
[info] downloading https://repo1.maven.org/maven2/org/scala-lang/scala-library/2.12.5/scala-library-2.12.5.jar ...
[info] downloading https://repo1.maven.org/maven2/org/scala-lang/scala-compiler/2.12.5/scala-compiler-2.12.5.jar ...
[info] downloading https://repo1.maven.org/maven2/org/scala-lang/scala-reflect/2.12.5/scala-reflect-2.12.5.jar ...
[info] [SUCCESSFUL ] org.scala-lang#scala-reflect;2.12.5!scala-reflect.jar (3224ms)
[info] [SUCCESSFUL ] org.scala-lang#scala-compiler;2.12.5!scala-compiler.jar (5435ms)
[info] [SUCCESSFUL ] org.scala-lang#scala-library;2.12.5!scala-library.jar (7481ms)
[info] Done updating.
[info] Compiling 1 Scala source to J:\00-IDEA\work\hellow\target\scala-2.12\classes ...
[info] Non-compiled module 'compiler-bridge_2.12' for Scala 2.12.5. Compiling...
[info] Compilation completed in 53.072s.
[info] Done compiling.
[info] Packaging J:\00-IDEA\work\hellow\target\scala-2.12\hellow_2.12-0.1.jar ...
[info] Done packaging.
[info] Running com.lcc.hellow
Hello World!
[success] Total time: 103 s, completed 2018-4-17 10:11:22
Finally successfully printedHello World!
4. Detailed description
J:\00-IDEA\work\hellow>J:\Sbt\install\bin\sbt
Java HotSpot(TM) 64-Bit Server VM warning: ignoring option MaxPermSize=256m; support was removed in 8.0
[info] Loading settings from plugins.sbt ...
[info] Loading project definition from J:\00-IDEA\work\hellow\project
[info] Loading settings from build.sbt ...
[info] Set current project to hellow (in build file:/J:/00-IDEA/work/hellow/)
[info] sbt server started at local:sbt-server-6b1e3137e27e680a0ddb
sbt:hellow> run
[info] Running com.lcc.hellow
Hello World!
[success] Total time: 6 s, completed 2018-4-17 10:29:02
sbt:hellow>
In this example, sbt works exactly by convention. sbt will automatically find the following:
Source files in src/main/scala or in src/main/java in the project root directory in
src/test/scala or in src/test/java in
src/main/resources or in src/test/resources
jar files
in datafile lib By default, sbt builds the project with the same version of Scala that it started with. You can run the project by executing sbt run or enter the Scala REPL through the sbt console. The sbt console has already set the classpath of the project for you, so you can try the actual Scala example based on the code of the project.
3.1 Build Definition
Most projects require some manual setup. The basic build settings are placed in the build.sbt file in the project root directory. For example, if your project is placed under hello, in hello/build.sbt you can write:
Note :
set sbt version
You can force a version of sbt by creating a hello/project/build.properties file. In this file, write the following to force the use of 0.13.16:
lazy val root = (project in file("."))
.settings(
name := "hellow",
version := "1.0",
scalaVersion := "2.12.2"
)
bt is 99% compatible across different releases. But setting the sbt version in the project/build.properties file still avoids some potential confusion.
run test
sbt:hellow> run
[info] Running com.lcc.hellow
Hello World!
[success] Total time: 7 s, completed 2018-4-17 10:33:05
sbt:hellow>
4. Common commands
Other common commands
Tables | Cool |
---|---|
clean | Delete all generated files (under target directory). |
compile | Compile the source files (in the src/main/scala and src/main/java directories). |
test | Compile and run all tests. |
console | Enter a Scala parser that contains all compiled files and all dependencies on the classpath. Type: quit, Ctrl+D (Unix), or Ctrl+Z (Windows) to return to sbt. |
run <parameter>* | Execute the main class of the project on the same virtual machine as sbt. |
package | Package the files under src/main/resources and the compiled class files in src/main/scala and src/main/java into a jar file. |
help <command> | Displays detailed help information for the specified command. If no command is specified, a brief description of all commands is displayed. |
reload | Reload build definitions (those defined in build.sbt, project/ .scala, project/ .sbt files). A reload is required after modifying the build definition file . |
Reference: https://www.scala-sbt.org/0.13/docs/en-us/Getting-Started.html