Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 :

version=1.0.0

to This:

version=1.0.1

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

like image 465
Sidharth Avatar asked Oct 03 '16 04:10

Sidharth


People also ask

How do I set Java version in build Gradle?

Set the JDK version To use the bundled JDK, do the following: Open your project in Android Studio and select File > Settings... > Build, Execution, Deployment > Build Tools > Gradle (Android Studio > Preferences... > Build, Execution, Deployment > Build Tools > Gradle on a Mac).

Is higher than the Java version used by Gradle?

Gradle can only run on Java version 8 or higher. Gradle still supports compiling, testing, generating Javadoc and executing applications for Java 6 and Java 7. Java 5 and below are not supported.

What is Gradle task?

The work that Gradle can do on a project is defined by one or more tasks. A task represents some atomic piece of work which a build performs. This might be compiling some classes, creating a JAR, generating Javadoc, or publishing some archives to a repository.


1 Answers

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.

A variant:

version='1.0.0'

task incrementVersion<<{
    String minor=version.substring(version.lastIndexOf('.')+1)
    int m=minor.toInteger()+1
    String major=version.substring(0,version.length()-1)
    String s=buildFile.getText().replaceFirst("version='$version'","version='"+major+m+"'")
    buildFile.setText(s)
}
like image 133
Alexiy Avatar answered Oct 28 '22 10:10

Alexiy