All Projects β†’ dwijnand β†’ Sbt Dynver

dwijnand / Sbt Dynver

Licence: apache-2.0
An sbt plugin to dynamically set your version from git

Programming Languages

scala
5932 projects

Projects that are alternatives of or similar to Sbt Dynver

Sbt Dependency Check
SBT Plugin for OWASP DependencyCheck. Monitor your dependencies and report if there are any publicly known vulnerabilities (e.g. CVEs). 🌈
Stars: ✭ 187 (-23.05%)
Mutual labels:  sbt, sbt-plugin
Sbt Tpolecat
scalac options for the enlightened
Stars: ✭ 227 (-6.58%)
Mutual labels:  sbt, sbt-plugin
Sbt Prompt
An SBT plugin for making your SBT prompt more awesome
Stars: ✭ 107 (-55.97%)
Mutual labels:  sbt, sbt-plugin
Sbt Docker Compose
Integrates Docker Compose functionality into sbt
Stars: ✭ 168 (-30.86%)
Mutual labels:  sbt, sbt-plugin
Gatling Sbt Plugin Demo
Showcase of the Gatling Plugin for SBT
Stars: ✭ 137 (-43.62%)
Mutual labels:  sbt, sbt-plugin
Sbt Dependency Graph
sbt plugin to create a dependency graph for your project
Stars: ✭ 1,223 (+403.29%)
Mutual labels:  sbt, sbt-plugin
Sbt S3 Resolver
☁️Amazon S3-based resolver for sbt
Stars: ✭ 112 (-53.91%)
Mutual labels:  sbt, sbt-plugin
Sbt Ignore Play Generated
Configure linters and coverage tools to ignore Play's generated source files.
Stars: ✭ 10 (-95.88%)
Mutual labels:  sbt, sbt-plugin
Stryker4s
Mutation testing for Scala. Work in progress...
Stars: ✭ 118 (-51.44%)
Mutual labels:  sbt, sbt-plugin
Sbt Jacoco
JaCoCo Code Coverage plug-in for sbt.
Stars: ✭ 115 (-52.67%)
Mutual labels:  sbt, sbt-plugin
Sbt Frege
Frege support for sbt
Stars: ✭ 51 (-79.01%)
Mutual labels:  sbt, sbt-plugin
Sbt Crossproject
Cross-platform compilation support for sbt.
Stars: ✭ 176 (-27.57%)
Mutual labels:  sbt, sbt-plugin
Sbt Multi Jvm
Multi-JVM testing in sbt
Stars: ✭ 50 (-79.42%)
Mutual labels:  sbt, sbt-plugin
Flyway Sbt
Flyway SBT plugin
Stars: ✭ 101 (-58.44%)
Mutual labels:  sbt, sbt-plugin
Sbt Play Gulp
Gulp asset pipeline for Play Framework
Stars: ✭ 38 (-84.36%)
Mutual labels:  sbt, sbt-plugin
Sbt Native Packager
sbt Native Packager
Stars: ✭ 1,480 (+509.05%)
Mutual labels:  sbt, sbt-plugin
Sbt Updates
sbt plugin that can check Maven and Ivy repositories for dependency updates
Stars: ✭ 653 (+168.72%)
Mutual labels:  sbt, sbt-plugin
Sbt Docker
Create Docker images directly from sbt
Stars: ✭ 689 (+183.54%)
Mutual labels:  sbt, sbt-plugin
Sbt Unidoc
sbt plugin to create a unified API document across projects
Stars: ✭ 113 (-53.5%)
Mutual labels:  sbt, sbt-plugin
Sbt Protobuf
sbt plugin for compiling protobuf files
Stars: ✭ 163 (-32.92%)
Mutual labels:  sbt, sbt-plugin

sbt-dynver travis-badge gitter-badge release-badge

sbt-dynver is an sbt plugin to dynamically set your version from git.

Inspired by:

Features:

  • Dynamically set your version by looking at the closest tag to the current commit
  • Detect the previous version

Setup

Add this to your sbt build plugins, in either project/plugins.sbt or project/dynver.sbt:

addSbtPlugin("com.dwijnand" % "sbt-dynver" % "x.y.z")

The latest release is: release-badge

Then make sure to NOT set the version setting, otherwise you will override sbt-dynver.

In CI, you may need to run git fetch --unshallow (or, sometimes, git fetch --depth=10000), to avoid situations where a shallow clone will result in the last tag not being fetched. Additionally git fetch --tags if the repo is cloned with --no-tags.

Other than that, as sbt-dynver is an AutoPlugin that is all that is required.

Detail

ThisBuild / version, ThisBuild / isSnapshot and ThisBuild / isVersionStable will be automatically set to:

| tag    | dist | HEAD sha | dirty | version                        | isSnapshot | isVersionStable |
| ------ | ---- | -------- | ----- | ------------------------------ | ---------- | --------------- |
| v1.0.0 | 0    | -        | No    | 1.0.0                          | false      | true            |
| v1.0.0 | 0    | 1234abcd | Yes   | 1.0.0+0-1234abcd+20140707-1030 | true       | false           |
| v1.0.0 | 3    | 1234abcd | No    | 1.0.0+3-1234abcd               | true       | true            |
| v1.0.0 | 3    | 1234abcd | Yes   | 1.0.0+3-1234abcd+20140707-1030 | true       | false           |
| <none> | 3    | 1234abcd | No    | 0.0.0+3-1234abcd               | true       | true            |
| <none> | 3    | 1234abcd | Yes   | 0.0.0+3-1234abcd+20140707-1030 | true       | false           |
| no commits or no git repo at all | HEAD+20140707-1030             | true       | false           |

Where:

  • tag means what is the latest tag (relative to HEAD)
  • dist means the distance of the HEAD commit from the tag
  • dirty refers to whether there are local changes in the git repo

Previous Version Detection

Given the following git history, here's what previousStableVersion returns when at each commit:

*   (tagged: v1.1.0)       --> Some("1.0.0")
*   (untagged)             --> Some("1.0.0")
| * (tagged: v2.1.0)       --> Some("2.0.0")
| * (tagged: v2.0.0)       --> Some("1.0.0")
|/
*   (tagged: v1.0.0)       --> None
*   (untagged)             --> None

Previous version is detected by looking at the closest tag of the parent commit of HEAD.

If the current commit has multiple parents, the first parent will be used. In git, the first parent comes from the branch you merged into (e.g. master in git checkout master && git merge my-feature-branch)

To use this feature with the Migration Manager MiMa sbt plugin, add

mimaPreviousArtifacts := previousStableVersion.value.map(organization.value %% moduleName.value % _).toSet

Tag Requirements

In order to be recognized by sbt-dynver, by default tags must begin with the lowercase letter 'v' followed by a digit.

If you're not seeing what you expect, then either start with this:

git tag -a v0.0.1 -m "Initial version tag for sbt-dynver"

or change the value of ThisBuild / dynverVTagPrefix to remove the requirement for the v-prefix:

ThisBuild / dynverVTagPrefix := false

or, more generally, use ThisBuild / dynverTagPrefix to fully customising tag prefixes, for example:

ThisBuild / dynverTagPrefix := "foo-" // our tags have the format foo-<version>, e.g. foo-1.2.3

Tasks

  • dynver: Returns the dynamic version of your project, inferred from the git metadata
  • dynverCurrentDate: Returns the captured current date. Used for (a) the dirty suffix of dynverGitDescribeOutput and (b) the fallback version (e.g if not a git repo).
  • dynverGitDescribeOutput: Returns the captured git describe out, in a structured form. Useful to define a custom version string.
  • dynverCheckVersion: Checks if version and dynver match
  • dynverAssertVersion: Asserts if version and dynver match

Publishing to Sonatype's snapshots repository (aka "Sonatype mode")

If you're publishing to Sonatype sonashots then enable ThisBuild / dynverSonatypeSnapshots := true to append "-SNAPSHOT" to the version if isSnapshot is true (which it is unless building on a tag with no local changes). This opt-in exists because the Sonatype's snapshots repository requires all versions to end with -SNAPSHOT.

Portable version strings

The default version string format includes + characters, which is an escape character in URL and is not compatible with docker tags. This character can be overridden by setting:

ThisBuild / dynverSeparator := "-"

Custom version string

Sometimes you want to customise the version string. It might be for personal preference, or for compatibility with another tool or spec.

For simple cases you can customise a version by simply post-processing the value of ThisBuild / version (and optionally ThisBuild / dynver), for example by replacing '+' with '-' (emulating the docker support mentioned above):

ThisBuild / version ~= (_.replace('+', '-'))
ThisBuild / dynver  ~= (_.replace('+', '-'))

To completely customise the string format you can use dynverGitDescribeOutput, dynverCurrentDate and sbtdynver.DynVer, like so:

def versionFmt(out: sbtdynver.GitDescribeOutput): String = {
  val dirtySuffix = out.dirtySuffix.dropPlus.mkString("-", "")
  if (out.isCleanAfterTag) out.ref.dropPrefix + dirtySuffix // no commit info if clean after tag
  else out.ref.dropPrefix + out.commitSuffix.mkString("-", "-", "") + dirtySuffix
}

def fallbackVersion(d: java.util.Date): String = s"HEAD-${sbtdynver.DynVer timestamp d}"

inThisBuild(List(
  version := dynverGitDescribeOutput.value.mkVersion(versionFmt, fallbackVersion(dynverCurrentDate.value)),
   dynver := {
     val d = new java.util.Date
     sbtdynver.DynVer.getGitDescribeOutput(d).mkVersion(versionFmt, fallbackVersion(d))
   }
))

Essentially this:

  1. defines how to transform the structured output of git describe's into a string, with versionFmt
  2. defines the fallback version string, with fallbackVersion, and
  3. wires everything back together

Sanity checking the version

As a sanity check, you can stop the build from loading by running a check during sbt's onLoad. For instance, to make sure that the version is derived from tags you can use:

Global / onLoad := (Global / onLoad).value.andThen { s =>
  dynverAssertTagVersion.value
  s
}

This will return an error message like the following:

[error] Failed to derive version from git tags. Maybe run `git fetch --unshallow`? Version: 3-d9489763

Or, using sbt-dynver v1.1.0 to v4.0.0:

Global / onLoad := (Global / onLoad).value.andThen { s =>
  val v = version.value
  if (dynverGitDescribeOutput.value.hasNoTags)
    throw new MessageOnlyException(
      s"Failed to derive version from git tags. Maybe run `git fetch --unshallow`? Version: $v"
    )
  s
}

Dependencies

  • git, on the PATH

FAQ

How do I make previousStableVersion return None for major version branches?

Deciding whether going from one version to another is a "breaking change" is out of scope for this project. If you have binary compatibility check setup using previousStableVersion in CI and want to skip the check for major version branches (e.g. 1.x vs 2.x), see https://github.com/dwijnand/sbt-dynver/issues/70#issuecomment-458620722 for the recommended solution.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].