SBT简介
SBT 是 Scala 的构建工具,全称是 Simple Build Tool, 类似 Maven 或 Gradle。
Java可以用Maven快速构建项目,scala用SBT快速构建一个Scala项目。
sbt下载官网
百度网盘链接:https://pan.baidu.com/s/1eJkdWndZ0izcd3waqA2hdQ
提取码:oeq1
SBT参数配置
配置本地目录:
在config的该目录下追加内容
-Dsbt.log.format=true
-Dfile.encoding=UTF8
-Dsbt.global.base=e:/sbt/.sbt
-Dsbt.boot.directory=e:/sbt/.sbt/boot/
-Dsbt.repository.config=e:/sbt/.sbt/repo.properties
-Dsbt.ivy.home=e:/sbt/.ivy2
-Dsbt.override.build.repos=true
-Xmx512M
-Xss2M
-XX:+CMSClassUnloadingEnabled
有路径的设置为本地路径即可
设置阿里镜像(新建repo.properties)
新建的目录要和上面配置的对应
[repositories]
local
aliyun: http://maven.aliyun.com/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
java.io.IOException: failed to create lock file e:\Scala\sbt-1.7.2\repo\boot\sbt.boot.lock
java.io.IOException: failed to create lock file e:\Scala\sbt-1.7.2\repo\boot\sbt.boot.lock
at xsbt.boot.Locks$.liftedTree1$1(Locks.scala:43)
at xsbt.boot.Locks$.apply0(Locks.scala:41)
at xsbt.boot.Locks$.apply(Locks.scala:36)
at xsbt.boot.Launch.locked(Launch.scala:393)
at xsbt.boot.Launch.getAppProvider(Launch.scala:304)
at xsbt.boot.Launch.app(Launch.scala:216)
at xsbt.boot.Launch.app(Launch.scala:214)
at xsbt.boot.Launch$.run(Launch.scala:140)
at xsbt.boot.Launch$.$anonfun$apply$1(Launch.scala:44)
at xsbt.boot.Launch$.launch(Launch.scala:159)
at xsbt.boot.Launch$.apply(Launch.scala:44)
at xsbt.boot.Launch$.apply(Launch.scala:21)
at xsbt.boot.Boot$.runImpl(Boot.scala:78)
at xsbt.boot.Boot$.run(Boot.scala:73)
at xsbt.boot.Boot$.main(Boot.scala:21)
at xsbt.boot.Boot.main(Boot.scala)
Caused by: java.io.IOException: 系统找不到指定的路径。
at java.io.WinNTFileSystem.createFileExclusively(Native Method)
at java.io.File.createNewFile(File.java:1012)
at xsbt.boot.Locks$.liftedTree1$1(Locks.scala:41)
... 15 more
[error] [launcher] error during sbt launcher: java.io.IOException: failed to create lock file e:\Scala\sbt-1.7.2\repo\boot\sbt.boot.lock
Dsbt.repository.config=e:/sbt/.sbt/repo.properties
在本地设置时/
是在Linux系统的路径,在window中要使用\
。例如本地配置
# sbt configuration file for Windows
# Set the java args
#-mem 1024 was added in sbt.bat as default
#-Xms1024m
#-Xmx1024m
#-Xss4M
#-XX:ReservedCodeCacheSize=128m
# Set the extra sbt options
# -Dsbt.log.format=true
-Dsbt.log.format=true
-Dfile.encoding=UTF8
-Dsbt.global.base=d:\Scala\sbt-1.7.2\repo\base
-Dsbt.boot.directory=d:\Scala\sbt-1.7.2\repo\boot
-Dsbt.repository.config=d:\Scala\sbt-1.7.2\repo\repo.properties
-Dsbt.ivy.home=d:\Scala\sbt-1.7.2\repo\.ivy2
-Dsbt.override.build.repos=true
出现该错误都是路径问题,未找到路径或路径配置不对注意修改
安装完成测试
- 若sbt配置了环境变量直接在dos下输入
sbt
- 未配置环境变量在sbt的bin目录下输入
sbt
>出现上面界面即配置完成
IDEA 基于sbt构建scala项目
基于IDEA的创建方式需要全局配置sbt,即配置环境变量。
之后版本选择与本地版本对应就行了
第一次加载时间会很长,切换为阿里镜像会快一点
【切换阿里镜像】找到用户资料的文件夹下的.sbt
文件夹
创建repo.properties
文件:
[repositories]
local
Nexus osc : http://maven.oschina.net/content/groups/public/
Nexus osc thirdparty : http://maven.oschina.net/content/repositories/thirdparty/
typesafe: http://repo.typesafe.com/typesafe/ivy-releases/, [organization]/[module]/(scala_[scalaVersion]/)(sbt_[sbtVersion]/)[revision]/[type]s/[artifact](-[classifier]).[ext], bootOnly
typesafe2: http://repo.typesafe.com/typesafe/releases/
sbt-plugin: http://repo.scala-sbt.org/scalasbt/sbt-plugin-releases/
sonatype: http://oss.sonatype.org/content/repositories/snapshots
uk_maven: http://uk.maven.org/maven2/
ibibli: http://mirrors.ibiblio.org/maven2/
repo2: http://repo2.maven.org/maven2/
添加镜像后重启项目,刷新sbt
[info] loading settings for project untitled1 from build.sbt ...
[info] set current project to untitled1 (in build file:/D:/Scala/ScalaProjects/untitled1/)
[warn] there's a key that's not used by any other settings/tasks:
[warn]
[warn] * untitled1 / idePackagePrefix
[warn] +- D:\Scala\ScalaProjects\untitled1\build.sbt:7
[warn]
[warn] note: a setting might still be used by a command; to exclude a key from this `lintUnused` check
[warn] either append it to `Global / excludeLintKeys` or call .withRank(KeyRanks.Invisible) on the key
[info] sbt server started at local:sbt-server-fe2e6dcb84198d6f4b31
[info] started sbt server
sbt:untitled1>
;set _root_.scala.collection.Seq(historyPath := None,shellPrompt := { _ => "" }
,SettingKey[_root_.scala.Option[_root_.sbt.File]]("sbtStructureOutputFile") in _root_.sbt.Global := _root_.scala.Some(_root_.sbt.file("C:/Users/fireapproval/AppData/Local/Temp/sbt-structure.xml")),SettingKey[_root_.java.lang.String]
("sbtStructureOptions") in _root_.sbt.Global := "download, resolveClassifiers, resolveSbtClassifiers")
[info] Defining Global / sbtStructureOptions, Global / sbtStructureOutputFile and 1 others.
[info] The new values will be used by cleanKeepGlobs
[info] Run `last` for details.
[info] Reapplying settings...
[info] set current project to untitled1 (in build file:/D:/Scala/ScalaProjects/untitled1/)
[info] Applying State transformations org.jetbrains.sbt.CreateTasks from C:/Users/fireapproval/AppData/Roaming/JetBrains/IntelliJIdea2021.2/plugins/Scala/repo/org/jetbrains/scala/sbt-structure-extractor_2.12_1.0/2021.1.1/sbt-structure-extractor-2021.1.1.jar
[info] Reapplying settings...
[info] set current project to untitled1 (in build file:/D:/Scala/ScalaProjects/untitled1/)
[warn] sbt 0.13 shell syntax is deprecated; use slash syntax instead: Global / dumpStructure
[info] Writing structure to C:\Users\fireapproval\AppData\Local\Temp\sbt-structure.xml...
[info] Done.
[success] Total time: 1 s, completed 2023-1-23 18:47:10
[info] shutting down sbt server
出现上面的就表示构建成功
构建的项目如图所示:
基于Maven构建
打开设置中的项目结构:
打开项目结构,选择全局库,点击+号,导入下载的scala文件(scala.zip解压的文件夹,是scala-sdk)
导完后应该是上图所示的样子
再新建选择Maven-----------------》选择从原型构建----------》------------》scala-archtype-simple
下一步
maven配置
IDEA创建scala项目的三种方法