1. 程式人生 > >在IDEA中使用SBT構建工具建立SCALA專案

在IDEA中使用SBT構建工具建立SCALA專案

最近學習SCALA語言,用到IDEA建立以sbt構建的專案,其中遇到很多問題,現在把我建專案的步驟和解決辦法做如下總結,希望幫助後來人少走彎路。

一,環境

win10    jdk1.8    scala 2.12.0    sbt1.1.2

idea scala 外掛地址https://plugins.jetbrains.com/plugin/1347-scala

sbt 構建工具下載https://www.scala-sbt.org/download.html

scala 下載 http://www.scala-lang.org/download/all.html

環境變數 SCALA_HOME = D:\develop\scala-2.12.0

               SBT_HOME = D:\develop\sbt

                path = %SCALA_HOME%\bin;%SBT_HOME%\bin;

環境變數設定好了後請自行檢測,這裡不再做說明

sbt 設定倉庫地址步驟:

在SBT_HOME\sbt\conf 下新建repository.properties 檔案,內容如下

[repositories]

local

ali: http://maven.aliyun.com/nexus/content/repositories/central/

修改sbtconfig.txt檔案,加上如下內容:

# Set the extra SBT options
-Dsbt.log.format=true
-Dsbt.boot.directory=D:/develop/sbt/.sbt/boot
-Dsbt.ivy.home=D:/develop/sbt/.ivy2
-Dsbt.global.base=D:/develop/sbt/.sbt

-Dsbt.repository.config=D:/develop/sbt/conf/repository.properties

這些是設定sbt下載專案依賴目錄。

到此,環境都準備完畢

二,在idea中建立專案

1.選擇IDEA選單File->New->Project


2.點選Next,目前idea使用的sbt版本為0.13.X系列與我們下載的1.1.2版本不同,這裡不要緊,選擇最新的版本就好了,我們等下在專案中修改過來就好了。(不知道為什Idea使用的sbt版本和官網版本為兩個系列)

3.建立如下目錄結構


4.修改project目錄下build.properties檔案中sbt.version為1.1.2


5.選擇File->project Structure 

左邊選擇Modules 右邊選擇Sources,將src目錄Mark as Sources後就可以在src下新建Scala類了

然後右邊繼續選擇Dependencies,點選+號,新增Scala類庫



6.修改sbt構建設定,其中VM parameters配置為

-Xmx512M
-XX:MaxPermSize=256m
-XX:ReservedCodeCacheSize=128m
-Dsbt.log.format=true
-Dsbt.ivy.home=D:/develop/sbt/.ivy2
-Dsbt.global.base=D:/develop/sbt/.sbt

-Dsbt.repository.config=D:/develop/sbt/conf/repository.properties


好了到這裡結束後,就可以新建一個類測試下環境是否運轉。在src->scala包下新建Main檔案,如下:

package main.scala

/**
  * Created by Administrator on 2018/4/5.
  */
object Main extends App {
  val ages = Seq(42, 75, 29, 64)
  println(s"The oldest person is ${ages.max}")
}

在檔案上選中,右鍵選單選擇run Main,執行成功。結果如下:


----------------------------------------------------------------------------------------------------------------------------------

關於新建sbt task的步驟和設定

1.選擇run->edit configuration,點選左上角+號,選擇sbt task,

tasks輸入:~run,

VM parameters輸入:

-Xms512M
-Xmx1024M
-Xss1M
-XX:+CMSClassUnloadingEnabled
-Dsbt.log.format=true
-Dsbt.boot.directory=D:/develop/sbt/.sbt/boot
-Dsbt.ivy.home=D:/develop/sbt/.ivy2
-Dsbt.global.base=D:/develop/sbt/.sbt
-Dsbt.repository.config=D:/develop/sbt/conf/repository.properties