Accessing the current Subversion Revision Number in Gradle

A common use case for build tools like Ant, Maven or Gradle is to retrieve the current revision number of the project sources in the Version Control System (VCS), in many cases Subversion (SVN). This revision number is then used in the file names of the build artifacts, for example. As mature build tools, Ant and Maven provide plugins to access the current revision number of the SVN working copy. But how about Gradle?

Having recently moved from Ant to Gradle in ~500.000 LOC Java project, I can say that Gradle offers a lot of well-thought-out features that make life easier. However, getting the Subversion revision number of a project workspace is not one of them. It’s remarkably easy to do it yourself, though, as shown in the code snippet below.

Using the buildscript closure you can define dependencies that are only available in your build script (i.e. these dependencies do not spill into the dependencies of your project). Using this way, you can add the dependency to tmatesoft’s SVNKit to your build. SVNKit provides a Java API to Subversion funcionality.

By defining a function (named getSvnRevision() in the snippet above), you can then simply use SVNKit to retrieve the current SVN revision number from your working copy. This function can then be called anywhere in your Gradle build script. In the case of the snippet above, I used it to append the revision number to a standard major/minor/bugfix versioning pattern. This complete version number can then be used in Gradle subprojects.



  1. Mike


    I’m trying to use your instructions but cannot get the tmatesoft part of the import statement to resolve. I’m new to Android Studio and gradle have just migrated from eclipse. Any suggestions please?

    • Tom


      I have no experience with Android Studio, but it sounds as if your Android Studio project has does not have the svnkit library in its classpath. There must be some way to get Android Studio to import the gradle file so that it knows what libraries it needs to load from the web.

      Anyhow, you should check if it work when you call “gradle build” from the console. If it does not work here, you have a different problem that you should fix first.


      • Mike

        Thanks mate, you actually made me think along slightly different lines and it helped.

        I had assumed that the gradle dependencies and the project ones would be different but that would not appear to be the case. Once I realised that, I simply added the jar to the project and voila… I can now use your wonderful solution.

        Thanks mate

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s