How do I add a code generation plugin into the lifeycle?

I have a code generation task which processes IDL into java source. I would like this to run automatically before the javaCompile task. In addition, I have an extension defined, which the task needs. I have not found a single example that does all of this, so I have attempted to put something together from a number, but it doesn’t work. Either the task doesn’t get run, or it winds up with a null extension, even though the appropriate calls are in the build.gradle file that uses the plugin - and I know it is being processed, as the processing failed when I had neglected to define some methods. Here is what I have currently. What am I doing wrong?

package mycompany.build

import mycompany.build.idlj.GenerationOption import mycompany.build.idlj.IdljExtension import mycompany.build.idlj.PackagePrefix import org.gradle.api.Plugin import org.gradle.api.Project

class IdljPlugin implements Plugin {

void apply(Project target) {

def packagePrefixes = target.container(PackagePrefix)

def configurationOptions = target.container(GenerationOption)

target.extensions.create(“idlj”, IdljExtension, packagePrefixes, configurationOptions)

target.afterEvaluate( {

target.tasks.withType(IdljTask).whenTaskAdded { theTask ->

target.compileJava.dependsOn(theTask)

def extension = target.extensions.findByName(“idlj”)

theTask.setExtension(extension == null ? IdljExtension.NULL : extension)

}

})

} }