这就是您如何使用纯 SBT 0.13.x 实现您想要的(该示例通常适用于旧版本,但可能您必须使用不同的运算符)。
build.sbt
import Path.flat
libraryDependencies ++= Seq(
"com.fasterxml.jackson.core" % "jackson-core" % "2.3.1",
"com.netflix.rxjava" % "rxjava-core" % "0.16.1"
// maybe more dpendencies
)
packageOptions in (Compile, packageBin) +=
Package.ManifestAttributes("PluginMainClass" -> "com.xxx.xxx.Class")
// this will copy all managed jars to the ./lib in your jar
mappings in (Compile, packageBin) ++= {
val cp = (managedClasspath in Compile).value.files
cp pair flatRebase("/lib")
}
// this will move all your compiled classes to folder ./com.xxx.xxx in your jar
mappings in (Compile, packageBin) ++= {
val compiledClasses = (products in Compile).value ** "*.class"
val classDirectoryBase = (classDirectory in Compile).value
compiledClasses pair rebase(classDirectoryBase, "com.xxx.xxx")
}
然后您可以使用package 来构建jar。在上面的示例中,jar 将如下所示:
Length Date Time Name
--------- ---------- ----- ----
301 2014-05-09 20:13 META-INF/MANIFEST.MF
0 2014-05-09 20:13 /lib/
7126003 2013-09-27 11:44 /lib/scala-library.jar
197986 2013-12-28 02:01 /lib/jackson-core-2.3.1.jar
663553 2014-01-15 08:17 /lib/rxjava-core-0.16.1.jar
--------- -------
7987843 5 files
清单看起来像这样
Manifest-Version: 1.0
Implementation-Vendor: default
Implementation-Title: q-23553321
Implementation-Version: 0.1-SNAPSHOT
Implementation-Vendor-Id: default
PluginMainClass: com.xxx.xxx.Class // MAIN CLASS OF THE PLUGIN
Specification-Vendor: default
Specification-Title: q-23553321
Specification-Version: 0.1-SNAPSHOT
编辑
是否可以在多模块构建中获取已编译的类
尽管? IE 如果我有 /modules/X,Y,Z 并且我想要编译的类
来自工件中“com.xxx.xxx”下捆绑的那些项目?
如果类是唯一的,一般答案是肯定的。为此,您可以使用Scopes。在build.sbt 中,将类的映射替换为以下内容:
// define the configuration axis
val anyProjectsCompile = ScopeFilter(inAnyProject, inConfigurations(Compile))
// we want to have class directory and products together to use rebase in the next step
val classDirectoryProducts = Def.task {
(classDirectory.value, products.value)
}
mappings in (Compile, packageBin) ++= {
classDirectoryProducts.all(anyProjectsCompile).value.flatMap { case (classDir, prods) =>
val compiledClasses = (prods ** "*.class").get
compiledClasses pair rebase(classDir, "com.xxx.xxx")
}
}