歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> 使用sbt搭建Scala開發環境的總結

使用sbt搭建Scala開發環境的總結

日期:2017/2/27 16:01:35   编辑:Linux教程
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安裝目錄]\sbt.bat,設置Java啟動參數:-Dsbt.ivy.home=E:/dev/ivy/
@REM SBT launcher script
@REM 
@REM Envioronment:
@REM JAVA_HOME - location of a JDK home dir (mandatory)
@REM SBT_OPTS  - JVM options (optional)
@setlocal
@echo off
set SBT_HOME=%~dp0
set ERROR_CODE=0
rem We use the value of the JAVACMD environment variable if defined
set _JAVACMD=%JAVACMD%
if "%_JAVACMD%"=="" (
  if not "%JAVA_HOME%"=="" (
    if exist "%JAVA_HOME%\bin\java.exe" set "_JAVACMD=%JAVA_HOME%\bin\java.exe"
  )
)

if "%_JAVACMD%"=="" set _JAVACMD=java
rem We use the value of the JAVA_OPTS environment variable if defined
set _JAVA_OPTS=%JAVA_OPTS%
if "%_JAVA_OPTS%"=="" set _JAVA_OPTS=-Xmx512M -XX:MaxPermSize=256m -XX:ReservedCodeCacheSize=128m -Dsbt.log.format=true -Dsbt.ivy.home=E:/dev/ivy/
:run
"%_JAVACMD%" %_JAVA_OPTS% %SBT_OPTS% -cp "%SBT_HOME%jansi.jar;%SBT_HOME%sbt-launch.jar;%SBT_HOME%classes" SbtJansiLaunch %*
if ERRORLEVEL 1 goto error
goto end
:error
set ERROR_CODE=1
:end
@endlocal
exit /B %ERROR_CODE%

用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“:

點擊所需庫的版本號到Artifact Details頁面:

展開Scala SBT,將裡面的Dependency Information拷貝粘貼到build.sbt文件裡面,然後在sbt console裡面運用update,eclipse。下載庫,配置項目的classpath就自動完成了。

Copyright © Linux教程網 All Rights Reserved