且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在IntelliJ IDEA中使用SBT构建Uber JAR(Fat JAR)?

更新时间:2021-12-16 01:11:04

最后,我完全跳过使用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?