Set project version from task

I want to install a version of the project, which depends on the git branch, in order to have the current major version + the current date in the development version and just a large number in production. Therefore, I completed a task that calculates 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 that returns Task[Boolean] , indicating that it is a branch with a non-master:

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

then I tried to install the computed version on version :

 version := Versioning.projectVersion.value 

But this is forbidden:

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

What is the right way to do this?

+4
source share
1 answer

I'm not sure if you can fix this without changing TaskKey to SettingKey.

However, there is already a plugin that 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 a short version, working in another branch gives an obsolete version.

0
source

Source: https://habr.com/ru/post/1500249/


All Articles