A note about versioning:
Judging from your gradle snippet version = '0.0.1'
you formally align to the major
.minor
.patch
versioning pattern. With major
versions representing breaking changes, minor
versions bringing new features and patch
versions bringing bug fixes.
I doubt, that it is ensured that every publishing either contains major
, minor
or patch
changes exclusively. A steady increasing of one of these version digits may not be best practice nor offers clarity of the versioning.
Instead, I see two approaches that may suite your CI (Continues Integration) setup:
- If you publish to a
maven
repository, you can use add a -SNAPSHOT
suffix to you versioning for non-release builds. The maven-publish
plugin will automatically add a build timestamp before uploading to the maven repository. The maven repository then manages to always serve the latest SNAPSHOT
bild when the major
.minor
.path
-SNAPSHOT
artefact is requested.
- If you have a centralized CI tool (e.g. jenkins, teamcity, ...) and just want to dinstinguish different builds of the same version, you can add the
build number
as suffix to the version. Most of the CI tools provide the build number as environment variable or other type of property. If building and publishing takes place locally, you have to manage the build number on your own. May use an environment variable
or a gradle project property
. Register a new task
to your build.gradle
, get the current build number
, increment it before publishing and update the build number
origin to the new value.
gradle example (not tested)
// may use ext.publishVersion in case of value update issues
def publishVersion = project.version
publishing {
publications {
myArtifact(MavenPublication) {
artifactId = artifactBaseName
version = publishVersion
}
}
}
// example with gradle.properties file. Same approach (read, update) for env. variables
tasks.register("IncreaseBuildNumber") {
// buildNumber property is stored in 'gradle.properties'
newbuildNumber = project.property["buildNumber"] as Integer
newbuildNumber = newbuildNumber + 1
publishVersion = publishVersion + ".${newbuildNumber}"
// update 'gradle.properties' file
var propertiesFile = new File("${project.rootDir}/gradle.properties")
doLast {
var updatedContent = propertiesFile.getText('UTF-8').replace(
"buildNumber=${project.version}",
"buildNumber=${newbuildNumber }"
)
delete propertiesFile
new File(propertiesFile.toString()).write(updatedContent, 'UTF-8')
}
}
tasks.named('publish').configure {
dependsOn tasks.named('IncreaseBuildNumber')
}