Gradle local variables

I had to automate version computation in Gradle. The version was supposed to have a base version, snapshot qualifier and a timestamp. My mistake was to write it this way:

def baseVersion = '1.0.0'
version = computeVersion()

def computeVersion() {
    return baseVersion + '-SNAPSHOT-' + (new Date().format('yyyyMMddHHmmss')))
}

That did not work... The error was "Could not find property 'baseVersion'". I was wondering why and checked the Gradle documentation:

"Local variables are declared with the def keyword. They are only visible in the scope where they have been declared. Local variables are a feature of the underlying Groovy language".

So I checked the Groovy documentation:

"When you define a variable in a script it is always local. But methods are not part of that scope. So defining a method using different variables as if they were attributes and then defining these variables normally in the script leads to problems".

Lovely! One can use local variables inside tasks, but not inside methods in the script.

Fortunately Gradle has a way to solve this. I added baseVersion into gradle.properties file. It is not a local variable anymore, it is a property. But it works.