# Coud not find method error, but method does exist

**URL:** <https://discuss.gradle.org/t/coud-not-find-method-error-but-method-does-exist/9426>\
**Category:** Help/Discuss\
**Created:** [May 1, 2015, 12:39am UTC](https://discuss.gradle.org/t/coud-not-find-method-error-but-method-does-exist/9426 "2015-05-01T00:39:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Martin\_d\_Anjou](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/martin_d_anjou/32/5248_2.png) [@Martin\_d\_Anjou](https://discuss.gradle.org/u/Martin_d_Anjou)\
**Post date:** [May 1, 2015, 12:39am UTC](https://discuss.gradle.org/t/coud-not-find-method-error-but-method-does-exist/9426/1 "2015-05-01T00:39:21Z")

</div>

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

```gradle
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
$ 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.

---

<div class="post-metadata">

**Author:** ![sterling](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/sterling/32/5395_2.png) [@sterling](https://discuss.gradle.org/u/sterling)\
**Post date:** [May 1, 2015, 1:07am UTC](https://discuss.gradle.org/t/coud-not-find-method-error-but-method-does-exist/9426/2 "2015-05-01T01:07:44Z")

</div>

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

What are you trying to do?

---

<div class="post-metadata">

**Author:** ![Martin\_d\_Anjou](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/martin_d_anjou/32/5248_2.png) [@Martin\_d\_Anjou](https://discuss.gradle.org/u/Martin_d_Anjou)\
**Post date:** [May 1, 2015, 1:19am UTC](https://discuss.gradle.org/t/coud-not-find-method-error-but-method-does-exist/9426/3 "2015-05-01T01:19:51Z")

</div>

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:

```gradle
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).
