3

对于我的Runtime配置,我可以定义一个额外resourceGenerator的如下:

resourceGenerators in Runtime <+= myResourceGeneratingTask

对于该assembly任务,这不起作用:

resourceGenerators in assembly <+= myResourceGeneratingTask

错误信息是:

Reference to undefined setting:

  *:assembly::resourceGenerators from *:assembly::resourceGenerators (...\build.sbt:20)

当我将 'append' 运算符<+=更改为 assignment<<=时,它不再导致任何错误,但不会调用我的资源生成器。

4

1 回答 1

3

请注意,我使用 sbt 0.13.5。它也适用于 Typesafe Activator 1.2.3(因为它基于 sbt 0.13.5)。

assembly是一项任务而不是配置,不提供resourceGenerators设置,因此错误 - undefined setting

但是,您可以在给定配置中设置任务的设置,因此以下内容将正常工作:

resourceGenerators in (Runtime, assembly) += myResourceGeneratingTask.taskValue

请注意,我使用+=taskValue组合这是 0.13 中的首选方法。我还提取了一种方法def makeSomeSources,以便在必要时在其他用例中重用它。请参阅sbt 官方文档中的生成文件:

因为我们要添加任务,而不是执行后的值,所以我们使用taskValue而不是通常的值。应该根据生成的文件是 main ( Compile) 还是 test ( Test) 资源来确定范围。

在您的情况下,它用于配置assembly任务Runtime

build.sbt我用作参考:

import AssemblyKeys._

assemblySettings

def makeSomeSources(f: File): Seq[File] = {
  val content = "Hello"
  IO.write(f, content)
  Seq(f)
}

lazy val myResGenT = taskKey[Seq[File]]("My personal resgen")

myResGenT := {
  val log = streams.value.log
  val f = (resourceManaged in Compile).value / "resgen.xml"
  val fs: Seq[File] = makeSomeSources(f)
  log.info(s"Generating file(s): $fs")
  fs
}

resourceGenerators in Compile += myResGenT.taskValue

resourceGenerators in Runtime += myResGenT.taskValue

resourceGenerators in (Runtime, assembly) += myResGenT.taskValue

执行runtime:assembly给我以下输出:

> runtime:assembly
[info] Generating file(s): List(C:\dev\sandbox\resourceGenerators\target\scala-2.10\resource_managed\main\resgen.xml)
[info] Including: scala-library.jar
[info] Checking every *.class/*.jar file's SHA-1.
[info] Merging files...
[warn] Merging 'META-INF\MANIFEST.MF' with strategy 'discard'
[warn] Strategy 'discard' was applied to a file
[info] SHA-1: 875d4b95f38dc85d14e53bdcd10e442520aeeefd
[info] Packaging C:\dev\sandbox\resourceGenerators\target\scala-2.10\resourcegenerators-assembly-0.1-SNAPSHOT.jar ...
[info] Done packaging.
于 2014-07-07T09:36:21.627 回答