使用sbt-assembly构建多项目胖罐

时间:2018-06-08 09:43:46

标签: scala sbt sbt-assembly multi-project

我有一个主项目的多项目,称为root,consumer和producer模块,依赖于核心模块的依赖项。核心模块包含配置相关的类。 我想为消费者和生产者构建2个独立的罐子,并使用sbt-assembly创建单独的主类。但是,当我尝试像sbt consumer/assembly那样单独构建它们或者通过运行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)

1 个答案:

答案 0 :(得分:1)

问题出在这一行:

lazy val commonSettings = Seq(
  name := "DemoTest",  // should not be defined at this level!
)

应为root:

定义此name
lazy val root = (project in file("."))
  .aggregate(core, producer, consumer)
  .settings(
    name := "demotest"
  )

子模块将具有"核心","生产者"和"消费者"作为他们自己的名字,除非在他们的设置中没有覆盖name

另请注意,在您的示例.aggregate中不包含core,但确实应该包含var totalList_grps = element.all(by.css('p.group-name-text')); totalList_grps.getText().then(function(text){ console.log('Total list of joined groups : ' + text); });