作为项目构建的一部分,我想将托管依赖项的zip工件解压缩到项目的特定目录中。在开始使用SBT之前,我是通过ANT脚本执行此操作的,该脚本将从maven依赖项中获取zip工件并解压缩。
我的问题是:
"eu.delving" % "sip-creator" % "0.4.6-SNAPSHOT"
但这不会获取zip工件答案 0 :(得分:6)
如果要提取一组托管依赖项,下面的代码应该可以使用。我在sbt 0.12.0中进行了测试,但它也适用于0.11.x。
import sbt._
import Keys._
import Classpaths.managedJars
object TestBuild extends Build {
lazy val jarsToExtract = TaskKey[Seq[File]]("jars-to-extract", "JAR files to be extracted")
lazy val extractJarsTarget = SettingKey[File]("extract-jars-target", "Target directory for extracted JAR files")
lazy val extractJars = TaskKey[Unit]("extract-jars", "Extracts JAR files")
lazy val testSettings = Defaults.defaultSettings ++ Seq(
// define dependencies
libraryDependencies ++= Seq(
"com.newrelic" % "newrelic-api" % "2.2.1"
),
// collect jar files to be extracted from managed jar dependencies
jarsToExtract <<= (classpathTypes, update) map { (ct, up) =>
managedJars(Compile, ct, up) map { _.data } filter { _.getName.startsWith("newrelic-api") }
},
// define the target directory
extractJarsTarget <<= (baseDirectory)(_ / "extracted"),
// task to extract jar files
extractJars <<= (jarsToExtract, extractJarsTarget, streams) map { (jars, target, streams) =>
jars foreach { jar =>
streams.log.info("Extracting " + jar.getName + " to " + target)
IO.unzip(jar, target)
}
},
// make it run before compile
compile in Compile <<= extractJars map { _ => sbt.inc.Analysis.Empty }
)
lazy val test: Project = Project("test", file(".")) settings (testSettings: _*)
}
如果您只是要提取jar文件,则可以将它们添加为非托管依赖项,即。将它们放入/ lib文件夹中。请参阅:https://github.com/harrah/xsbt/wiki/Getting-Started-Library-Dependencies
如果你真的有zip文件(或者想要提取非托管依赖项),你可以更改上面的代码来列出它们:
// list jar files to be extracted
jarsToExtract <<= (baseDirectory) map { dir => Seq(dir / "lib" / "newrelic-api-2.2.1.zip") },
您现在应该能够从sbt手动提取它们,并且应该在编译之前自动提取它们:
> clean
[success] Total time: 0 s, completed Oct 12, 2012 5:39:16 PM
> extract-jars
[info] Extracting newrelic-api-2.2.1.zip to /Users/balagez/Sites/test/extracted
[success] Total time: 0 s, completed Oct 12, 2012 5:39:22 PM
> compile
[info] Extracting newrelic-api-2.2.1.zip to /Users/balagez/Sites/test/extracted
[success] Total time: 0 s, completed Oct 12, 2012 5:39:24 PM
现在,您可以添加新任务或扩展现有的任务,从提取的依赖项中提取zip文件。如果您不需要依赖项的内容,则可以使用task-temporary-directory设置,该设置为您提供sbt可写的临时目录:
// define the target directory
extractJarsTarget <<= taskTemporaryDirectory,