欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

工欲善其事,必先利其器之—MAC下搭建scala的开发环境

程序员文章站 2022-04-23 22:37:49
...

前置环境

  • 安装JDK(scala编译成java字节码,最终跑在jvm),需要java1.8以上的版本
  • 安装Atom或IDEA

安装scala与sbt

使用homebrew安装scala sdk和可以构建scala应用的工具

brew install scala
brew install aaa@qq.com1

各工具的版本

  • Scala : 2.12.6
  • Sbt : 1.1.6
  • IDEA : 2018.1.4
  • Atom : 1.23.2

tips: 由于scala与sbt是使用brew安装的,可以用brew info命令查看它们的版本,示例如下

aaa@qq.com-MacBook-Pro hello-world-template$ brew info scala
scala: stable 2.12.6, devel 2.13.0-M3

命令行的Scala开发环境

1.cd到一个空的目录,运行如下命令创建一个模板工程

# 该命令会从github上下载一个模板工程(所有要用网络呀!)
luogw@luogw-MacBook-Pro hello-world-template$ sbt new scala/hello-world.g8
[info] Set current project to hello-world-template (in build file:/Users/luogw/temp/hello-world-template/)
[info] Set current project to hello-world-template (in build file:/Users/luogw/temp/hello-world-template/)

A template to demonstrate a minimal Scala application

name [Hello World template]:

# 直接回车,使用默认工程名即可
# 回车后多一个 hello-world-template

2.cd到hello-world-template目录,并运行sbt命令,进入sbt的交互模式

aaa@qq.com hello-world-template$ cd hello-world-template/
aaa@qq.com hello-world-template$ sbt
[info] Loading project definition from /Users/luogw/temp/hello-world-template/hello-world-template/project
[info] Updating ProjectRef(uri("file:/Users/luogw/temp/hello-world-template/hello-world-template/project/"), "hello-world-template-build")...
[info] Done updating.
[info] Loading settings from build.sbt ...
[info] Set current project to hello-world (in build file:/Users/luogw/temp/hello-world-template/hello-world-template/)
[info] sbt server started at local:///Users/luogw/.sbt/1.0/server/3bab035d199a92d0d5cc/sock
sbt:hello-world>

3.运行应用,在步骤2的基础上执行 ~run命令

sbt:hello-world> ~run
[info] Updating ...
[info] Done updating.
[info] Compiling 1 Scala source to /Users/luogw/temp/hello-world-template/hello-world-template/target/scala-2.12/classes ...
[info] Done compiling.
[info] Packaging /Users/luogw/temp/hello-world-template/hello-world-template/target/scala-2.12/hello-world_2.12-1.0.jar ...
[info] Done packaging.
[info] Running Main
Hello, World!
[success] Total time: 1 s, completed 2018-6-28 14:55:42
1. Waiting for source changes... (press enter to interrupt)

注:输出结果后,sbt交互界面挂起,一直监听项目代码文件的更改,这意味着你可以用喜欢的编辑器打开项目的某个文件编辑(如src/main/scala/Main.scala),编辑后保存文件,sbt交互终端将重新编译项目并运行,示例如下

Hello, World!
[success] Total time: 1 s, completed 2018-6-28 14:55:42
1. Waiting for source changes... (press enter to interrupt)
[info] Compiling 1 Scala source to /Users/luogw/temp/hello-world-template/hello-world-template/target/scala-2.12/classes ...
[info] Done compiling.
[info] Packaging /Users/luogw/temp/hello-world-template/hello-world-template/target/scala-2.12/hello-world_2.12-1.0.jar ...
[info] Done packaging.
[info] Running Main
Hello, World!, go go go
[success] Total time: 1 s, completed 2018-6-28 15:01:16
2. Waiting for source changes... (press enter to interrupt)

Atom下的Scala开发环境

安装插件: ensime(该插件能使scala工程被atom打开发,编辑源文件时有代码补全,高亮等的支持),并在其settings中更新配置
* ensime server version为 2.0.0-SNAPSHOT
* sbt的路径 /usr/local/bin/sbt (使用which命令可以找到sbt的位置)

aaa@qq.com-MacBook-Pro hello-world-template$ which sbt
/usr/local/bin/sbt

工欲善其事,必先利其器之—MAC下搭建scala的开发环境

安装ensime-sbt的插件

在~/.sbt/1.0/plugins目录下新建文件plugins.sbt,并在文件中添加如下内容

addSbtPlugin("org.ensime" % "sbt-ensime" % "2.5.1")

创建工程

同命令行的scala开发环境步骤1

配置工程

在stb下运行 ensimeConfig,以便在当前工程下生成 .ensime 配置文件

aaa@qq.com hello-world-template$ sbt
[info] Loading settings from plugins.sbt ...
[info] Loading global plugins from /Users/luogw/.sbt/1.0/plugins
[info] Loading project definition from /Users/luogw/temp/hello-world-template/project
[info] Updating ProjectRef(uri("file:/Users/luogw/temp/hello-world-template/project/"), "hello-world-template-build")...
[info] Done updating.
[info] Loading settings from build.sbt ...
[info] Set current project to hello-world (in build file:/Users/luogw/temp/hello-world-template/)
[info] sbt server started at local:///Users/luogw/.sbt/1.0/server/d256dd7eefdec7178cf8/sock
sbt:hello-world> ensimeConfig
[info] ENSIME update.
[info] Updating ...
[info] Done updating.
[info] downloading https://repo1.maven.org/maven2/org/typelevel/cats-core_2.12/1.1.0/cats-core_2.12-1.1.0-javadoc.jar ...
[info] downloading https://repo1.maven.org/maven2/org/typelevel/machinist_2.12/0.6.2/machinist_2.12-0.6.2-javadoc.jar ...
[info] downloading https://repo1.maven.org/maven2/org/typelevel/cats-macros_2.12/1.1.0/cats-macros_2.12-1.1.0-javadoc.jar ...
[info] downloading https://repo1.maven.org/maven2/org/typelevel/cats-kernel_2.12/1.1.0/cats-kernel_2.12-1.1.0-javadoc.jar ...
[info]  [SUCCESSFUL ] org.typelevel#machinist_2.12;0.6.2!machinist_2.12.jar(doc) (3958ms)
[info] downloading https://repo1.maven.org/maven2/org/typelevel/machinist_2.12/0.6.2/machinist_2.12-0.6.2-sources.jar ...
[info]  [SUCCESSFUL ] org.typelevel#machinist_2.12;0.6.2!machinist_2.12.jar(src) (1052ms)
[info]  [SUCCESSFUL ] org.typelevel#cats-core_2.12;1.1.0!cats-core_2.12.jar(doc) (5747ms)
[info]  [SUCCESSFUL ] org.typelevel#cats-kernel_2.12;1.1.0!cats-kernel_2.12.jar(doc) (5813ms)
[info] downloading https://repo1.maven.org/maven2/org/typelevel/cats-core_2.12/1.1.0/cats-core_2.12-1.1.0-sources.jar ...
[info] downloading https://repo1.maven.org/maven2/org/typelevel/cats-kernel_2.12/1.1.0/cats-kernel_2.12-1.1.0-sources.jar ...
[info]  [SUCCESSFUL ] org.typelevel#cats-macros_2.12;1.1.0!cats-macros_2.12.jar(doc) (6355ms)
[info] downloading https://repo1.maven.org/maven2/org/typelevel/cats-macros_2.12/1.1.0/cats-macros_2.12-1.1.0-sources.jar ...
[info]  [SUCCESSFUL ] org.typelevel#cats-core_2.12;1.1.0!cats-core_2.12.jar(src) (1070ms)
[info]  [SUCCESSFUL ] org.typelevel#cats-kernel_2.12;1.1.0!cats-kernel_2.12.jar(src) (1037ms)
[info]  [SUCCESSFUL ] org.typelevel#cats-macros_2.12;1.1.0!cats-macros_2.12.jar(src) (1042ms)
[info] ENSIME processing hello-world-template (hello-world)
[info] Creating /Users/luogw/temp/hello-world-template/target/scala-2.12/src_managed/test. Read about `ensimeIgnoreMissingDirectories`
[info] Creating /Users/luogw/temp/hello-world-template/src/test/scala-2.12. Read about `ensimeIgnoreMissingDirectories`
[info] Creating /Users/luogw/temp/hello-world-template/src/test/scala. Read about `ensimeIgnoreMissingDirectories`
[info] Creating /Users/luogw/temp/hello-world-template/src/test/java. Read about `ensimeIgnoreMissingDirectories`
[info] Creating /Users/luogw/temp/hello-world-template/target/scala-2.12/src_managed/main. Read about `ensimeIgnoreMissingDirectories`
[info] Creating /Users/luogw/temp/hello-world-template/src/main/scala-2.12. Read about `ensimeIgnoreMissingDirectories`
[info] Creating /Users/luogw/temp/hello-world-template/src/main/java. Read about `ensimeIgnoreMissingDirectories`
[success] Total time: 9 s, completed 2018-6-28 16:37:10

打开工程

使用atom的add project folder功能导入前面创建的工程目录(hello-world-template)
如下图所示:相比使用atom直接编辑源代码文件的情况(命令行的开发环境),现在多个实时的代码高亮与补全等功能的支持。
工欲善其事,必先利其器之—MAC下搭建scala的开发环境

编码运行

同命令行环境一样,还是需要在sbt

aaa@qq.com hello-world-template$ sbt
[info] Loading settings from plugins.sbt ...
[info] Loading global plugins from /Users/luogw/.sbt/1.0/plugins
[info] Loading project definition from /Users/luogw/temp/hello-world-template/project
[info] Loading settings from build.sbt ...
[info] Set current project to hello-world (in build file:/Users/luogw/temp/hello-world-template/)
[info] sbt server started at local:///Users/luogw/.sbt/1.0/server/d256dd7eefdec7178cf8/sock
sbt:hello-world> ~run
[info] Compiling 1 Scala source to /Users/luogw/temp/hello-world-template/target/scala-2.12/classes ...
[info] Done compiling.
[info] Packaging /Users/luogw/temp/hello-world-template/target/scala-2.12/hello-world_2.12-1.0.jar ...
[info] Done packaging.
[info] Running Main
Hello, World!
go go go
[success] Total time: 2 s, completed 2018-6-28 17:50:29
1. Waiting for source changes... (press enter to interrupt)
[info] Compiling 1 Scala source to /Users/luogw/temp/hello-world-template/target/scala-2.12/classes ...
[info] Done compiling.
[info] Packaging /Users/luogw/temp/hello-world-template/target/scala-2.12/hello-world_2.12-1.0.jar ...
[info] Done packaging.
[info] Running Main
Hello, World!
go go go
yes
[success] Total time: 1 s, completed 2018-6-28 17:50:55
2. Waiting for source changes... (press enter to interrupt)

IDEA下的Scala开发环境

安装插件

安装插件scala,并重启Scala
工欲善其事,必先利其器之—MAC下搭建scala的开发环境

创建工程

1.新建项目 File >> New >> Projects
工欲善其事,必先利其器之—MAC下搭建scala的开发环境
2.新建代码文件
工欲善其事,必先利其器之—MAC下搭建scala的开发环境
选Object类型(可运行的代码单元)
工欲善其事,必先利其器之—MAC下搭建scala的开发环境

编码运行

工欲善其事,必先利其器之—MAC下搭建scala的开发环境

快捷的测试一段代码

* 使用Scala Worksheets来运行一段代码*
启动一个Worksheets的实例是在项目左则面板的src/main/scala目录上右键菜单,然后依序选择 New >> Scala Worksheets, 如下图所示
工欲善其事,必先利其器之—MAC下搭建scala的开发环境

注:Worksheets左则的代码编程区,右则是代运行结果(同步执行效果,当源代码变更时),类似命中行下的运行效果,估计实现原理也是同样的
工欲善其事,必先利其器之—MAC下搭建scala的开发环境

参考资料

相关标签: scala sbt ensime