搭建簡單SBT工程實踐


    在本機jdk(主要配置環境變量)、scala(主要配置環境變量)、sbt(主要配置①私服repositories  ②sbtconfig.txt)都已經安裝且配置好的情況下。

repositories:

[repositories]
local
  Ivy Repositories: http://xxxIP:port/repositories/, [organization]/[module]/(scala_[scalaVersion]/)(sbt_[sbtVersion]/)[revision]/[type]s/[artifact](-[classifier]).[ext]
  Maven Repositories: http://xxxIP:port/repositories/

 使用OSChina提供的Maven Center鏡像庫以及Ivy依賴,修改配置之前可以先備份本地原有配置。

(參考:https://segmentfault.com/a/1190000002474507)

[repositories]
local
oschina:http://maven.oschina.net/content/groups/public/ 
oschina-ivy:http://maven.oschina.net/content/groups/public/, [organization]/[module]/(scala_[scalaVersion]/)(sbt_[sbtVersion]/)[revision]/[type]s/[artifact](-[classifier]).[ext]
typesafe: http://repo.typesafe.com/typesafe/ivy-releases/, [organization]/[module]/(scala_[scalaVersion]/)(sbt_[sbtVersion]/)[revision]/[type]s/[artifact](-[classifier]).[ext], bootOnly
sonatype-oss-releases
maven-central
sonatype-oss-snapshots

可惜使用sbt命令后,依然出現依賴下載失敗的情況:

看來ChinaOS不可用,修改為阿里雲,發現可用:

[repositories]
local
nexus-aliyun:http://maven.aliyun.com/nexus/content/groups/public
nexus-aliyun-ivy:http://maven.aliyun.com/nexus/content/groups/public/, [organization]/[module]/(scala_[scalaVersion]/)(sbt_[sbtVersion]/)[revision]/[type]s/[artifact](-[classifier]).[ext]
typesafe: http://repo.typesafe.com/typesafe/ivy-releases/, [organization]/[module]/(scala_[scalaVersion]/)(sbt_[sbtVersion]/)[revision]/[type]s/[artifact](-[classifier]).[ext], bootOnly

 

sbtconfig.txt

(官方文檔:http://www.scala-sbt.org/0.13/docs/Proxy-Repositories.html):

官方解釋:http://www.scala-sbt.org/0.13/docs/Launcher-Configuration.html

# Set the java args to high

-Xmx1024M     #JVM最大允許分配的堆內存,按需分配   # Xms JVM初始分配的堆內存
-Xss10M       #設置每個線程的堆棧大小

-XX:MaxPermSize=1024m   #非堆區分配的內存的最大內存;持久代大小
-XX:ReservedCodeCacheSize=512m   #代碼緩存


# Set the extra SBT options

-Dsbt.log.format=true

-Dsbt.override.build.repos=true  #This setting is used to specify that all sbt project added resolvers should be ignored in favor of those configured in the repositories configuration. Using this with a properly configured

-Dsbt.ivy.home=D:/Tools/sbt_project/ivy2  #ivy的cache等,保存到指定的文件夾;不配置的話sbt下載的jar包都會默認放到C盤的用戶目錄下

-Dfile.encoding=UTF8

-Dsbt.boot.directory=D:/Tools/sbt_project/boot #The directory defined here is used to store all cached JARs resolved launcher.

-Dhttp.proxyHost=xxx.xxx.com   #代理設置

-Dhttp.proxyPort=xx

 

 第一種,建立好sbt工程,再用idea開發

 

1.按sbt規范,建好project、src等文件夾以及build.sbt、plugins.sbt文件,基本結構(官方文檔(有中文版):http://www.scala-sbt.org/0.13/docs/zh-cn/Organizing-Build.html):

2.build.sbt 配置:

name := "SimpleApp"   //任意

version := "1.0"     // 任意

scalaVersion := "2.10.4"   //與安裝的scala版本對應

3.plugins.sbt  為了能使用sbt gen-idea  等命令,要引入相關的插件,配置:

addSbtPlugin("com.github.mpeltonen" % "sbt-idea" % "1.6.0")   //sbt gen-idea

addSbtPlugin("io.spray" % "sbt-revolver" % "0.7.2")

addSbtPlugin("de.johoop" % "jacoco4sbt" % "2.1.5")

addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.11.2")    //sbt assembly

 

 

4.打開命令行,進入項目根目錄,輸入sbt gen-idea生成工程。

 

5.工程開發中要使用到配置文件

①在resource目錄下建立application.conf文件

②要使用到com.typesafe.config的jar包,為了讓sbt托管依賴。需要在build.sbt文件中添加依賴。

通過查http://mvnrepository.com,查到依賴寫法:

libraryDependencies += "com.typesafe" % "config" % "1.3.0"

需要注意,com.typesafe.config的jar包是否還依賴其他jar包,如果有其他依賴,也需要引入。

 

第二種:通過Idea創建sbt工程

 

1.idea安裝sbt插件:File->Settings->Plugins 搜索sbt,然后安裝。(sbt插件版本和本地安裝scala版本有關聯)

2.新建sbt工程,選擇工程所在路徑、輸入工程名即可:

自動生成sbt規范目錄:

自動生成的build.sbt內容:

name := "Project_Test"

version := "1.0"

 

這樣創建的工程,已經可以通過sbt命令行執行

sbt update

sbt compile

sbt package ……

 

還不可以執行sbt gen-idea(現在已經是idea工程,不執行gen-idea命令也沒關系了)、 sbt assembly

如果需要gen-idea,在plugins.sbt配置相關插件(修改后立即生效)即可,例如:

addSbtPlugin("com.github.mpeltonen" % "sbt-idea" % "1.6.0")

 

添加完成后,如果執行sbt gen-idea 命令,由於build.sbt文件中沒有配置scala版本,導致會去下載默認的一個scala的一些jar包。

在build.sbt文件中,設置scala版本: 

 scalaVersion in ThisBuild := "2.10.4"

 

3.為了打包 sbt assembly   (只使用sbt的 package 命令打包,是不會把第三方庫打包進去的,需要sbt的assembly pulgin,它的任務,就是負責把所有依賴的jar包都打成一個 fat jar)

在plugins.sbt文件中配置插件,例如:

addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.11.2")

 

這樣配置完后還不夠,還需要在build.sbt中配置:

import sbtassembly.Plugin.AssemblyKeys._

assemblySettings jarName in assembly :
= "test_project.jar" mainClass in assembly := Some("com.test.Boot") //package 相對於,例如 src/main/scala目錄

 

4.配置完成,進入工程所在目錄,使用命令行執行sbt assembly,完成打包,包在工程目錄的\target\scala-2.10下

 使用assembly打包出來,包名一般為 配置的name.jar,package打包,包名一般為 name_scalaVersion前兩段-version.jar

SimpleApp.jar
SimpleApp_2.10-1.0.jar

參考:

http://www.cnblogs.com/zhangqingping/p/4997324.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM