Question

I want to set project version which depends on the git branch, to have a current a major version + current date in development version and just major number in production. So i've made a task which computes the version i need:

val projectVersion = taskKey[String]("Compute project version")
projectVersion := {
  val v = version.value // get Major version number
  val date = new SimpleDateFormat("yyyyMMdd").format(new Date)
  if (isDev.value) v + "-" + date 
  else v
}

isDev is another task which return Task[Boolean] indicating wether this is a non master branch:

branch := Process("git rev-parse --abbrev-ref HEAD").lines.headOption
isDev  := branch.value != "master"

then i tried to set computed version to version key:

version      := Versioning.projectVersion.value

But it is forbidden:

BuildSettings.scala:15: A setting cannot depend on a task

What's the right way to do this?

Was it helpful?

Solution

I am not sure you can fix this without changing your TaskKey to a SettingKey.

However there is already a plugin which gives you most of what you need: sbt-git

I tried the following: In project/plugins.sbt:

addSbtPlugin("com.typesafe.sbt" % "sbt-git" % "0.6.2")

Then build.sbt looks like this:

name := "Foo"

git.baseVersion := "1.0"

version := {
  val branch = git.gitCurrentBranch.value
  val isDev  = branch != "master"
  val v      = git.baseVersion.value
  val date   = new java.text.SimpleDateFormat("yyyyMMdd").format(new java.util.Date)
  if (isDev) v + "-" + date
  else v
}

Running sbt version in master gives the short version, running in other branch gives dated version.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top