業(yè)界一直把sbt simple build tools ,叫成 SB tool,說(shuō)實(shí)話辐啄,入門沒(méi)有maven 和gradle 簡(jiǎn)單亭罪, 稍微不留意就不知道哪里就報(bào)錯(cuò)了。
不用sbt 的高階功能還湊合著用窜司,一旦你想用點(diǎn)谆焊,折磨的你死去活來(lái)的。
比如 sbt 的plugins坷剧,
我就發(fā)現(xiàn) scala sbt 好像跨版本兼容 簡(jiǎn)直就是狗屎一般惰爬,scala 和sbt 的版本最后是用一致的,否則 報(bào)錯(cuò) 何時(shí)了惫企。
另外 sbt 的plugin 插件 也要用對(duì) 版本號(hào)的撕瞧,否則 調(diào)試何時(shí)了。
另外 sbt 的版本分兩個(gè)分支 一個(gè) 1.0.X 一個(gè) 0.13.X,之前一直使用
0.13.13咩有問(wèn)題狞尔,但是發(fā)現(xiàn)在 plugin 安裝上 0.13.13 和0.13.15都傷透了我的心丛版,我scala 用的是 2.12.1,我發(fā)現(xiàn) sbt 0.13.13 和0.13.15所依賴的scala版本都是 2.10,就差不多 是jdk6吧偏序。最后在sbt 官網(wǎng)選擇了页畦,sbt 1.0.4,這個(gè)版本算是sbt的 正房了 當(dāng)時(shí)最新版本研儒。
也是用這個(gè)版本 真正解決了 sbt-assembly這個(gè)難題豫缨,用其他版本所出現(xiàn)的問(wèn)題是,assembly 依賴找不到 端朵,assembly 命令識(shí)別不了好芭,asembly 的關(guān)鍵字無(wú)法識(shí)別。
參考文件 :https://github.com/sbt/sbt-assembly#using-published-plugin
https://github.com/softprops/assembly-sbt
https://github.com/sbt/sbt-assembly/tree/0.11.2
http://wiki.jikexueyuan.com/project/sbt-getting-started/using-plugin.html
http://www.scala-sbt.org/download.html
下面步入正題
1.首先是目錄結(jié)構(gòu) 逸月,一定不要放錯(cuò)位置了栓撞。
用IDEA 創(chuàng)建 sbt scala 項(xiàng)目后,需要額外 再創(chuàng)建兩個(gè) 文件
./project/plugins.sbt 和 assembly.sbt
其中plugins.sbt在 項(xiàng)目 根目錄下的project目錄下
assembly.sbt則在 項(xiàng)目的根目錄下,放錯(cuò)位置碗硬,則assembly就不可以好好運(yùn)行了
然后是文件內(nèi)容
我的環(huán)境是
scala 2.12.1
sbt 1.0.4
java 8
IDEA 2017.2 + 因?yàn)樗?scala 插件2017.2.5才開始支持sbt 1.0.x !!!!
使用低版本的IDEA 一直有bug的
在 plugins.sbt中
logLevel := Level.Warn addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.14.6")
在 ./project/build.properties 中
sbt.version = 1.0.4
在 assembly.sbt
resolvers += Resolver.url("bintray-sbt-plugins", url("http://dl.bintray.com/sbt/sbt-plugin-releases"))(Resolver.ivyStylePatterns)
在 build.sbt
name := "DebugHDP"
version := "1.0"
scalaVersion := "2.12.1"
scalaVersion in ThisBuild := "2.12.1"
lazy val commonSettings = Seq(
organization := "com.example",
version := "0.1.0-SNAPSHOT"
)
lazy val app = (project in file(".")).
settings(commonSettings: _*).
settings(
name := "fat-jar-test"
).enablePlugins()
resolvers in Global ++= Seq(
"Sbt plugins" at "https://dl.bintray.com/sbt/sbt-plugin-releases",
"Maven Central Server" at "http://repo1.maven.org/maven2",
"TypeSafe Repository Releases" at "http://repo.typesafe.com/typesafe/releases/",
"TypeSafe Repository Snapshots" at "http://repo.typesafe.com/typesafe/snapshots/"
)
然后在 項(xiàng)目根目錄下
輸入 sbt update
稍等片刻瓤湘,sbt首次使用記得 要翻墻 ,或者使用中國(guó)倉(cāng)庫(kù)鏡像
這個(gè)時(shí)候 assembly 插件一般就會(huì)安裝成功了
然后 輸入 sbt plugins 可查看 本項(xiàng)目中的依賴的插件
`
[info] Loading settings from plugins.sbt ...
[info] Loading project definition from /Users/linkedmemuller/Documents/gitlab/DebugHDP/project
[info] Loading settings from assembly.sbt,build.sbt ...
[info] Set current project to DebugHDP (in build file:/Users/linkedmemuller/Documents/gitlab/DebugHDP/)
In file:/Users/linkedmemuller/Documents/gitlab/DebugHDP/
sbt.plugins.IvyPlugin: enabled in app
sbt.plugins.JvmPlugin: enabled in app
sbt.plugins.CorePlugin: enabled in app
sbt.plugins.JUnitXmlReportPlugin: enabled in app
sbt.plugins.Giter8TemplatePlugin: enabled in app
sbtassembly.AssemblyPlugin: enabled in app
`
我們發(fā)現(xiàn) sbtassembly.AssemblyPlugin: enabled in app
說(shuō)明assembly插件安裝成功了恩尾,之后輸入
sbt assembly 就可以打 fat jar了
正常的sbt打包是不會(huì)把依賴的第三方j(luò)ar包打到里面的弛说。
然后我們找到 打好的jar 包的路徑
在terminal 中 使用
java -jar jarName
然后就可以正常使用了。
另外 sbt 創(chuàng)建的scala 項(xiàng)目是沒(méi)有resources目錄的翰意,需要自己手工創(chuàng)建 木人,這也有一個(gè)重點(diǎn)就是 resources目錄的位置 和maven的不一樣
sbt 的Resources 要放在 項(xiàng)目根目錄下 ./src/,千萬(wàn)不要放到了./src/main信柿,否則是無(wú)法加載到目錄下的配置文件的。
最后的是 項(xiàng)目結(jié)構(gòu)是 ./src/resources
我們可以通過(guò) 在 ./src/resources 目錄下創(chuàng)建 properties屬性文件 檢驗(yàn)
import java.io.FileInputStream
import java.util.Properties
/**
* Created by linkedmemuller on 13/12/2017.
*/
class DeHdfs {
}
object DeHdfs{
def main(args: Array[String]): Unit = {
println("hello world clean")
val fileProperties:Properties=new Properties()
val filepath=DeHdfs.getClass.getClassLoader.getResource("./file.properties").getPath
println(filepath)
fileProperties.load(new FileInputStream(filepath))
val name=fileProperties.getProperty("Zoo")
println(name+"name")
}
}
這個(gè)時(shí)候還有一點(diǎn)要注意的就是 需要把 ./src/resources 目錄 標(biāo)記為 資源目錄 醒第,
File --> project Structure --> Modules --> 然后找到 ./src/resources
選中 右鍵點(diǎn)擊 選擇第三個(gè) Resources就可以了渔嚷,然后打包時(shí),才可以把 Resource的配置文件打包的jar包中
|
2
When you package it the csv-file will reside in the jar archive; right? Then you can't access it directly as a file. Instead you need to ask for a stream:
val stream = getClass.getResourceAsStream("/data.csv")
Possibly you need to wrap the stream in a InputStreamReader for the CSVReader to accept it:
val reader = new InputStreamReader(stream)
CSVReader.open(reader)
@Absurd-Mind you are completely right, it is CSVReader.open(reader). I've corrected the question now. I got it working with @thoredge's approach. But as he said,
I had to wrap the stream in an InputStreamReader as follows:
val reader = new InputStreamReader(getClass.getResourceAsStream("/data.csv"))
and then I used that reader with the CSV reader CSVReader.open(reader)
總的來(lái)說(shuō) 要用流Stream 讀取稠曼,
getClass.getClassLoader.getResourceAsStream(path)
用普通的getResource 就會(huì)失敗
在 build.sbt 加入這些
unmanagedSourceDirectories in Test := Seq(baseDirectory.value / "test")
unmanagedResourceDirectories in Compile := Seq(baseDirectory.value / "src/main/resources")
形病,另外我們也可以選擇 直接用IDEA自帶的打包工具
File --> project Structure --> Artifacts 創(chuàng)建 新的 包,然后選擇 主類霞幅。在idea 的菜單欄就選擇 Build --- Build Artifacts --build 就可以了
What is estimate to support SBT 1.0.x? or java.lang.ClassNotFoundException: org.jetbrains.sbt.CreateTasks$
最新版 IDEA 下載地址
https://www.jetbrains.com/idea/download/#section=mac
https://www.jetbrains.com/idea/download/previous.html?fromIDE=
sbt發(fā)布assembly解決jar包沖突問(wèn)題 deduplicate: different file contents found in the following
http://blog.csdn.net/oopsoom/article/details/41318599
https://stackoverflow.com/questions/25144484/sbt-assembly-deduplication-found-error