2016-11-17 103 views
1

我有一个根项目,我想用sbt-assembly构建一个胖JAR。它有一个子项目,它依赖于我想为胖JAR忽略的根目录(就像它不存在一样)。我该怎么做呢?在构建胖项目的同时忽略子项目JAR的根项目

基本上我想根项目包好像有从build.sbt没有localMode子项目中尝试的1

尝试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 
) 

回答

1

我想你可以用assembly::fullClasspath设置做到这一点。通过default它被设置为fullClasspath or (fullClasspath in Runtime)。所以,可能是你可以做这样的事情:

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

在没有关于你的具体项目配置的详细信息,我想localMode是要排除的子项目的名称。

UPDATE

有一些问题,在你尝试2 build.sbt: - 你不给你的项目 添加常用设置 - “根”是一个在的,好了,根你的项目目录(即in file(".")) - 如果你明确地定义root项目,另外一个应该依赖于它,而不是RootProject,这仅仅是一个参考的“含蓄”定义根项目

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是否按预期工作?

+0

没有帮助。我在我的错误消息中添加了信息。 – Make42

+0

@ Make42你也应该发布你的'build.sbt'。可能这个问题是可以解决的,只需正确配置你的子项目。 – laughedelic

+0

你是否试图用项目范围调用它?像'root/assembly'。 – laughedelic

0

尝试把

aggregate in (Compile, assembly) := false 
+0

这似乎没有工作。我收到了通常的“重复数据删除”错误。 – Make42

相关问题