安装包地址:https://pan.baidu.com/s/1x42iv4MGJBhGhfJ2gt2eVg 提取码:ihyr
不建议用sdkman安装,官网建议用,但考虑到天国的网络状态,sdk经常不在线,建议还是本地安装吧!
[root@master sbt]# curl https://bintray.com/sbt/rpm/rpm | tee /etc/yum.repos.d/bintray-sbt-rpm.repo
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 160 0 160 0 0 74 0 --:--:-- 0:00:02 --:--:-- 74
#bintray--sbt-rpm - packages by from Bintray
[bintray--sbt-rpm]
name=bintray--sbt-rpm
baseurl=https://sbt.bintray.com/rpm
gpgcheck=0
repo_gpgcheck=0
enabled=1
[root@master sbt]# yum install sbt -y
[root@master sbt]# rpm -ql sbt
/usr
/usr/share
/usr/share/doc
/usr/share/doc/sbt
/usr/share/doc/sbt/copyright
/usr/share/man
/usr/share/man/man1
/usr/share/man/man1/sbt.1.gz
/usr/share/sbt/LICENSE
/usr/share/sbt/NOTICE
/usr/share/sbt/bin
/usr/share/sbt/bin/java9-rt-export.jar
/usr/share/sbt/bin/sbt
/usr/share/sbt/bin/sbt-launch.jar
/usr/share/sbt/conf
/usr/share/sbt/conf/sbtconfig.txt
/usr/share/sbt/conf/sbtopts
[root@master sbt]# pwd
/usr/share/sbt/
[root@master sbt]# ls
bin conf lib LICENSE NOTICE repository
[root@master sbt]# vim repository
[repositories]
local
maven-local: file://~/.m2/repository
osc: http://maven.oschina.net/content/groups/public/
typesafe: http://repo.typesafe.com/typesafe/ivy-releases/, [organization]/[module]/(scala_[scalaVersion]/)(sbt_[sbtVersion]/)[revision]/[type]s/[artifact](-[classifier]).[ext], bootOnly
sonatype-oss-releases
maven-central
sonatype-oss-snapshots
或者
[repositories]
local
aliyun-nexus: http://maven.aliyun.com/nexus/content/groups/public/
ibiblio-maven: http://maven.ibiblio.org/maven2/
typesafe-ivy: https://dl.bintray.com/typesafe/ivy-releases/, [organization]/[module]/(scala_[scalaVersion]/)(sbt_[sbtVersion]/)[revision]/[type]s/[artifact](-[classifier]).[ext]
uk-repository: http://uk.maven.org/maven2/
jboss-repository: http://repository.jboss.org/nexus/content/groups/public/
typesafe: http://repo.typesafe.com/typesafe/ivy-releases/, [organization]/[module]/(scala_[scalaVersion]/)(sbt_[sbtVersion]/)[revision]/[type]s/[artifact](-[classifier]).[ext], bootOnly
sonatype-oss-releases
maven-central
sonatype-oss-snapshots
又或者
[repositories]
local
comp-maven: http://repo.data.1verge.net/nexus/content/groups/public/
store_cn: http://maven.oschina.net/content/groups/public/
store_mir: http://mirrors.ibiblio.org/maven2/
store_0: http://maven.net.cn/content/groups/public/
store_1: http://repo.typesafe.com/typesafe/ivy-releases/
store_2: http://repo2.maven.org/maven2/
又或者
[repositories]
local
huaweicloud-maven: https://repo.huaweicloud.com/repository/maven/
maven-central: https://repo1.maven.org/maven2/
sbt-plugin-repo: https://repo.scala-sbt.org/scalasbt/sbt-plugin-releases, [organization]/[module]/(scala_[scalaVersion]/)(sbt_[sbtVersion]/)[revision]/[type]s/[artifact](-[classifier]).[ext]
[root@master ~]# echo export SBT_HOME=/share/sbt/
[root@master ~]# echo export PATH=\$SBT_HOME:\$PATH
修改 $SBT_HOME/bin/sbt
SBT_OPTS可以添加选项和配置参数,也可在命令行在写入
[root@master ~]# vim $SBT_HOME/bin/sbt
SBT_OPTS="-Xms512M -Xmx1536M -Xss1M -XX:+CMSClassUnloadingEnabled -XX:MaxPermSize=256M"
java $SBT_OPTS -jar `dirname $0`/sbt-launch.jar "$@"
[root@master bin]# sbt sbtVersion
Java HotSpot(TM) 64-Bit Server VM warning: ignoring option MaxPermSize=256M; support was removed in 8.0
[warn] No sbt.version set in project/build.properties, base directory: /share/sbt/bin
[info] Set current project to bin (in build file:/share/sbt/bin/)
[info] 1.3.8
[root@master ~]# vim $SBT_HOME/conf/sbtopts
#sbt工作文件夹.
-sbt-dir
-Dsbt.global.base=~/.sbt
#仓库位置
-ivy
-Dsbt.ivy.home=~/.ivy2
#boot位置
-sbt-boot
-Dsbt.boot.directory=~/.sbt/boot
#加速器位置
-Dsbt.repository.config=~/.sbt/repositories
-Dsbt.repository.secure=false
-Dsbt.log.format=true
#强制覆盖默认的仓库
-Dsbt.override.build.repos=true
#设置网络代理
-Dhttp.proxyHost=proxy.zte.com.cn
-Dhttp.proxyPort=80
#设置编码
-Dfile.encoding=UTF8
插件路径和文件名都需要创建。
在eclipse中使用sbt相应的配置文件,需要安装sbteclipse插件,sbteclipse插件会依据sbt的配置生成eclipse项目需要的文件。
[root@master ~]# vim ~/.sbt/1.0/plugins/plugins.sbt
addSbtPlugin(“com.typesafe.sbteclipse” % “sbteclipse-plugin” % “5.2.4”)
ProjectName (base directory)
build.sbt
.gitignore (用于忽略文件或目录)
project/
xx.scala (defines helper objects and one-off plugins,辅助对象和一次性插件)
xx.sbt (并不等价于基本目录下的sbt文件)
src/
main/
resources/ (files to include in main jar here)
scala/ (main scala sources)
java/ (main java sources)
test/
resources/ (files to include in test jar here)
scala/ (test scala sources)
java/ (test java sources)
target/ (generated files, like compiled classes, packaged jars, managed files, caches, and documentation)
编译并运行所有的测试用例
[root@master ~]# test
启动一个 Scala 语言交互模式, sbt 在启动的时候会指定依赖的所有classpath, 返回 sbt 可以用 :quit 、 Ctrl+D(Unix) 和 Ctrl+Z(Windows)
[root@master ~]# console
运行项目在虚拟机中
[root@master ~]# run *
创建一个 jar 包其中包含 src/main/resources 和编译 src/main/scala 或 src/main/java 目录的 class 文件
[root@master ~]# package
重新加载配置文件(build.sbt, project/.scala 和 project/.sbt 文件)
[root@master ~]# reload
quit console
[root@master ~] scala> :q // to quit
new project
[root@master ~]# mkdir foo-build
[root@master ~]# cd foo-build
[root@master ~]#t ouch build.sbt
[root@master ~]# sbt
[root@master ~]# sbt:foo-build> exit
compile project
sbt:foo-build> compile
recompile project
sbt:foo-build> ~compile
new scala object and run scala programe
[root@master ~]# mkdir foo-build/src/main/scala/exampl
[root@master ~]# vim -/Hello.scala
package example
object Hello extends App {
println("Hello")
}
[root@master ~]# sbt:foo-build> run
Set ThisBuild / scalaVersion from sbt shell
[root@master ~]# sbt:foo-build> set ThisBuild / scalaVersion := "2.12.7"
show the scalaVersion
[root@master ~]# sbt:foo-build> scalaVersion
显示项目配置 show,能show+tab能show出来很多
#查看当前项目的名字
[root@master ~]# show name
#查看当前项目依赖的库
[root@master ~]# show libraryDependencies
Save the session to build.sbt
[root@master ~]# sbt:foo-build> session save
[root@master ~]# sbt:scala1> session
clear clear-all list list-all remove save save-all
Named project
[root@master ~]# vim build.sbt
ThisBuild / scalaVersion := "2.12.7"
ThisBuild / organization := "com.example"
lazy val hello = (project in file("."))
.settings(
name := "Hello"
)
#after named,Reload the build
[root@master ~]# sbt:foo-build> reload
Add ScalaTest to libraryDependencies
[root@master ~]# vim build.sbt
ThisBuild / scalaVersion := "2.12.7"
ThisBuild / organization := "com.example"
lazy val hello = (project in file("."))
.settings(
name := "Hello",
libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.5" % Test,
)
[root@master ~]# sbt:Hello> reload
[root@master ~]# sbt:Hello> test
[root@master ~]# sbt:Hello> ~testQuick
Write a test
[root@master ~]# vim src/test/scala/HelloSpec.scala
import org.scalatest._
class HelloSpec extends FunSuite with DiagrammedAssertions {
test("Hello should start with H") {
assert("hello".startsWith("H"))
}
}
[root@master ~]# ~testQuick
#Confirm that the test passes, then press Enter to exit the continuous test.
Add a library dependency
[root@master ~]# vim build.sbt
ThisBuild / scalaVersion := "2.12.7"
ThisBuild / organization := "com.example"
lazy val hello = (project in file("."))
.settings(
name := "Hello",
libraryDependencies += "com.typesafe.play" %% "play-json" % "2.6.9",
libraryDependencies += "com.eed3si9n" %% "gigahorse-okhttp" % "0.3.1",
libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.5" % Test,
)
Use Scala REPL(Read-Eval-Print Loop)
[root@master ~]# sbt:Hello> console
[info] Starting scala interpreter...
Welcome to Scala 2.12.7 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_171).
Type in expressions for evaluation. Or try :help.
scala> :paste
// Entering paste mode (ctrl-D to finish)
import scala.concurrent._, duration._
import gigahorse._, support.okhttp.Gigahorse
import play.api.libs.json._
Gigahorse.withHttp(Gigahorse.config) { http =>
val baseUrl = "https://www.metaweather.com/api/location"
val rLoc = Gigahorse.url(baseUrl + "/search/").get.
addQueryString("query" -> "New York")
val fLoc = http.run(rLoc, Gigahorse.asString)
val loc = Await.result(fLoc, 10.seconds)
val woeid = (Json.parse(loc) \ 0 \ "woeid").get
val rWeather = Gigahorse.url(baseUrl + s"/$woeid/").get
val fWeather = http.run(rWeather, Gigahorse.asString)
val weather = Await.result(fWeather, 10.seconds)
({Json.parse(_: String)} andThen Json.prettyPrint)(weather)
}
// press Ctrl+D
// Exiting paste mode, now interpreting.
scala> :q // to quit
Make a subproject
[root@master ~]# vim build.sbt
ThisBuild / scalaVersion := "2.12.7"
ThisBuild / organization := "com.example"
lazy val hello = (project in file("."))
.settings(
name := "Hello",
libraryDependencies += "com.eed3si9n" %% "gigahorse-okhttp" % "0.3.1",
libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.5" % Test,
)
lazy val helloCore = (project in file("core"))
.settings(
name := "Hello Core",
)
List all subprojects
[root@master ~]# sbt:Hello> projects
[info] In file:/tmp/foo-build/
[info] * hello
[info] helloCore
Compile the subproject
[root@master ~]# sbt:Hello> helloCore/compile
Add ScalaTest to the subproject
[root@master ~]# vim build.sbt
ThisBuild / scalaVersion := "2.12.7"
ThisBuild / organization := "com.example"
val scalaTest = "org.scalatest" %% "scalatest" % "3.0.5"
lazy val hello = (project in file("."))
.settings(
name := "Hello",
libraryDependencies += "com.eed3si9n" %% "gigahorse-okhttp" % "0.3.1",
libraryDependencies += scalaTest % Test,
)
lazy val helloCore = (project in file("core"))
.settings(
name := "Hello Core",
libraryDependencies += scalaTest % Test,
)
未完待续
https://www.scala-sbt.org/release/docs/sbt-by-example.html
…
————Blueicex 2020/2/12 12:40 [email protected]