博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
工欲善其事,必先利其器之—MAC下搭建scala的开发环境
阅读量:4030 次
发布时间:2019-05-24

本文共 8350 字,大约阅读时间需要 27 分钟。

前置环境

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

安装scala与sbt

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

brew install scalabrew install sbt@1

各工具的版本

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

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

luogw@luogw-MacBook-Pro hello-world-template$ brew info scalascala: 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 applicationname [Hello World template]:# 直接回车,使用默认工程名即可# 回车后多一个 hello-world-template

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

luogw@luogw-MacBook-Pro hello-world-template$ cd hello-world-template/luogw@luogw-MacBook-Pro 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/socksbt: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 MainHello, World![success] Total time: 1 s, completed 2018-6-28 14:55:421. 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:421. 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 MainHello, World!, go go go[success] Total time: 1 s, completed 2018-6-28 15:01:162. 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的位置)

luogw@luogw-MacBook-Pro hello-world-template$ which sbt/usr/local/bin/sbt

这里写图片描述

安装ensime-sbt的插件

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

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

创建工程

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

配置工程

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

luogw@luogw-MacBook-Pro 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/socksbt: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直接编辑源代码文件的情况(命令行的开发环境),现在多个实时的代码高亮与补全等功能的支持。
这里写图片描述

编码运行

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

luogw@luogw-MacBook-Pro 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/socksbt: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 MainHello, World!go go go[success] Total time: 2 s, completed 2018-6-28 17:50:291. 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 MainHello, World!go go goyes[success] Total time: 1 s, completed 2018-6-28 17:50:552. Waiting for source changes... (press enter to interrupt)

IDEA下的Scala开发环境

安装插件

安装插件scala,并重启Scala

这里写图片描述

创建工程

1.新建项目 File >> New >> Projects

这里写图片描述
2.新建代码文件
这里写图片描述
选Object类型(可运行的代码单元)
这里写图片描述

编码运行

这里写图片描述

快捷的测试一段代码

* 使用Scala Worksheets来运行一段代码*

启动一个Worksheets的实例是在项目左则面板的src/main/scala目录上右键菜单,然后依序选择 New >> Scala Worksheets, 如下图所示
这里写图片描述

注:Worksheets左则的代码编程区,右则是代运行结果(同步执行效果,当源代码变更时),类似命中行下的运行效果,估计实现原理也是同样的

这里写图片描述

参考资料

你可能感兴趣的文章
linux irqdebug
查看>>
git 常用命令
查看>>
linux位操作API
查看>>
snprintf 函数用法
查看>>
uboot.lds文件分析
查看>>
uboot start.s文件分析
查看>>
没有路由器的情况下,开发板,虚拟机Ubuntu,win10主机,三者也可以ping通
查看>>
本地服务方式搭建etcd集群
查看>>
安装k8s Master高可用集群
查看>>
忽略图片透明区域的事件(Flex)
查看>>
忽略图片透明区域的事件(Flex)
查看>>
AS3 Flex基础知识100条
查看>>
Flex动态获取flash资源库文件
查看>>
flex中设置Label标签文字的自动换行
查看>>
Flex 中的元数据标签
查看>>
flex4 中创建自定义弹出窗口
查看>>
01Java基础语法-11. 数据类型之间的转换
查看>>
01Java基础语法-13. if分支语句的灵活使用
查看>>
01Java基础语法-15.for循环结构
查看>>
01Java基础语法-16. while循环结构
查看>>