I have an expensive task that I need to reference in my tests
lazy val exampleSources = TaskKey[Seq[File]]("exampleSources", "for use in tests")
exampleSources := (updateClassifiers in Test).value.select(
artifact = artifactFilter(classifier = "sources")
)
(and then I can pass exampleSources.value
as a parameter to my forked tests)
However, every time I run a test, this task is called, and updateClassifiers
(expensive) is called. But I'm happy caching the value on first call and then using that for the session.
Without writing the cache myself, is there any way to do this using built-in sbt objects?
UPDATE: this doesn't work. Second evaluation has CACHE=true
but the resolution tasks still run.
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
}