‹‹ All posts

Version Scala SBT project using git tags

30 of May, 2017


Default project versioning in Scala SBT is very simple. All you need is to set version parameter in build.sbt file:

version := 0.0.1

Unfortunately it has few near fatal flaws:

  1. You would have to remember to change version number in build file once it’s “ready”;
  2. If you use git (you should), then you would probably have to maintain version number in few places (and try to keep it in sync too);
  3. The command to get the only version number in sbt is quite ugly and verbose (ok, maybe this one is not that fatal):
sbt -no-colors --info "version" | tail -1 | sed 's/\[info\]\ //'

Wouldn’t it be cool if SBT would be able to determine version number from git tags?

Thankfully there is a plugin just for that and it’s called sbt-git.

Installation

is very trivial and is exactly what you would need to do for most of other sbt plugins.

Add plugin dependency at project/plugins.sbt:

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

Enable it in build.sbt:

enablePlugins(GitVersioning)

Git tag version matching rules

  1. It requires tags to start with “v” (by default, this can be configured otherwise);
  2. When there are no matching tags or when current commit is not tagged, then it will use full commit sha like this one: 1ba5dc155ff1ff1436905f20100d60af92b1d4cb;
  3. When there is matching tag, then it will be used as a version (minus the “v”) - 0.0.1 for tag v0.0.1;
  4. When current commit is tagged, but there are changes to tracked files, then it will use “-SNAPSHOT” suffix, like 0.0.1-SNAPSHOT.

There are other version matching rules and example matching customisation in original documentation (for instance, to match any tag starting with a number, not requiring the “v” prefix anymore).

So is it any good?

Sbt-git is great in context of continuous delivery and automation:

  1. You don’t have to change code to tag version number;
  2. With sbt-git the git tags become the only version to maintain - no discrepancies;
  3. Virtually any ci environment supports git, it’s easy to automate and there are tons of examples.

The plugin is well maintained and worked flawlessly in my case - highly recommended.

comments powered by Disqus