天天看點

idea 設定module_在IDEA中配置sbt

idea 設定module_在IDEA中配置sbt

(本文适用于Windows中安裝了java、scala、idea的情形)

第一次

配置sbt對項目進行管理

1. 進入官網https://www.scala-sbt.org/download.html下載下傳sbt(版本無影響,安裝路徑不能帶有空格)

2. 下載下傳後進入安裝目錄下的conf檔案夾

idea 設定module_在IDEA中配置sbt

.ivy2 .sbt boot三個檔案為後期加載生成

建立repo.properties檔案,并修改sbtconfig.txt檔案

idea 設定module_在IDEA中配置sbt

repo. properties建立内容:

[repositories]
local
aliyun nexus:http://maven.aliyun.com/nexus/content/groups/public
typesafe-ivy-releases: http://repo.typesafe.com/typesafe/ivy-releases/, [organization]/[module]/[revision]/[type]s/artifact.
[ext]
maven-central
sonatype-snapshots: https://oss.sonatype.org/content/repositories/snapshots           

sbtconfig.txt修改内容(其中D:/software_sp/spark-sbt要換成自己的sbt安裝目錄,注意檔案夾名稱要對應):

#-XX:+CMSClassUnloadingEnabled
-XX:MetaspaceSize=512m
-XX:MaxMetaspaceSize=768m
-Xss2m
-Xmx2048m
#-XX:MaxPermSize=256m support removed from 8.0

# Set the java args to high
-XX:ReservedCodeCacheSize=128m

# Set the extra SBT options
-Dsbt.log.format=true
-Dsbt.repository.config=D:/software_sp/spark-sbt/conf/repo.properties

#dependencies download position
-Dsbt.ivy.home=D:/software_sp/spark-sbt/.ivy2
-Dsbt.log.format=true
-Dsbt.boot.directory=D:/software_sp/spark-sbt/boot/
-Dsbt.global.base=D:/software_sp/spark-sbt/.sbt
-Dfile.encoding=UTF8
-Dsbt.override.build.repos=true           

3. 為提高下載下傳速度:

idea 設定module_在IDEA中配置sbt

參考連結:https://blog.csdn.net/bitbyteworld/article/details/52782776

ps:也可找到下載下傳了對應版本sbt的同鞋的sbt安裝目錄下的.ivy檔案,減少下載下傳時間。

4. 配置環境變量

idea 設定module_在IDEA中配置sbt
idea 設定module_在IDEA中配置sbt

5. 在指令行視窗輸入指令:sbt 驗證sbt是否安裝成功,若提示continue or quit,輸入continue。

idea 設定module_在IDEA中配置sbt

6. IDEA配置sbt:File -- Settings -- Buile,Execution -- Build Tools -- sbt,在VM parameters中填入上面的相關參數。(為避免麻煩可以進行全局設定:

舊版本的IDEA:File -- Other Settings -- Default Settings

新版本的IDEA:File -- Other Settings -- Settings for New Projects

如不配置,idea會自動在c盤使用者檔案夾下自動生成相關檔案,造成備援。全局設定後建立項目時将自動加載本地安裝目錄。

idea 設定module_在IDEA中配置sbt

VM parameters與conf檔案夾下sbtconfig.txt以-Dsbt指令開頭的參數相同,可選擇複雜粘貼;

選擇安裝在本地的sbt-launch.jar;

idea 設定module_在IDEA中配置sbt

7. 建立scala項目,選擇sbt進行管理

idea 設定module_在IDEA中配置sbt

8. 選擇對應的本地的jdk、scala、sbt版本号

idea 設定module_在IDEA中配置sbt

9. 點選finish完成建立,此時sbt插件将自動調用上述本地安裝的sbt軟體,下載下傳預設依賴包,生成sbt項目結構目錄,第一次項目dump需要一段時間。

idea 設定module_在IDEA中配置sbt

10. 加載完成後,打開項目Project Structure,在libraries中添加scala SDK

idea 設定module_在IDEA中配置sbt
idea 設定module_在IDEA中配置sbt

Browse本地的scala安裝目錄,如D:/software_sp/spark-scala點選OK,彈出Choose Modules框預設OK。

idea 設定module_在IDEA中配置sbt

11. 現在可添加依賴

idea 設定module_在IDEA中配置sbt

(如右下角有彈出是否自動import,選擇Enable Auto-import)

注:spark依賴有兩種編寫方式:

(1) 不明确指定spark庫的版本

依賴包的組織部分 "org.apache.spark" 後面加兩個%号,sbt自行去倉庫源裡面根據scala版本比對,例如:

libraryDependencies += "org.apache.spark" %% "spark-core" % "2.0.0"

(2) 明确指明spark庫版本

依賴包組織部分 "org.apache.spark" 後面隻加一個%号,例如:

libraryDependencies += "org.apache.spark" % "spark-core_2.11" % "2.0.0"

繼續閱讀