Annotation Processor 'com.company.svc.tools.codegen.AnnotationProcessor' not found, but the processor is clearly in the class path


(Jason) #1

Hello,

This is a project I am migrating from ant. The project includes a class AnnotationProcessor.java, which I need to use to generate source code for the same project. I am invoking JavaCompile like so:

task compileAnnotationProcessor (type: JavaCompile) { //first pass compiles AnnotationProcessor.java and puts it in sourceSets.main.java.outputDir
  source sourceSets.main.java
  classpath = sourceSets.main.compileClasspath
  destinationDir sourceSets.main.java.outputDir
  sourceCompatibility = '1.8'
  targetCompatibility = '1.8'
}
task generateSource (type: JavaCompile) { //second pass, uses the compiled annotation processor to generate source code
  dependsOn 'compileAnnotationProcessor'
  source sourceSets.main.java
  destinationDir sourceSets.main.java.outputDir
  classpath = sourceSets.main.compileClasspath + fileTree(dir: sourceSets.main.java.outputDir)
  options.compilerArgs += [
    "-processor",
    "com.[company].svc.tools.codegen.AnnotationProcessor,com.[company].logging.processor.Processor"
  ]
}

I’ve tried several other methods as well, including several variations of

dependencies {
  annotationProcessor 
}

And still nothing works. I usually return this error:

Execution failed for task ':[company]:generateSource'.
Annotation processor 'com.[company].svc.tools.codegen.AnnotationProcessor' not found

but when I look at my debug output, my -processorpath and -classpath both include …/[projectdir]/build/classes/java/main/com/[company]/svc/tools/codegen/AnnotationProcessor.class or .java.
I suspect the issue may have something with out project not using typical gradle file structure (“src/com/[company]” instead of “src/main/java”), so I have also been trying different sourceSets. Here is what I have:

sourceSets {
  main {
    java {
      srcDirs 'src'
    }
    resources {
      srcDirs 'resources', 'src'
      include '**/*.xml'
      include '**/*.properties'
			include '**/*.vm'
    }
  }
  test {
    java {
      srcDirs 'test'
    }
  }
}

What is the best method to use a local annotation processor? It works in ant, although I clearly don’t understand why :frowning:


(Stefan Oehme) #2

You can’t do circular inputs like that (i.e. the task taking its own output as an input). The compile task will clean up its outputs before it runs, which means it’ll delete the processor.

Instead, either put your processor into a separate project or at the least into a different sourceSet.


(Jason) #3

I would love to reorganize the project, but its a monolithic legacy nightmare. The idea is that sourceSets.main.java.outputDir is populated by the first compilation. The second task then uses it as input, then overwrites it with the generated code.


(Stefan Oehme) #4

You can’t do that, simply put. Just use a different output dir for compiling the processor and exclude its sources from the second compile task.