开发任务以将托管依赖项与SBT中的另一个目录同步?

时间:2014-03-13 19:51:56

标签: sbt

我正在尝试编写一个检索依赖项jar的任务,并将它们放在项目根目录的单个目录中。这是我到目前为止所做的:

retrieveManaged := true

val libDir = settingKey[File]("the directory to retrieve dependency libraries to")

lazy val getLibs = taskKey[Unit]("retrieves all dependency libraries to the libDir")

libDir := baseDirectory.value / "libs"

getLibs := {
  val a = update.value
  sbt.IO.delete(libDir.value)
  for (src <- (managedDirectory.value ** "*.jar").get) {
    sbt.IO.copyFile(src, libDir.value / src.getName, true)
  }
}

我使用retrieveManaged := true以便sbt将依赖项复制到managedDirectory。然后我将这些库复制到我定义的目录中。我的问题是:

  1. 我希望我的任务依赖于update任务,以确保首先将依赖项复制到managedDirectory。如果我理解How can I call another task from my SBT task?,我应该可以通过调用update.value来完成此操作。但这似乎不起作用。

  2. 我不想复制文件,而是想“同步”。这意味着只应复制新添加的文件,并且应删除任何不再存在的文件。我怎么能这样做?

  3. 更新

    感谢Jacek的建议,我能够提出以下解决方案,解决了#1问题。我仍然需要弄清楚如何做#2(同步而不是复制)。

    getLibs := {
      sbt.IO.delete(libDir.value)
      val depFiles = update.value.matching((s: String) => Set("compile", "runtime") contains s)
      for (depFile <- depFiles) {
        sbt.IO.copyFile(depFile, libDir.value / depFile.getName, true)
      }
    }
    

1 个答案:

答案 0 :(得分:0)

我认为答案是真的使用update任务的值并解析其结果。这将解决问题1和2.如果没有,请将症状附加到您的问题。

所有val a = update.value执行update任务并将其值分配给a。由于您根本不使用a,因此会丢失update给您的所有先前信息。要查看它在shell中执行show update的内容。

[triggeredby]> help update
Resolves and optionally retrieves dependencies, producing a report.

[triggeredby]> show update
[info] Updating {file:/Users/jacek/sandbox/so/triggeredby/}triggeredby...
[info] Resolving org.fusesource.jansi#jansi;1.4 ...
[info] Done updating.
[info] Update report:
[info]  Resolve time: 2152 ms, Download time: 114 ms, Download size: 0 bytes
[info]  compile:
[info]      org.scala-lang:scala-library:2.10.3 (): (Artifact(scala-library,jar,jar,None,List(),None,Map()),/Users/jacek/.sbt/boot/scala-2.10.3/lib/scala-library.jar)
[info]  runtime:
[info]      org.scala-lang:scala-library:2.10.3 (): (Artifact(scala-library,jar,jar,None,List(),None,Map()),/Users/jacek/.sbt/boot/scala-2.10.3/lib/scala-library.jar)
[info]  test:
[info]      org.scala-lang:scala-library:2.10.3 (): (Artifact(scala-library,jar,jar,None,List(),None,Map()),/Users/jacek/.sbt/boot/scala-2.10.3/lib/scala-library.jar)
[info]  provided:
[info]  optional:
[info]  compile-internal:
[info]      org.scala-lang:scala-library:2.10.3 (): (Artifact(scala-library,jar,jar,None,List(),None,Map()),/Users/jacek/.sbt/boot/scala-2.10.3/lib/scala-library.jar)
[info]  runtime-internal:
[info]      org.scala-lang:scala-library:2.10.3 (): (Artifact(scala-library,jar,jar,None,List(),None,Map()),/Users/jacek/.sbt/boot/scala-2.10.3/lib/scala-library.jar)
[info]  test-internal:
[info]      org.scala-lang:scala-library:2.10.3 (): (Artifact(scala-library,jar,jar,None,List(),None,Map()),/Users/jacek/.sbt/boot/scala-2.10.3/lib/scala-library.jar)
[info]  plugin:
[info]  sources:
[info]  docs:
[info]  pom:
[info]  scala-tool:
[info]      org.scala-lang:scala-compiler:2.10.3: (Artifact(scala-compiler,jar,jar,None,List(),None,Map()),/Users/jacek/.sbt/boot/scala-2.10.3/lib/scala-compiler.jar)
[info]      org.scala-lang:scala-library:2.10.3 (): (Artifact(scala-library,jar,jar,None,List(),None,Map()),/Users/jacek/.sbt/boot/scala-2.10.3/lib/scala-library.jar)
[info]      org.scala-lang:scala-reflect:2.10.3 (): (Artifact(scala-reflect,jar,jar,None,List(),None,Map()),/Users/jacek/.sbt/boot/scala-2.10.3/lib/scala-reflect.jar)
[info]      org.scala-lang:jline:2.10.3: (Artifact(jline,jar,jar,None,List(),None,Map()),/Users/jacek/.sbt/boot/scala-2.10.3/lib/jline.jar)
[info]      org.fusesource.jansi:jansi:1.4: (Artifact(jansi,jar,jar,None,ArraySeq(master),None,Map()),/Users/jacek/.ivy2/cache/org.fusesource.jansi/jansi/jars/jansi-1.4.jar)
[success] Total time: 3 s, completed Mar 14, 2014 12:16:26 AM

我认为它几乎是您所需要的 - 使用返回值来满足您的需求。