title: Gradle custom plugin : add dependency from extension object
body: <p>I'm trying to write a plugin which adds dependencies to <code>project.dependencies</code> according to informations gathered in the plugin extension object. But it seems to be impossible.</p>
  
  <p>Indeed, the data from extension object is only available in a new task or in <code>project.afterEvaluate</code> closure, but dependencies added in those places are ignored. </p>
  
  <hr>
  
  <p>The following code tries to add the dependency in <code>afterEvaluate</code> but the dependency is ignored :</p>
  
  <pre><code>apply plugin: MyPlugin
  
  myplugin {
    version '1.0'
  }
  
  class MyPlugin implements Plugin&lt;Project&gt; {
    void apply(Project project) {
      project.extensions.create('myplugin', MyPluginExtension)
  
      project.afterEvaluate {
         def version = project.myplugin.version
         project.dependencies.add("compile", "org.foo:bar:$version") // --&gt; ignored
      }
    }
  }
  
  class MyPluginExtension {
    def version
  }
  </code></pre>
  
  <hr>
  
  <p>In the following code the dependency injection works but I don't have access to the extension object :</p>
  
  <pre><code>apply plugin: MyPlugin
  
  myplugin {
    version '1.0'
  }
  
  class MyPlugin implements Plugin&lt;Project&gt; {
    void apply(Project project) {
      project.extensions.create('myplugin', MyPluginExtension)    
  
      def version = project.myplugin.version // == null
      project.dependencies.add("compile", "org.foo:bar:$version") // --&gt; fail because $version is null
  
    }
  }
  
  class MyPluginExtension {
    def version
  }
  </code></pre>
  
  <p>Is there a solution ?</p>