sbt编程语言scala的构建工具配置及项目构建(附带网盘下载)

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构建

再新建选择Maven-----------------》选择从原型构建----------》------------》scala-archtype-simple

在这里插入图片描述

下一步
在这里插入图片描述
maven配置

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

打开项目结构,选择全局库,点击+号,导入下载的scala文件(scala.zip解压的文件夹,是scala-sdk)
在这里插入图片描述

导完后应该是上图所示的样子

找不到org.scala-tools:maven-scala-plugin该插件

如果出现找不到插件导入下面两个依赖即可:

<!-- https://mvnrepository.com/artifact/org.scala-tools/maven-scala-plugin -->
    <dependency>
      <groupId>org.scala-tools</groupId>
      <artifactId>maven-scala-plugin</artifactId>
      <version>2.11</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-eclipse-plugin -->
    <dependency>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-eclipse-plugin</artifactId>
      <version>2.5.1</version>
    </dependency>

导完后clean一下,在编译。

构建完成的目录结构:
在这里插入图片描述

注意创建完毕之后,自带App静态对象,继承了Application

package scala;

public interface Application extends ScalaObject {
    void main(String[] var1);

    long executionStart();

    void executionStart_$eq(long var1);
}

这就造成了scala的语法问题,这是由于IDEA插件自动生成的pom文件中scala的版本是2.7,如最新版本语法不一样,这里需要修改成与本地scala一致的版本号。版本号在导入sdk哪里

在这里插入图片描述

修改为本地的scala版本。

scalac: bad constant pool index: 318 at pos: 2716

scalac: 
  bad constant pool index: 318 at pos: 2716
     while compiling: D:\Scala\ScalaProjects\scala-maven\src\test\scala\org\example\MySpec.scala
        during phase: globalPhase=typer, enteringPhase=namer
     library version: version 2.12.17
    compiler version: version 2.12.17
  reconstructed args: -classpath C:\Program Files\Java\jdk1.8.0_181\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\cldrdata.jar;C:\Program

== Source file context for tree position ==

     3 import org.specs._
     4 import org.specs.runner.{ConsoleRunner, JUnit4}
     5 
     6 class MySpecTest extends JUnit4(MySpec)
     7 //class MySpecSuite extends ScalaTestSuite(MySpec)
     8 object MySpecRunner extends ConsoleRunner(MySpec)
     9 

如果出现该错误,是在在没修改pom的scala版本号是就编译了导致test目录下存在一些旧语法的文件

在这里插入图片描述
删除这些就可以了。

如下图成功运行:

在这里插入图片描述

到此maven构建scala项目就完成了。

IDEA创建scala项目的三种方法

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xvwen

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值