如何在IntelliJ IDEA中使用SBT构建Uber JAR(Fat JAR)? [英] How to build an Uber JAR (Fat JAR) using SBT within IntelliJ IDEA?
问题描述
我正在使用SBT(在IntelliJ IDEA中)构建一个简单的Scala项目。
I'm using SBT (within IntelliJ IDEA) to build a simple Scala project.
我想知道什么是最简单的方法建立一个优步JAR 文件(又名Fat JAR,超级JAR)。
I would like to know what is the simplest way to build an Uber JAR file (aka Fat JAR, Super JAR).
我目前正在使用SBT,但当我在将我的JAR文件提交到 Apache Spark 我收到以下错误:
I'm currently using SBT but when I'm submiting my JAR file to Apache Spark I get the following error:
线程main中的异常java.lang.SecurityException:Manifest主要属性的
签名文件摘要无效
Exception in thread "main" java.lang.SecurityException: Invalid signature file digest for Manifest main attributes
编译时出现此错误:
java.lang.RuntimeException:deduplicate:找到不同的文件内容
以下:
PATH \DEPENDENCY.jar:META-INF / DEPENDENCIES
PATH \DEPENDENCY.jar:META-INF / MANIFEST.MF
java.lang.RuntimeException: deduplicate: different file contents found in the following:
PATH\DEPENDENCY.jar:META-INF/DEPENDENCIES
PATH\DEPENDENCY.jar:META-INF/MANIFEST.MF
看起来l这是因为我的一些依赖项包括签名文件(META-INF),需要在最终的Uber JAR文件中删除。
It looks like it is because some of my dependencies include signature files (META-INF) which needs to be removed in the final Uber JAR file.
我试过使用 sbt-assembly 插件,如下所示:
I tried to use the sbt-assembly plugin like that:
/ project /assembly.sbt
/project/assembly.sbt
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.12.0")
/project/plugins.sbt
/project/plugins.sbt
logLevel := Level.Warn
/build.sbt
/build.sbt
lazy val commonSettings = Seq(
name := "Spark-Test"
version := "1.0"
scalaVersion := "2.11.4"
)
lazy val app = (project in file("app")).
settings(commonSettings: _*).
settings(
libraryDependencies ++= Seq(
"org.apache.spark" %% "spark-core" % "1.2.0",
"org.apache.spark" %% "spark-streaming" % "1.2.0",
"org.apache.spark" % "spark-streaming-twitter_2.10" % "1.2.0"
)
)
当我点击 Build Artifact ... 时IntelliJ IDEA我得到一个JAR文件。但我最终得到了同样的错误...
When I click "Build Artifact..." in IntelliJ IDEA I get a JAR file. But I end up with the same error...
我是SBT的新手,并没有对IntelliJ IDE进行过多尝试。
I'm new to SBT and not very experimented with IntelliJ IDE.
谢谢。
推荐答案
最后,我完全跳过使用IntelliJ IDEA来避免在我的全球理解中产生噪音:)
Finally I totally skip using IntelliJ IDEA to avoid generating noise in my global understanding :)
我开始阅读官方SBT教程。
我使用以下文件结构创建了我的项目:
I created my project with the following file structure :
my-project/project/assembly.sbt
my-project/src/main/scala/myPackage/MyMainObject.scala
my-project/build.sbt
添加 sbt-assembly 插件在我的 assembly.sbt 文件中。允许我构建一个胖JAR:
Added the sbt-assembly plugin in my assembly.sbt file. Allowing me to build a fat JAR :
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.12.0")
我的最小 build.sbt 看起来像:
lazy val root = (project in file(".")).
settings(
name := "my-project",
version := "1.0",
scalaVersion := "2.11.4",
mainClass in Compile := Some("myPackage.MyMainObject")
)
libraryDependencies ++= Seq(
"org.apache.spark" %% "spark-core" % "1.2.0" % "provided",
"org.apache.spark" %% "spark-streaming" % "1.2.0" % "provided",
"org.apache.spark" % "spark-streaming-twitter_2.10" % "1.2.0"
)
// META-INF discarding
mergeStrategy in assembly <<= (mergeStrategy in assembly) { (old) =>
{
case PathList("META-INF", xs @ _*) => MergeStrategy.discard
case x => MergeStrategy.first
}
}
注意 : %提供了
表示不在最终的胖JAR中包含依赖项(这些库已包含在我的工作人员中)
Note: The % "provided"
means not to include the dependency in the final fat JAR (those libraries are already included in my workers)
注意:META- INF放弃受此答案启发。
Note: META-INF discarding inspired by this answser.
现在我可以使用SBT构建我的胖JAR(如何安装它)通过在我的 / my-project 根文件夹:
Now I can build my fat JAR using SBT (how to install it) by running the following command in my /my-project root folder:
sbt assembly
我的胖JAR现在位于新生成的 / target 文件夹中:
My fat JAR is now located in the new generated /target folder :
/my-project/target/scala-2.11/my-project-assembly-1.0.jar
希望能帮助别人。
对于那些想要入侵的人IntelliJ IDE中的SBT:如何运行sbt-assembly任务来自IntelliJ IDEA?
For those who wants to embeed SBT within IntelliJ IDE: How to run sbt-assembly tasks from within IntelliJ IDEA?
这篇关于如何在IntelliJ IDEA中使用SBT构建Uber JAR(Fat JAR)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!