Question: Within a custom gradle plugin the `project.version` seems to always be `unspecified`. Why and how to retrieve the `project.version` within a custom plugin (class)?

For Example:

```
apply plugin: 'java'
apply plugin: MyPlugin

version = "1.0.0"

println "In build file: $project.version" 

class MyPlugin implements Plugin<Project> {
  public void apply(Project project) {
    project.task('myTask') {
      println "In plugin: $project.version"
    }
  }
}

```

Prints out:

```
%> gradle -q myTask
  In plugin: unspecified
  In build file: 1.0.0

```

Besides the how I really would like to know the why?

Answer: 
You set the property after you call `apply`

If you move

```
version = "1.0.0"

```

To before the line

```
apply plugin: MyPlugin

```

It should work

### Edit

[Following Peter's answer](https://stackoverflow.com/a/13198744/6509), you can see that this works also:

```
apply plugin: 'java'
apply plugin: MyPlugin

version = "1.0.0"

println "In build file: $project.version" 

class MyPlugin implements Plugin<Project> {
  public void apply(Project project) {
    project.task('myTask') {
      project.gradle.projectsEvaluated { 
        println "In plugin: $project.version"
      }
    }
  }
}

```

To delay the evaluation of `$project.version` until evaluation of the build is complete
Check my answer [here](https://stackoverflow.com/questions/27050052/getversion-in-custom-gradle-plugin-is-always-unspecified/29354968#29354968)

You can place this block after the projectEvaluated closure or afterTask closure. I haven't tested just pure standalone, but it may work.

```
AppExtension android = (AppExtension) project.extensions.findByName("android")
String versionName = android.defaultConfig.versionName
String versionCode = android.defaultConfig.versionCode

```