Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Derive version from git #419

Merged
merged 8 commits into from
Sep 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
}