0

I need a way to update a SettingKey[String] in my sbt build each time the compile task executes. A SettingKey is necessary so it can be injected into application via sbt-buildinfo. Creating a command is one way to accomplish this per this question, but I want it to happen when the compile task executes, which can be caused by any number of other tasks. Any ideas how to accomplish this in sbt 0.12?

Community
  • 1
  • 1
joescii
  • 6,495
  • 1
  • 27
  • 32

1 Answers1

0

You cannot use SettingKey[String] as the SettingKeys are only computed once - during the project load, which is specified in the documentation:

For a setting, the value will be computed once at project load time. For a task, the computation will be re-run each time the task is executed.

How about following Build.scala

import sbt._
import Keys._
import sbtbuildinfo.Plugin._

object HelloBuild extends Build {

  val helloSettings = Defaults.defaultSettings ++
    buildInfoSettings ++ Seq (
    sourceGenerators in Compile <+= buildInfo,
    buildInfoKeys := Seq[BuildInfoKey](
      name, 
      version, 
      scalaVersion,
      // Here is the interesting part
      BuildInfoKey.action("buildTime") { 
        System.currentTimeMillis 
      }
    ),
    buildInfoPackage := "org.myorg.myapp"
  )

  lazy val project = Project (
    id = "project",
    base = file ("."),
    settings = helloSettings
  )

}
lpiepiora
  • 13,659
  • 1
  • 35
  • 47