diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 4f351556..83fbd7fa 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -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: diff --git a/build.gradle b/build.gradle index 9883067e..0a6791b6 100644 --- a/build.gradle +++ b/build.gradle @@ -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' diff --git a/gradle/versioning.gradle b/gradle/versioning.gradle new file mode 100644 index 00000000..9960e0d1 --- /dev/null +++ b/gradle/versioning.gradle @@ -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" +}