使用 sbt-assembly 构建多项目 fat jar
Build multi-project fat jars with sbt-assembly
我有多个项目,主模块称为根,消费者和生产者模块具有依赖于核心模块的依赖项。核心模块包含与配置相关的 类。
我想用单独的 main 类 和 sbt-assembly 为消费者和生产者构建 2 个独立的 jar。但是,当我尝试像这样单独构建它们 sbt consumer/assembly
或通过 运行ning sbt assembly
完全构建它们时,我得到这样的错误并且 sbt 无法编译整个项目:
[error] (consumer / update) java.lang.IllegalArgumentException: a module is not authorized to depend on itself: demotest#demotest_2.11;0.1
[error] (producer / update) java.lang.IllegalArgumentException: a module is not authorized to depend on itself: demotest#demotest_2.11;0.1
我应该更改依赖项中的哪些内容才能正确打包 jar?我应该 运行 他们分开还是一起?
lazy val overrides = Seq("com.fasterxml.jackson.core" % "jackson-core" % "2.9.5",
"com.fasterxml.jackson.core" % "jackson-databind" % "2.9.5",
"com.fasterxml.jackson.module" % "jackson-module-scala_2.11" % "2.9.5")
lazy val commonSettings = Seq(
name := "DemoTest",
version := "0.1",
scalaVersion := "2.11.8",
resolvers += "Spark Packages Repo" at "http://dl.bintray.com/spark-packages/maven",
dependencyOverrides ++= overrides
)
lazy val assemblySettings = Seq(
assemblyMergeStrategy in assembly := {
case PathList("org","aopalliance", xs @ _*) => MergeStrategy.last
case PathList("javax", "inject", xs @ _*) => MergeStrategy.last
case PathList("javax", "servlet", xs @ _*) => MergeStrategy.last
case PathList("javax", "activation", xs @ _*) => MergeStrategy.last
case PathList("org", "apache", xs @ _*) => MergeStrategy.last
case PathList("com", "google", xs @ _*) => MergeStrategy.last
case PathList("com", "esotericsoftware", xs @ _*) => MergeStrategy.last
case PathList("com", "codahale", xs @ _*) => MergeStrategy.last
case PathList("com", "yammer", xs @ _*) => MergeStrategy.last
case PathList("org", "slf4j", xs @ _*) => MergeStrategy.last
case PathList("org", "neo4j", xs @ _*) => MergeStrategy.last
case PathList("com", "typesafe", xs @ _*) => MergeStrategy.last
case PathList("net", "jpountz", xs @ _*) => MergeStrategy.last
case PathList("META-INF", xs @ _*) => MergeStrategy.discard
case "about.html" => MergeStrategy.rename
case "META-INF/ECLIPSEF.RSA" => MergeStrategy.last
case "META-INF/mailcap" => MergeStrategy.last
case "META-INF/mimetypes.default" => MergeStrategy.last
case "plugin.properties" => MergeStrategy.last
case "log4j.properties" => MergeStrategy.last
case x =>
val oldStrategy = (assemblyMergeStrategy in assembly).value
oldStrategy(x)
}
)
lazy val sparkVersion = "2.2.0"
lazy val commonDependencies = Seq(
"org.apache.kafka" %% "kafka" % "1.1.0",
"org.apache.spark" %% "spark-core" % sparkVersion % "provided",
"org.apache.spark" %% "spark-sql" % sparkVersion,
"org.apache.spark" %% "spark-streaming" % sparkVersion,
"org.apache.spark" %% "spark-streaming-kafka-0-10" % sparkVersion,
"neo4j-contrib" % "neo4j-spark-connector" % "2.1.0-M4",
"com.typesafe" % "config" % "1.3.0",
"org.neo4j.driver" % "neo4j-java-driver" % "1.5.1",
"com.opencsv" % "opencsv" % "4.1",
"com.databricks" %% "spark-csv" % "1.5.0",
"com.github.tototoshi" %% "scala-csv" % "1.3.5",
"org.elasticsearch" %% "elasticsearch-spark-20" % "6.2.4"
)
lazy val root = (project in file("."))
.aggregate(consumer, producer)
lazy val core = (project in file("core"))
.settings(
commonSettings,
libraryDependencies ++= commonDependencies
)
lazy val consumer = (project in file("consumer"))
.settings(
commonSettings,
assemblySettings,
libraryDependencies ++= commonDependencies)
.settings(
mainClass in assembly := Some("consumer.SparkConsumer"),
assemblyJarName in assembly := "demo_consumer.jar"
).dependsOn(core)
lazy val producer = (project in file("producer"))
.settings(
commonSettings,
assemblySettings,
libraryDependencies ++= commonDependencies
).settings(
mainClass in assembly := Some("producer.KafkaCheckinsProducer"),
assemblyJarName in assembly := "demo_producer.jar"
).dependsOn(core)
问题出在这一行:
lazy val commonSettings = Seq(
name := "DemoTest", // should not be defined at this level!
)
这个 name
应该为 root 定义:
lazy val root = (project in file("."))
.aggregate(core, producer, consumer)
.settings(
name := "demotest"
)
子模块将以 "core"、"producer" 和 "consumer" 作为它们自己的名称,除非在它们的设置中未覆盖 name
。
另请注意,在您的示例中 .aggregate
不包括 core
,但它确实应该包括。
我有多个项目,主模块称为根,消费者和生产者模块具有依赖于核心模块的依赖项。核心模块包含与配置相关的 类。
我想用单独的 main 类 和 sbt-assembly 为消费者和生产者构建 2 个独立的 jar。但是,当我尝试像这样单独构建它们 sbt consumer/assembly
或通过 运行ning sbt assembly
完全构建它们时,我得到这样的错误并且 sbt 无法编译整个项目:
[error] (consumer / update) java.lang.IllegalArgumentException: a module is not authorized to depend on itself: demotest#demotest_2.11;0.1
[error] (producer / update) java.lang.IllegalArgumentException: a module is not authorized to depend on itself: demotest#demotest_2.11;0.1
我应该更改依赖项中的哪些内容才能正确打包 jar?我应该 运行 他们分开还是一起?
lazy val overrides = Seq("com.fasterxml.jackson.core" % "jackson-core" % "2.9.5",
"com.fasterxml.jackson.core" % "jackson-databind" % "2.9.5",
"com.fasterxml.jackson.module" % "jackson-module-scala_2.11" % "2.9.5")
lazy val commonSettings = Seq(
name := "DemoTest",
version := "0.1",
scalaVersion := "2.11.8",
resolvers += "Spark Packages Repo" at "http://dl.bintray.com/spark-packages/maven",
dependencyOverrides ++= overrides
)
lazy val assemblySettings = Seq(
assemblyMergeStrategy in assembly := {
case PathList("org","aopalliance", xs @ _*) => MergeStrategy.last
case PathList("javax", "inject", xs @ _*) => MergeStrategy.last
case PathList("javax", "servlet", xs @ _*) => MergeStrategy.last
case PathList("javax", "activation", xs @ _*) => MergeStrategy.last
case PathList("org", "apache", xs @ _*) => MergeStrategy.last
case PathList("com", "google", xs @ _*) => MergeStrategy.last
case PathList("com", "esotericsoftware", xs @ _*) => MergeStrategy.last
case PathList("com", "codahale", xs @ _*) => MergeStrategy.last
case PathList("com", "yammer", xs @ _*) => MergeStrategy.last
case PathList("org", "slf4j", xs @ _*) => MergeStrategy.last
case PathList("org", "neo4j", xs @ _*) => MergeStrategy.last
case PathList("com", "typesafe", xs @ _*) => MergeStrategy.last
case PathList("net", "jpountz", xs @ _*) => MergeStrategy.last
case PathList("META-INF", xs @ _*) => MergeStrategy.discard
case "about.html" => MergeStrategy.rename
case "META-INF/ECLIPSEF.RSA" => MergeStrategy.last
case "META-INF/mailcap" => MergeStrategy.last
case "META-INF/mimetypes.default" => MergeStrategy.last
case "plugin.properties" => MergeStrategy.last
case "log4j.properties" => MergeStrategy.last
case x =>
val oldStrategy = (assemblyMergeStrategy in assembly).value
oldStrategy(x)
}
)
lazy val sparkVersion = "2.2.0"
lazy val commonDependencies = Seq(
"org.apache.kafka" %% "kafka" % "1.1.0",
"org.apache.spark" %% "spark-core" % sparkVersion % "provided",
"org.apache.spark" %% "spark-sql" % sparkVersion,
"org.apache.spark" %% "spark-streaming" % sparkVersion,
"org.apache.spark" %% "spark-streaming-kafka-0-10" % sparkVersion,
"neo4j-contrib" % "neo4j-spark-connector" % "2.1.0-M4",
"com.typesafe" % "config" % "1.3.0",
"org.neo4j.driver" % "neo4j-java-driver" % "1.5.1",
"com.opencsv" % "opencsv" % "4.1",
"com.databricks" %% "spark-csv" % "1.5.0",
"com.github.tototoshi" %% "scala-csv" % "1.3.5",
"org.elasticsearch" %% "elasticsearch-spark-20" % "6.2.4"
)
lazy val root = (project in file("."))
.aggregate(consumer, producer)
lazy val core = (project in file("core"))
.settings(
commonSettings,
libraryDependencies ++= commonDependencies
)
lazy val consumer = (project in file("consumer"))
.settings(
commonSettings,
assemblySettings,
libraryDependencies ++= commonDependencies)
.settings(
mainClass in assembly := Some("consumer.SparkConsumer"),
assemblyJarName in assembly := "demo_consumer.jar"
).dependsOn(core)
lazy val producer = (project in file("producer"))
.settings(
commonSettings,
assemblySettings,
libraryDependencies ++= commonDependencies
).settings(
mainClass in assembly := Some("producer.KafkaCheckinsProducer"),
assemblyJarName in assembly := "demo_producer.jar"
).dependsOn(core)
问题出在这一行:
lazy val commonSettings = Seq(
name := "DemoTest", // should not be defined at this level!
)
这个 name
应该为 root 定义:
lazy val root = (project in file("."))
.aggregate(core, producer, consumer)
.settings(
name := "demotest"
)
子模块将以 "core"、"producer" 和 "consumer" 作为它们自己的名称,除非在它们的设置中未覆盖 name
。
另请注意,在您的示例中 .aggregate
不包括 core
,但它确实应该包括。