在构建根项目

时间:2016-11-17 15:42:09

标签: sbt sbt-assembly

我有一个根项目,我想用sbt-assembly构建一个胖JAR。它确实有一个子项目,它取决于我想要为胖JAR忽略的root(就好像它不存在)。我该怎么做?

基本上我希望根项目包好像在Try 1中的localMode没有build.sbt子项目。

尝试1

我的build.sbt

import sbt.Keys._

name := "myprojectname"
version := "0.0.1-SNAPSHOT"
scalaVersion in ThisBuild := "2.11.8"

mainClass in(Compile, run) := Some("com.mywebsite.MyExample")
mainClass in(Compile, packageBin) := Some("com.mywebsite.MyExample")
mainClass in assembly := Some("com.mywebsite.MyExample")

libraryDependencies += "org.apache.spark" %% "spark-core" % "2.0.0" % Provided
libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.0" % Test

lazy val localMode = project.
  in(file("localMode")).
  dependsOn(RootProject(file("."))).
  settings(
    name := "myprojectname_local",
    libraryDependencies += "org.apache.spark" %% "spark-core" % "2.0.0" % Compile,
    mainClass in(Compile, run) := Some("com.mywebsite.MyExample"),
    mainClass in(Compile, packageBin) := Some("com.mywebsite.MyExample")
  )

fullClasspath in assembly := { 
  (fullClasspath in assembly).value diff 
  (fullClasspath in assembly in localMode).value 
}

目前我收到错误消息:

[error] (localMode/*:assembly) deduplicate: different file contents found in the following:
[error] ~/.ivy2/cache/org.sonatype.sisu/sisu-guice/jars/sisu-guice-2.1.7-noaop.jar:com/google/inject/AbstractModule.class
[error] ~/.ivy2/cache/com.google.inject/guice/jars/guice-3.0.jar:com/google/inject/AbstractModule.class
[error] deduplicate: different file contents found in the following:
[error] ~/.ivy2/cache/org.sonatype.sisu/sisu-guice/jars/sisu-guice-2.1.7-noaop.jar:com/google/inject/Binder.class
[error] ~/.ivy2/cache/com.google.inject/guice/jars/guice-3.0.jar:com/google/inject/Binder.class
[error] deduplicate: different file contents found in the following:
[error] ~/.ivy2/cache/org.sonatype.sisu/sisu-guice/jars/sisu-guice-2.1.7-noaop.jar:com/google/inject/ConfigurationException.class
[error] ~/.ivy2/cache/com.google.inject/guice/jars/guice-3.0.jar:com/google/inject/ConfigurationException.class

and so on...

如果我命令sbt root/assembly

[error] Expected ID character
[error] Not a valid command: root (similar: reboot, boot, project)
[error] Expected project ID
[error] Expected configuration
[error] Expected ':' (if selecting a configuration)
[error] Expected key
[error] Not a valid key: root (similar: products)
[error] root/assembly
[error]     ^

尝试2

我的第二个build.sbt无法构建:

import sbt.Keys._

lazy val commonSettings = Seq(
  version := "0.0.1-SNAPSHOT",
  scalaVersion in ThisBuild := "2.11.8",
  mainClass in(Compile, run) := Some("com.mywebsite.MyExample"),
  mainClass in(Compile, packageBin) := Some("com.mywebsite.MyExample"),
  libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.0" % Test
)

lazy val root = project.
  in(file("root")).
  dependsOn(RootProject(file("."))).
  settings(
    name := "myprojectname",
    libraryDependencies += "org.apache.spark" %% "spark-core" % "2.0.0" % Provided,
    mainClass in assembly := Some("com.mywebsite.MyExample")
  )

lazy val localMode = project.
  in(file("localMode")).
  dependsOn(RootProject(file("."))).
  settings(
    name := "myprojectname_local",
    libraryDependencies += "org.apache.spark" %% "spark-core" % "2.0.0" % Compile
  )

2 个答案:

答案 0 :(得分:1)

我认为您可以使用assembly::fullClasspath设置来完成此操作。到default时,它设置为fullClasspath or (fullClasspath in Runtime)。所以你可能会做这样的事情:

fullClasspath in assembly := { 
  (fullClasspath in assembly).value diff 
  (fullClasspath in assembly in localMode).value 
}

如果没有关于特定项目配置的详细信息,我猜localMode是您要排除的子项目的名称。

<强>更新

试用版2中的build.sbt存在一些问题:   - 您不会为项目添加常用设置   - &#34; root&#34;是你的项目目录中的那个(即in file("."))   - 如果你明确定义root项目,另一个应该依赖它,而不是RootProject,这只是一种方式来引用&#34;隐含地&#34;定义的根项目

import sbt.Keys._

lazy val commonSettings = Seq(
  version := "0.0.1-SNAPSHOT",
  scalaVersion in ThisBuild := "2.11.8",
  mainClass in(Compile, run) := Some("com.mywebsite.MyExample"),
  mainClass in(Compile, packageBin) := Some("com.mywebsite.MyExample"),
  libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.0" % Test
)

lazy val root = project.in(file(".")).
  settings(commonSettings: _*).
  settings(
    name := "myprojectname",
    libraryDependencies += "org.apache.spark" %% "spark-core" % "2.0.0" % Provided,
    mainClass in assembly := Some("com.mywebsite.MyExample")
  )

lazy val localMode = project. // by default the name of the project val is the name of its base directory
  dependsOn(root).
  settings(commonSettings: _*).
  settings(
    name := "myprojectname_local",
    libraryDependencies += "org.apache.spark" %% "spark-core" % "2.0.0" % Compile
  )

检查Multi-project builds上的sbt文档。关于root项目的问题,我们有一个名为Default root project的章节。现在有了这些修复,root/assembly是否按预期工作?

答案 1 :(得分:0)

尝试放

aggregate in (Compile, assembly) := false