1

I have a gradle project and I want to UPDATE its version by building a task so that when ever i run that task it UPDATE the version number in gradle.properties file automatically:

THIS:

ServiceVer=1.0.0 ServiceSnapshot=-SNAPSHOT

Should change like:

ServiceVer=2.0.0 ServiceSnapshot=-SNAPSHOT

what task do i need to write or any basic and easy example(link) to understand? i am new to gradle. i want to have all these changes in a java project not in an android project.

Sidharth
  • 1,402
  • 2
  • 16
  • 37
  • Possible duplicate of [How to autoincrement versionCode in Android Gradle](http://stackoverflow.com/questions/17448565/how-to-autoincrement-versioncode-in-android-gradle) – Bartłomiej Mucha Sep 19 '16 at 11:46
  • somewhat similar but i want to do it for java application not android. and thanks for the link, little bit helpful – Sidharth Sep 19 '16 at 11:51

1 Answers1

0

You could try something like this to update the properties file

task versionPlusPlus() << {
    Properties props = new Properties()
    props.load(new File('gradle.properties').newDataInputStream())
    versionString =  props.getProperty('ServiceVer')
    //  mutate versionString to whatever you want
    props.setProperty('ServiceVer', versionString)
    props.store(propsFile.newWriter(), null)
}

I haven't tested this, but you may want to be careful about calling this task and running a build in the same gradle invocation (for instance gradle versionPlusPlus build, the new versionString may not take effect; run gradle versionPlusPlus and then gradle build separately after gradle.properties is updated.

For the updated version to take effect within the same gradle invocation set project.version=ServiceVar after changing the version string.

RaGe
  • 22,696
  • 11
  • 72
  • 104
  • it does nothing. tried both `props.setProperty` and `System.setProperty` – Sidharth Sep 20 '16 at 05:30
  • I intended for this to be used in subsequent runs before I added the sysprops note. run `gradle versionplusplus` followed by `gradle build`. Does that not work either? – RaGe Sep 20 '16 at 12:19