使用sbt搭建Scala开发环境的总结

Scala是一种运行于JVM之上的新型语言。JRuby, Jython, Groovy等也可以将其他语言的一些特点带进Java的生态圈。但就个人体会,这些编程语言和Scala相比,如同Symbian之于Android或IOS。虽然大家都是一个圈子里面的,但完全不在同一个时代。加之Play Framework的助力,Scala的路会越走越宽。对于企业开发,Scala最大的障碍不在对之前Java产品的支持,而在程序员的数量上。Scala的切入点很好,因为它是强类型的语言,在运行的性能上远超出JRuby, Jython, Groovy,执行效率与Native的Java程序几无差别。在对Java的互操作方面体现很好,这就保证了企业可在使用Scala的开发中保证了之前Java软件产品的投资不会丢失。而它带入的函数式编程,对多线程编程的良好支持,为开发大量并发应用体现出优势。(这也是Play Framework的亮点)

回到现实,Scala短期内还无迹象被业界大规模采用。但如果打算将Scala带进企业开发,最好的出发点可能是自动化测试和支持软件开发的工具。对于这两方面,大多数企业的态度是,知道长期来看它的好处,但不愿在眼下去投资。如果要想做些事情,程序员不得不牺牲自己的业余时间。而Scala用于此再合适不过。所以下面我将Scala项目的开发搭建搭建做了一个总结。学语言,学以致用,用它去做一些正经的事情。

1. 安装、配置需要的软件

Scala:      http://www.scala-lang.org/downloads

Scala IDE:  http://scala-ide.org/

Scala-sbt: http://www.scala-sbt.org/release/docs/Getting-Started/Setup.html

因为sbt会使用ivy作为库管理工具。ivy默认把library repository建在user home下面。Unix/Linux/Mac OS都还好说,如果就一个分区(或一个逻辑分区),无所谓发在哪个位置啦。如果操作系统是Windows,有分了C: D: E: 等若干分区,还是不要放在默认的%USERPROFILE%下面,C盘会随着开发的项目越来越多,大量的空间被开发库所占用。定制library local repository的位置的方法是:

修改sbt配置文件:[sbt安装目录]\conf\sbtconfig.txt

文件中添加一行: -Dsbt.ivy.home=E:/dev/ivy/

用sbt创建,运行,测试和发布项目都很方便,但这还不够,软件开发是要写代码的。而务实的开发者都会用现代的IDE去写代码。用写字板,vi或其他文本编辑做开发,更多的是体现一种编程文化的象征意义,正经干活没必要放着更先进的好东西不用。安装sbteclipse插件https://github.com/typesafehub/sbteclipse, 可以在sbt console里面生成eclipse scala project。更为有用的是,sbteclipse在生成项目时会根据build.sbt里面定义的库依赖,生成一个.classpath文件。也就是已经把Eclipse Scala项目的classpath,设置好了。如果以后的开发中有使用新的第三方库,安装如下次序,可以更新classpath的配置

1). 在build.sbt里面定义库依赖

2). 在sbt console里面执行update命令,下载相应的库到local library repository

3). 在sbt console里面执行eclipse命令,更新classpath设置

 

2. 用sbt创建项目

这里用一个例子来说明:

f:\tmp\test>dir
 Volume in drive F is file
 Volume Serial Number is A646-F0A7
 
 Directory of f:\tmp\test
 
02/19/2013  02:09 PM    <DIR>          .
02/19/2013  02:09 PM    <DIR>          ..
02/19/2013  11:04 AM               269 build.sbt
02/19/2013  02:08 PM    <DIR>          target
               1 File(s)            269 bytes
               3 Dir(s)  29,323,710,464 bytes free
 
f:\tmp\test>type build.sbt
name := "Project Plan"
 
version := "1.0"
 
scalaVersion := "2.9.2"
 
libraryDependencies ++= Seq(
 "org.mongodb" %% "casbah" % "2.5.0",
 "net.sourceforge" % "mpxj" % "4.3.0"
)
 
 
f:\tmp\test>sbt
[info] Loading global plugins from C:\Users\gzhang\.sbt\plugins
[info] Set current project to Project Plan (in build file:/F:/tmp/test/)
> eclipse
[info] About to create Eclipse project files for your project(s).
[info] Updating {file:/F:/tmp/test/}default-c4a35f...
[info] Resolving org.scala-lang#scala-library;2.9.2 ...
[info] Resolving org.mongodb#casbah_2.9.2;2.5.0 ...
[info] Resolving org.mongodb#casbah-commons_2.9.2;2.5.0 ...
[info] Resolving com.github.nscala-time#nscala-time_2.9.2;0.2.0 ...
[info] Resolving joda-time#joda-time;2.1 ...
[info] Resolving org.joda#joda-convert;1.2 ...
[info] Resolving org.specs2#specs2_2.9.2;1.12.2 ...
[info] Resolving org.specs2#specs2-scalaz-core_2.9.2;6.0.1 ...
[info] Resolving org.slf4j#slf4j-api;1.6.0 ...
[info] Resolving org.mongodb#mongo-java-driver;2.10.1 ...
[info] Resolving org.mongodb#casbah-core_2.9.2;2.5.0 ...
[info] Resolving org.mongodb#casbah-query_2.9.2;2.5.0 ...
[info] Resolving org.mongodb#casbah-gridfs_2.9.2;2.5.0 ...
[info] Resolving net.sourceforge#mpxj;4.3.0 ...
[info] Resolving org.apache.poi#poi;3.7 ...
[info] Resolving junit#junit;3.8.1 ...
[info] Done updating.
[info] Successfully created Eclipse project files for project(s):
[info] Project Plan
> exit
 
f:\tmp\test>tree
Folder PATH listing for volume file
Volume serial number is A646-F0A7
F:.
├─project
│  └─target
│      └─config-classes
├─src
│  ├─main
│  │  ├─java
│  │  └─scala
│  └─test
│      ├─java
│      └─scala
└─target
    ├─scala-2.9.2
    │  └─cache
    │      └─update
    └─streams
        └─$global
            ├─ivy-configuration
            │  └─$global
            ├─ivy-sbt
            │  └─$global
            ├─project-descriptors
            │  └─$global
            └─update
                └─$global

其中,

定义build.sbt文件,请参照sbt的文档:http://www.scala-sbt.org/release/docs/index.html。 特别说明的是,当添加一个依赖库的时候,通过Maven Central Repository Search来查找很便捷。比如,项目需要使用Mongo DB的driver

在Maven Central Repository里面查找”mongodb“:



转载自:http://my.oschina.net/gczhang/blog/110421


你可能感兴趣的:(使用sbt搭建Scala开发环境的总结)