我有一项昂贵的任务需要在测试中参考
lazy val exampleSources = TaskKey[Seq[File]]("exampleSources", "for use in tests")
exampleSources := (updateClassifiers in Test).value.select(
artifact = artifactFilter(classifier = "sources")
)
(然后我可以通过exampleSources.value
作为我的分叉测试的参数)
但是,每次我运行测试时,都会调用此任务,并且updateClassifiers
(昂贵)被称为。但我很高兴在第一次调用时缓存该值,然后将其用于会话。
如果不自己编写缓存,有没有办法使用内置的 sbt 对象来做到这一点?
更新:这不起作用。第二次评价有CACHE=true
但解析任务仍在运行。
lazy val infoForTests = TaskKey[Seq[String]]("infoForTests", "for use in tests")
val infoForTestsCache = collection.mutable.Buffer[String]()
infoForTests := {
println("CACHE=" + infoForTestsCache.nonEmpty)
if (infoForTestsCache.isEmpty) {
infoForTestsCache ++= Seq[String](
"-Densime.compile.jars=" + jars((fullClasspath in Compile).value),
"-Densime.test.jars=" + jars((fullClasspath in Test).value),
"-Densime.compile.classDirs=" + classDirs((fullClasspath in Compile).value),
"-Densime.test.classDirs=" + classDirs((fullClasspath in Test).value),
"-Dscala.version=" + scalaVersion.value,
// sorry! this puts a source/javadoc dependency on running our tests
"-Densime.jars.sources=" + (updateClassifiers in Test).value.select(
artifact = artifactFilter(classifier = "sources")
).mkString(",")
)
println("CACHE=" + infoForTestsCache.nonEmpty)
}
infoForTestsCache
}
您可以使用FileFunction.cached http://www.scala-sbt.org/0.13.5/docs/faq.html#how-can-a-task-avoid-redoing-work-if-the-input-files-are-unchanged缓存结果并仅在输入更改时才工作。
只是链接的 sbt 文档中的一个示例:
// define a task that takes some inputs
// and generates files in an output directory
myTask := {
// wraps a function taskImpl in an uptodate check
// taskImpl takes the input files, the output directory,
// generates the output files and returns the set of generated files
val cachedFun = FileFunction.cached(cacheDirectory.value / "my-task") { (in: Set[File]) =>
taskImpl(in, target.value) : Set[File]
}
// Applies the cached function to the inputs files
cachedFun(inputs.value)
}
您可能还想查看我对类似问题的回答:如何列出增量重新编译输出的文件 https://stackoverflow.com/questions/23873031/sbt-plugin-how-to-list-files-output-by-incremental-recompilation/23876075#23876075.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)