A Gradle plugin for automatically generating versions from git tags and commits.
- Add the plugin to your
build.gradlefile as shown here. - Tag your repository (anywhere in history) with an annotated tag (light tags won't work) named like
v1.2. - Remove the
versionproperty from yourbuild.gradlefile. - (Optional) Use
gradle versionor./gradlew versionto see the currentversion.
If you want more control over how you version, you can directly access the VersionInfo object inside your gradle script.
def versionInfo = ZoltuGitVersioning.versionInfo
print "${versionInfo.major}.${versionInfo.minor}.${versionInfo.commitCount}"Additionally, you may also change how the plugin processes version information by specifying custom processors.
The plugin no longer supports semantic versioning out-of-box. Instead, you may specify a custom provider function which accepts the result of git describe and returns a map of each versioning component like so:
ZoltuGitVersioning {
customDescribeProcessor { describeResults ->
def matcher = (describeResults =~ /(?<major>[0-9]+?)\.(?<minor>[0-9]+?)(?:\-(?<tags>[0-9A-Za-z\.\-]+))?\-(?<commitCount>[0-9]+?)\-g(?<sha>[a-zA-Z0-9]+)/)
matcher.matches()
[
major: matcher.group('major'),
minor: matcher.group('minor'),
commitCount: matcher.group('commitCount'),
sha: matcher.group('sha'),
tags: matcher.group('tags')
]
}
}The default implementation merely assigns the value of VersionInfo.toString() as the project version. If you want to perform your own processing on the version information produced by the plugin, you may do so as follows:
// With input tag '1.0.0-36'
ZoltuGitVersioning {
customVersionToString { versionInfo ->
"${versionInfo.major}.${versionInfo.minor}.${versionInfo.patch}.${versionInfo.commitCount}"
}
}Will produce: '1.0.0.36'.
- Please keep in mind that any calls to
ZoltuGitVersioning.versionInfomust occur only after declaring any custom processors. - If you need to reference
versionin your build.gradle file (outside of a lazily executed closure) then you will need to referenceZoltuGitVersioning.versionInfosomewhere before you referenceversion. This plugin lazily populatesversionand referencingZoltuGitVersioning.versionInfowill trigger that lazy population. If you do nothing, it will be triggered after your build.gradle has finished processing (before any tasks are executed).