Skip to content

Commit

Permalink
Derive version from git (hamcrest#419)
Browse files Browse the repository at this point in the history
Instead of hard-coding the version into build.gradle, this change derives the version from git tags.

If the HEAD commit has a tag in the format "`vM.N`", the version number `M.N` is used directly. Otherwise, a snapshot version is assumed, and the version becomes `M.(N+1)-SNAPSHOT`.
  • Loading branch information
tumbarumba authored Sep 8, 2024
1 parent 01c776c commit b943810
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ jobs:
experimental: true
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Set up JDK ${{ matrix.java }}
uses: actions/setup-java@v4
with:
Expand Down
12 changes: 11 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,19 @@ plugins {
id "signing"
id "maven-publish"
}
apply from: "gradle/versioning.gradle"

group = "org.hamcrest"
version = "3.1-SNAPSHOT"
version = getMavenVersion()

tasks.register("showVersion") {
group = "Build"
description = "Show the version, as derived from git tags"
doLast {
println("git version: " + getGitVersion())
println("mvn version: " + getMavenVersion())
}
}

subprojects {
apply plugin: 'checkstyle'
Expand Down
25 changes: 25 additions & 0 deletions gradle/versioning.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
ext.getGitVersion = { ->
def stdout = new ByteArrayOutputStream()
exec {
commandLine "git", "describe", "--tags"
standardOutput = stdout
}
return stdout.toString().trim().replaceAll("v", "")
}

ext.getMavenVersion = { ->
def gitVersion = getGitVersion()
if (!gitVersion.contains("-")) {
// We're directly on a tagged commit
return gitVersion
}

def match = gitVersion =~ /^(\d+)\.(\d+)-.*$/
// Make sure we're only using the major.minor version.
assert match : "Unexpected version from git: ${gitVersion}"

// We're not directly on a tagged commit, so increment the minor version and call it a snapshot
def (_, major, minor) = match[0]
def mvnMinor = minor.toInteger() + 1
return "${major}.${mvnMinor}-SNAPSHOT"
}

0 comments on commit b943810

Please sign in to comment.