A Gradle plugin for automatically generating versions from git tags and commits.
- Add the plugin to your build.gradle as shown here.
- Tag your repository (anywhere in history) with a tag named like
v1.2
or a semantic version tag likev1.2.3-tag1.tag2
or something in between likev1.2-tag1.tag2
- Remove the
version
property from yourbuild.gradle
file - (Optional) Use
gradle version
or./gradlew version
to see the currentversion
.
When you run gradle, it uses jGit to execute git describe --long
(technically: git.describe().setLong(true).call)
) to create a string containing the nearest tag looking like v1.2
, the number of commits since that tag, and the short sha. The plugin then splits that apart and sets the project version to {major}.{minor}.{count}
or {major}.{minor}.{patch}-{tags}.{count}
or {major}.{minor}.{count}-{tags}
(depending on what your git tag looks like). It also sets the version in the jar manifest (if you are building a JAR):
Implementation-Version
:{major}.{minor}.{count}
or{major}.{minor}.{patch}-{tags}.{count}
or{major}.{minor}.{count}-{tags}
Implementation-Sha
:{sha}
Specification-Version
:{major}.{minor}
If you want more control over how you version, you can directly access the VersionInfo
object via the gradle project: project.ZoltuGitVersioning.major. Inside of your
build.gradle` this might look something like this:
def versionInfo = ZoltuGitVersioning.versionInfo
print "${versionInfo.major}.${versionInfo.minor}.${versionInfo.commitCount}"
Of course, you can do more advanced logic such as squashing the three values into a single number like:
def versionInfo = ZoltuGitVersioning.versionInfo
def versionCode =
versionInfo.major.toInteger() * 10000
+ versionInfo.minor.toInteger() * 100
+ versionInfo.patch.toInteger()