# Automatic Versioning

**URL:** <https://discuss.gradle.org/t/automatic-versioning/20060>\
**Category:** Help/Discuss\
**Created:** [October 18, 2016, 6:00pm UTC](https://discuss.gradle.org/t/automatic-versioning/20060 "2016-10-18T18:00:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![beepboop](https://avatars.discourse-cdn.com/v4/letter/b/ecccb3/32.png) [@beepboop](https://discuss.gradle.org/u/beepboop)\
**Post date:** [October 18, 2016, 6:00pm UTC](https://discuss.gradle.org/t/automatic-versioning/20060/1 "2016-10-18T18:00:21Z")

</div>

Hi all,

I’m quite new to Gradle and I’m trying to write a task which will read my version number from a file, increment it, and write it to the same file (not sure if this is the best approach, I’m happy to listen to advice on this too).

```
def versionFile = new File("src/main/java/version.txt")
        if(versionFile.canRead()) {
            String versionNumber = new File("src/main/java/version.txt").text
            def (major, minor, patch) = versionNumber.tokenize('.')
            println major + "|" + minor + "|" + patch
            def temp = major.toInteger() + 1
            versionNumber = "${temp}.${minor}.${patch}"
            println versionNumber
        }
        else {
           throw new GradleException("Can not read file.")
        }

```

That’s what I have so far but I haven’t had any luck putting the versionNumber back in.

Edit:So I also found the \<\<  
so for example just after "println versionNumber"  
putting versionFile \<\< versionNumber will append the file but I need something which will OVERWRITE the file.

Thanks for reading!

---

<div class="post-metadata">

**Author:** ![Chris\_Dore](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/chris_dore/32/7592_2.png) [@Chris\_Dore](https://discuss.gradle.org/u/Chris_Dore)\
**Post date:** [October 19, 2016, 1:17pm UTC](https://discuss.gradle.org/t/automatic-versioning/20060/2 "2016-10-19T13:17:01Z")

</div>

Here’s an example:

```
task incVersion {
    ext {
        versionFile = file( 'src/main/java/version.txt' )
    }
    doLast {
        if( versionFile.canRead() ) {
            String versionNumber
            // Assuming version is only ever on the first line.
            // (did this to make sure blank lines aren't accumulated, could strip them some other way if you want)
            versionFile.withReader { reader ->
                versionNumber = reader.readLine()
            }
            def (major, minor, patch) = versionNumber.tokenize('.')
            println major + "|" + minor + "|" + patch
            def temp = major.toInteger() + 1
            versionNumber = "${temp}.${minor}.${patch}"
            println versionNumber
            versionFile.withWriter { writer ->
                writer.println versionNumber
            }
        } else {
           throw new GradleException( 'Can not read file.' )
        }
    }
}

```

Some references:  
[https://docs.gradle.org/current/dsl/org.gradle.api.Project.html#org.gradle.api.Project:file(java.lang.Object)](https://docs.gradle.org/current/dsl/org.gradle.api.Project.html#org.gradle.api.Project:file(java.lang.Object))  
[http://docs.groovy-lang.org/latest/html/groovy-jdk/java/io/File.html#withReader(groovy.lang.Closure)](http://docs.groovy-lang.org/latest/html/groovy-jdk/java/io/File.html#withReader(groovy.lang.Closure))  
[http://docs.groovy-lang.org/latest/html/groovy-jdk/java/io/File.html#withWriter(groovy.lang.Closure)](http://docs.groovy-lang.org/latest/html/groovy-jdk/java/io/File.html#withWriter(groovy.lang.Closure))

---

<div class="post-metadata">

**Author:** ![beepboop](https://avatars.discourse-cdn.com/v4/letter/b/ecccb3/32.png) [@beepboop](https://discuss.gradle.org/u/beepboop)\
**Post date:** [October 19, 2016, 2:04pm UTC](https://discuss.gradle.org/t/automatic-versioning/20060/3 "2016-10-19T14:04:25Z")

</div>

Magnificent, this is exactly what I was hoping for.

Thanks Chris!
