Increment Version Numbers in Gradle

How to increment version number in Gradle

Following are a couple of examples how to address the issue

Prerequisite

The gradle version is constructed from gradle properties that are set in the gradle.properties file likewise:

//version Properties
artifactMajorVersion=1.0.0
artifactBuildNumber=2

So that the version is constructed so:

product-$artifactMajorVersion-$artifactBuildNumber.$ext

e.g.

product-1.0.0-2-SNAPSHOT.jar

or

product-1.0.0-2.jar

Following are two tasks that increment the buildNumber - pick one:

Groovy:

task buildnumberplusone « {
  Properties props = new Properties()
  File propsFile = new File(‘gradle.properties’)
  props.load(propsFile.newDataInputStream())
  Integer nextbuildnum = ( ((props.getProperty('artifactBuildNumber')) as BigDecimal) + 1 )
  props.setProperty('artifactBuildNumber', nextbuildnum.toString())
  props.store(propsFile.newWriter(), null)
  props.load(propsFile.newDataInputStream())
}

Ant:

task incrementBuildNumber(group: ‘versioning’, description: ‘Increments the project build number’) « {
  String current = “$artifactBuildNumber”
  //Note that this doesn’t modify project.version’s value
  artifactBuildNumber = artifactBuildNumber.toInteger() + 1
  logger.info “Incrementing build number: $current -> $artifactBuildNumber”
  ant.propertyfile(file: file(‘gradle.properties’)) {
      entry(key: ‘artifactBuildNumber’, type: ‘int’, operation: ‘+’, value: 1)
  }
}
Senior DevOps Engineer

DevOps Group
Thank you for your interest!

We will contact you as soon as possible.

Send us a message

Oops, something went wrong
Please try again or contact us by email at info@tikalk.com