Sidharth Sidharth - 1 year ago 241
Groovy Question

Version increment using gradle task

I want to increase the version number of my project from 1.0.0. to 1.0.1 automatically whenever a new build is made through bash command. I only need to increase path number and others i will be increasing manually during manual build.

i want to change

this :




using gradle task.
any help that how can i do this .
Is there any way to update this using regex or using substring function.

Answer Source

Here is an example task:

version='1.0.0'  //version we need to change

task increment<<{
    def v=buildFile.getText().find(version) //get this build file's text and extract the version value
    String minor=v.substring(v.lastIndexOf('.')+1) //get last digit
    int m=minor.toInteger()+1                      //increment
    String major=v.substring(0,v.length()-1)       //get the beginning
    //println m
    String s=buildFile.getText().replaceFirst("version='$version'","version='"+major+m+"'")
    //println s
    buildFile.setText(s) //replace the build file's text

Run this task several times and you should see the version change.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download