Coud not find method error, but method does exist

This build.gradle file tells me it could not find the getVersionNumber() method whic is indeed defined. I am baffled.

apply plugin: 'maven-publish'
group = "org.random"
publishing {
  publications {
    docs(MavenPublication) {
      artifact(new File("build.gradle"))
    }
  }
}
task getVersionNumber {
  doLast {
    return 42
  }
}
task setVersionTask() {
  doLast {
    project.version = getVersionNumber()
  }
}

publishToMavenLocal.dependsOn setVersionTask

Execute: gradle publishToMavenLocal. Gives:

$ gradle publishToMavenLocal
:generatePomFileForDocsPublication
:publishDocsPublicationToMavenLocal
:setVersionTask FAILED

FAILURE: Build failed with an exception.

* Where:
Build file '/mnt/homes/home/martin/tmp/gradle-method-not-found/build.gradle' line: 17

* What went wrong:
Execution failed for task ':setVersionTask'.
> Could not find method getVersionNumber() for arguments [] on task ':setVersionTask'.

Using Gradle 2.3.

You’re not defining a method called getVersionTask, you’re defining a task.

What are you trying to do?

Thanks Sterling! Now I get it… a task is not a function nor a method. For the record, when getVersionTask() is written as a method the error is fixed:

apply plugin: 'maven-publish'
group = "org.random"
Integer getVersionNumber() {
  return 42
}
version = getVersionNumber()
publishing {
  publications {
    docs(MavenPublication) {
      artifact(new File("build.gradle"))
    }
  }
}

What clued me in along with your reply was page 86 of Gradle in Action where a version number is read from an external file using a method (not a task as you pointed out).