本文共 8350 字,大约阅读时间需要 27 分钟。
使用homebrew安装scala sdk和可以构建scala应用的工具
brew install scalabrew install sbt@1
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
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)
安装插件: 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
在~/.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)
安装插件scala,并重启Scala
1.新建项目 File >> New >> Projects
2.新建代码文件 选Object类型(可运行的代码单元)* 使用Scala Worksheets来运行一段代码*
启动一个Worksheets的实例是在项目左则面板的src/main/scala目录上右键菜单,然后依序选择 New >> Scala Worksheets, 如下图所示注:Worksheets左则的代码编程区,右则是代运行结果(同步执行效果,当源代码变更时),类似命中行下的运行效果,估计实现原理也是同样的