子项目中的SBT包子项目

时间:2016-10-19 10:38:37

标签: scala sbt

我正在使用sbt 0.13.12,这是我的项目 root |--- common |--- sub1 |--- sub2

build.sbt文件中,我设置sub1取决于common。如果我按sbt project sub1 run运行,那没关系。但是,当我将子项目打包为jar文件时,我运行了sub1.jar文件,错误显示sub1无法找到common类。

我的目的是打包sub1.jarsub2.jar,并在每个jar文件中编译common个代码。

- 更新 -
我试过answer中的建议。运行sbt assembly时出现此问题:

[error] (utils/*:assembly) deduplicate: different file contents found in the following: [error] C:\Users\WindowsUser\.ivy2\cache\javax.inject\javax.inject\jars\javax.inject-1.jar:javax/inject/Inject.class [error] C:\Users\WindowsUser\.ivy2\cache\org.glassfish.hk2.external\javax.inject\jars\javax.inject-2.4.0-b34.jar:javax/inject/Inject.class [error] deduplicate: different file contents found in the following: [error] C:\Users\WindowsUser\.ivy2\cache\javax.inject\javax.inject\jars\javax.inject-1.jar:javax/inject/Named.class [error] C:\Users\WindowsUser\.ivy2\cache\org.glassfish.hk2.external\javax.inject\jars\javax.inject-2.4.0-b34.jar:javax/inject/Named.class [error] deduplicate: different file contents found in the following: [error] C:\Users\WindowsUser\.ivy2\cache\javax.inject\javax.inject\jars\javax.inject-1.jar:javax/inject/Provider.class [error] C:\Users\WindowsUser\.ivy2\cache\org.glassfish.hk2.external\javax.inject\jars\javax.inject-2.4.0-b34.jar:javax/inject/Provider.class [error] deduplicate: different file contents found in the following: [error] C:\Users\WindowsUser\.ivy2\cache\javax.inject\javax.inject\jars\javax.inject-1.jar:javax/inject/Qualifier.class [error] C:\Users\WindowsUser\.ivy2\cache\org.glassfish.hk2.external\javax.inject\jars\javax.inject-2.4.0-b34.jar:javax/inject/Qualifier.class [error] deduplicate: different file contents found in the following: [error] C:\Users\WindowsUser\.ivy2\cache\javax.inject\javax.inject\jars\javax.inject-1.jar:javax/inject/Scope.class [error] C:\Users\WindowsUser\.ivy2\cache\org.glassfish.hk2.external\javax.inject\jars\javax.inject-2.4.0-b34.jar:javax/inject/Scope.class [error] deduplicate: different file contents found in the following: [error] C:\Users\WindowsUser\.ivy2\cache\javax.inject\javax.inject\jars\javax.inject-1.jar:javax/inject/Singleton.class [error] C:\Users\WindowsUser\.ivy2\cache\org.glassfish.hk2.external\javax.inject\jars\javax.inject-2.4.0-b34.jar:javax/inject/Singleton.class [error] deduplicate: different file contents found in the following: [error] C:\Users\WindowsUser\.ivy2\cache\aopalliance\aopalliance\jars\aopalliance-1.0.jar:org/aopalliance/aop/Advice.class [error] C:\Users\WindowsUser\.ivy2\cache\org.glassfish.hk2.external\aopalliance-repackaged\jars\aopalliance-repackaged-2.4.0-b34.jar:org/aopalliance/aop/Advice.class [error] deduplicate: different file contents found in the following: [error] C:\Users\WindowsUser\.ivy2\cache\aopalliance\aopalliance\jars\aopalliance-1.0.jar:org/aopalliance/aop/AspectException.class [error] C:\Users\WindowsUser\.ivy2\cache\org.glassfish.hk2.external\aopalliance-repackaged\jars\aopalliance-repackaged-2.4.0-b34.jar:org/aopalliance/aop/AspectException.class .... (truncated because it's too long) .... [error] deduplicate: different file contents found in the following: [error] C:\Users\WindowsUser\.ivy2\cache\org.apache.hadoop\hadoop-yarn-common\jars\hadoop-yarn-common-2.2.0.jar:org/apache/hadoop/yarn/factories/package-info.class [error] C:\Users\WindowsUser\.ivy2\cache\org.apache.hadoop\hadoop-yarn-api\jars\hadoop-yarn-api-2.2.0.jar:org/apache/hadoop/yarn/factories/package-info.class [error] deduplicate: different file contents found in the following: [error] C:\Users\WindowsUser\.ivy2\cache\org.apache.hadoop\hadoop-yarn-common\jars\hadoop-yarn-common-2.2.0.jar:org/apache/hadoop/yarn/factory/providers/package-info.class [error] C:\Users\WindowsUser\.ivy2\cache\org.apache.hadoop\hadoop-yarn-api\jars\hadoop-yarn-api-2.2.0.jar:org/apache/hadoop/yarn/factory/providers/package-info.class [error] deduplicate: different file contents found in the following: [error] C:\Users\WindowsUser\.ivy2\cache\org.apache.hadoop\hadoop-yarn-common\jars\hadoop-yarn-common-2.2.0.jar:org/apache/hadoop/yarn/util/package-info.class [error] C:\Users\WindowsUser\.ivy2\cache\org.apache.hadoop\hadoop-yarn-api\jars\hadoop-yarn-api-2.2.0.jar:org/apache/hadoop/yarn/util/package-info.class [error] deduplicate: different file contents found in the following: [error] C:\Users\WindowsUser\.ivy2\cache\org.apache.spark\spark-core_2.11\jars\spark-core_2.11-2.0.1.jar:org/apache/spark/unused/UnusedStubClass.class [error] C:\Users\WindowsUser\.ivy2\cache\org.apache.spark\spark-launcher_2.11\jars\spark-launcher_2.11-2.0.1.jar:org/apache/spark/unused/UnusedStubClass.class [error] C:\Users\WindowsUser\.ivy2\cache\org.apache.spark\spark-tags_2.11\jars\spark-tags_2.11-2.0.1.jar:org/apache/spark/unused/UnusedStubClass.class [error] C:\Users\WindowsUser\.ivy2\cache\org.spark-project.spark\unused\jars\unused-1.0.0.jar:org/apache/spark/unused/UnusedStubClass.class [error] C:\Users\WindowsUser\.ivy2\cache\org.apache.spark\spark-network-common_2.11\jars\spark-network-common_2.11-2.0.1.jar:org/apache/spark/unused/UnusedStubClass.class [error] C:\Users\WindowsUser\.ivy2\cache\org.apache.spark\spark-network-shuffle_2.11\jars\spark-network-shuffle_2.11-2.0.1.jar:org/apache/spark/unused/UnusedStubClass.class [error] C:\Users\WindowsUser\.ivy2\cache\org.apache.spark\spark-unsafe_2.11\jars\spark-unsafe_2.11-2.0.1.jar:org/apache/spark/unused/UnusedStubClass.class [error] C:\Users\WindowsUser\.ivy2\cache\org.apache.spark\spark-sql_2.11\jars\spark-sql_2.11-2.0.1.jar:org/apache/spark/unused/UnusedStubClass.class [error] C:\Users\WindowsUser\.ivy2\cache\org.apache.spark\spark-sketch_2.11\jars\spark-sketch_2.11-2.0.1.jar:org/apache/spark/unused/UnusedStubClass.class [error] C:\Users\WindowsUser\.ivy2\cache\org.apache.spark\spark-catalyst_2.11\jars\spark-catalyst_2.11-2.0.1.jar:org/apache/spark/unused/UnusedStubClass.class [error] Total time: 670 s, completed Oct 20, 2016 10:36:31 AM

而且,是的!我搜索并遵循了这些解决方案,但无法解决新问题:

1 个答案:

答案 0 :(得分:0)

使用此插件https://github.com/sbt/sbt-assembly,您可以创建包含所有依赖项的胖jar。

在这种情况下,您可以输入:

sbt
project sub1
assembly

修改

关于重复数据删除错误,您必须为依赖项使用合并策略。 我一直在使用以下方法解决了99%的问题:

lazy val mergeStrategy = Seq(
  assemblyMergeStrategy in assembly := {
    case PathList("javax", "servlet", xs@_*) => MergeStrategy.first
    case PathList("META-INF", "io.netty.versions.properties") => MergeStrategy.last
    case PathList(ps@_*) if ps.last endsWith ".html" => MergeStrategy.first
    case "application.conf" => MergeStrategy.concat
    case "reference.conf" => MergeStrategy.concat
    case m if m.toLowerCase.endsWith("manifest.mf") => MergeStrategy.discard
    case m if m.toLowerCase.matches("meta-inf.*\\.sf$") => MergeStrategy.discard
    case _ => MergeStrategy.first
  }
)

lazy val root = Project(
  id = "root",
  base = file(".")
) aggregate(common, database, server)

lazy val server = (project in file("server"))
  .settings(cdbServerConfig: _*)
  .settings(mergeStrategy: _*)
  .settings(libraryDependencies ++= (commonDependencies ++ akkaDependencies ++ jsonDependencies))
  .dependsOn(database)

lazy val database = (project in file("database"))
  .settings(commonSettings: _*)
  .settings(libraryDependencies ++= (commonDependencies ++ databaseDependencies))
  .dependsOn(common)

lazy val common = (project in file("common"))
  .settings(commonSettings: _*)
  .settings(libraryDependencies ++= commonDependencies)

所以在我的情况下,我想打包服务器项目,这取决于依赖于公共项目的数据库项目。

使用该合并策略对我来说很好。

但正如我所说,99%它有效,但如果没有,我一直在使用以下插件:

https://github.com/jrudolph/sbt-dependency-graph

所以我可以看到依赖树,看看有什么冲突。