Extension values are not available in task created by custom plugin

(m m) #1

Hi, I have a custom plugin which I took from the sample in the gradle distribution.

package org.gradle
  import org.gradle.api.Project
import org.gradle.api.Plugin
  class GreetingPlugin implements Plugin<Project> {
 void apply(Project target) {
  target.task('helloInsideGreeting', type: GreetingTask)
  println ' creating new dependency '
  target.extensions.add("schemaObjectName", GreetingPluginExtension);
    target.task('createBinding', type:JavaExec)
   classpath target.configurations.bindGen
     classpath target.sourceSets.main.output.classesDir
     main = 'org.jibx.binding.generator.BindGen'
     println ' what r these values '
        println ' target.sourceSets.main.output.classesDir ' + target.sourceSets.main.output.classesDir
   println ' target.projectDir ' + target.projectDir
   println ' target.schemaObjectName.className ' + target.schemaObjectName.className
        args "-w" , "-o" , "-t", "${target.projectDir}/src/main/reosurces/bindgen",
  class GreetingPluginExtension {
 //String className = "my.cool.app.Employee"
 String className

In the above plugin I am dynamically creating an extension and using the values of the extension in the task .

In the consumer , the build.gradle is as below

apply plugin: 'java'
apply plugin: 'greeting'
//schemaObjectName.className = "my.cool.app.Employee"
{ className = "my.cool.app.Employee" }
dependencies { compile project(':my-lib') }

The value I set in the build.gradle is not reflecting within the task created by the plugin.

I tried the steps to create dynamic extension from the gradle document and it worked


The difference here is that the plugin is a standalone project. Let me know If I am doing something wrong.

(Peter Niederwieser) #2

Plugins must defer all access to the build model until at least the end of the configuration phase. (A plugin executes when it’s applied in the build script. At this point, extension properties typically haven’t been set yet.) One way to do so is to wrap all affected code with ‘gradle.projectsEvaluated { … }’. As this is a common question, you should find more information on the forums and on Stack Overflow.

(m m) #3

Thanks for the explanation Peter.

It worked .