sbt-assembly 是一个非常实用的sbt插件,可以将当前项目的二进制包以及依赖的所有第三方库都打包成一个jar包发布,即one-jar, 对于那种直接运行的应用程序很方便。
sbt版本:0.13.5
使用方法如下:
1、手工建立 ./project/assembly.sbt(用于下载sbt-assembly插件):
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.11.2")
2、手工建立 ./assembly.sbt(注意:文件放在根目录下,主要作用是对assembly进行设置):
import AssemblyKeys._ // put this at the top of the file
assemblySettings
// your assembly settings here
3、运行assembly任务,生成./target/scala_x.x.x/projectname-assembly-x.x.x.jar:
> assembly
import AssemblyKeys._
assemblySettings
jarName in assembly := "gogo.jar"
test in assembly := {}
mainClass in assembly := Some( "barnes.BoundMat01")
assemblyOption in packageDependency ~= { _.copy(appendContentHash = true) }
mergeStrategy in assembly <<= (mergeStrategy in assembly) { (old) =>
{
case PathList(ps @ _*) if ps.last endsWith "axiom.xml" => MergeStrategy.filterDistinctLines
case PathList(ps @ _*) if ps.last endsWith "Log.class" => MergeStrategy.first
case PathList(ps @ _*) if ps.last endsWith "LogConfigurationException.class" => MergeStrategy.first
case PathList(ps @ _*) if ps.last endsWith "LogFactory.class" => MergeStrategy.first
case PathList(ps @ _*) if ps.last endsWith "SimpleLog$1.class" => MergeStrategy.first
case x => old(x)
}
}
出现错误的解决方法:
[error] (*:assembly) deduplicate: different file contents found in the following:
[error] /home/mslinn/.ivy2/cache/org.fusesource.jansi/jansi/jars/jansi-1.4.jar:org/fusesource/jansi/Ansi$1.class
[error] /home/mslinn/.ivy2/cache/org.scala-lang/jline/jars/jline-2.10.2.jar:org/fusesource/jansi/Ansi$1.class
解决方法: