使用 sbt-assembly 来自单个项目的具有不同外部依赖项的多个可执行 jar 文件
Posted
技术标签:
【中文标题】使用 sbt-assembly 来自单个项目的具有不同外部依赖项的多个可执行 jar 文件【英文标题】:Multiple executable jar files with different external dependencies from a single project with sbt-assembly 【发布时间】:2013-09-07 19:28:39 【问题描述】:我有一个使用 build.scala 文件用 sbt 构建的 scala 项目。我可以使用 sbt-assembly 从该项目生成单个可执行 jar 文件,而不会出现问题。现在我想生成多个可执行的 jar 文件,其中每个文件都包含一组公共的内部和外部基本依赖项以及不同的外部依赖项。
当前版本的 sbt-assembly 是否可行?在 maven 中这很容易,因为可以在 pom 中定义多个配置文件,每个配置文件生成一个单独的 jar,但是在 sbt-assembly 中,您将程序集设置传递给您的项目,我还没有找到一种方法来修改设置以生成多个 jar 文件。
【问题讨论】:
【参考方案1】:Maven 配置文件的概念在 sbt/Ivy 中转换为配置。您可以在配置中定义设置并让 sbt-assembly 生成两个 jar。
您可以通过添加 % "config"
将 libraryDependency 添加到特定配置。但是,这需要项目预先了解配置。
在build.sbt
(需要sbt 0.13.0或以上):
import AssemblyKeys._
val Dispatch10 = config("dispatch10") extend(Compile)
val TestDispatch10 = config("testdispatch10") extend(Test)
val Dispatch11 = config("dispatch11") extend(Compile)
val TestDispatch11 = config("testdispatch11") extend(Test)
val root = project.in(file(".")).
configs(Dispatch10, TestDispatch10, Dispatch11, TestDispatch11).
settings(
name := "helloworld",
organization := "com.eed3si9n",
scalaVersion := "2.10.2",
compile in Test := inc.Analysis.Empty,
compile in Compile := inc.Analysis.Empty,
libraryDependencies ++= Seq(
"net.databinder.dispatch" %% "dispatch-core" % "0.10.0" % "dispatch10,testdispatch10",
"net.databinder.dispatch" %% "dispatch-core" % "0.11.0" % "dispatch11,testdispatch11",
"org.specs2" %% "specs2" % "2.2" % "test",
"com.github.scopt" %% "scopt" % "3.0.0"
)
).
settings(inConfig(Dispatch10)(Classpaths.configSettings ++ Defaults.configTasks ++ baseAssemblySettings ++ Seq(
test := (test in TestDispatch10).value,
test in assembly := test.value,
assemblyDirectory in assembly := cacheDirectory.value / "assembly-dispatch10",
jarName in assembly := name.value + "-assembly-dispatch10_" + version.value + ".jar"
)): _*).
settings(inConfig(TestDispatch10)(Classpaths.configSettings ++ Defaults.configTasks ++ Defaults.testTasks ++ Seq(
internalDependencyClasspath := Seq((classDirectory in Dispatch10).value).classpath
)): _*).
settings(inConfig(Dispatch11)(Classpaths.configSettings ++ Defaults.configTasks ++ baseAssemblySettings ++ Seq(
test := (test in TestDispatch11).value,
test in assembly := test.value,
assemblyDirectory in assembly := cacheDirectory.value / "assembly-dispatch11",
jarName in assembly := name.value + "-assembly-dispatch11_" + version.value + ".jar"
)): _*).
settings(inConfig(TestDispatch11)(Classpaths.configSettings ++ Defaults.configTasks ++ Defaults.testTasks ++ Seq(
internalDependencyClasspath := Seq((classDirectory in Dispatch11).value).classpath
)): _*)
要在配置中运行组装任务,你会说:
> dispatch11:assembly
[info] HelloWorldSpec
[info]
[info] The 'Hello world' string should
[info] + contain 11 characters
[info]
[info] Total for specification HelloWorldSpec
[info] Finished in 17 ms
[info] 1 examples, 0 failure, 0 error
[info] Passed: Total 1, Failed 0, Errors 0, Passed 4
[info] Including: scopt_2.10-3.0.0.jar
[info] Including: slf4j-api-1.6.2.jar
[info] Including: netty-3.6.3.Final.jar
[info] Including: scala-library.jar
[info] Including: async-http-client-1.7.16.jar
[info] Including: dispatch-core_2.10-0.11.0.jar
[warn] Merging 'META-INF/NOTICE.txt' with strategy 'rename'
[warn] Merging 'META-INF/license' with strategy 'rename'
[warn] Merging 'META-INF/LICENSE.txt' with strategy 'rename'
[warn] Merging 'META-INF/MANIFEST.MF' with strategy 'discard'
[warn] Strategy 'discard' was applied to a file
[warn] Strategy 'rename' was applied to 3 files
[info] Checking every *.class/*.jar file's SHA-1.
[info] SHA-1: xxxxxxxxxxxx
[info] Packaging /Users/foo/helloworld/target/scala-2.10/helloworld-assembly-dispatch11_0.1-SNAPSHOT.jar ...
[info] Done packaging.
【讨论】:
必须补充一点,这似乎适用于 2013 年中期的 sbt 0.13.0 和 sbt-assembly 0.9.0,但不适用于当代版本。以上是关于使用 sbt-assembly 来自单个项目的具有不同外部依赖项的多个可执行 jar 文件的主要内容,如果未能解决你的问题,请参考以下文章