6

I have a custom annotation processor (that extends AbstractProcessor) which adds a properties file to the project based on the annotations. I want this to be run everytime when a compilation is happening. The project is a java project using gradle.

How do I get the annotation processor run during compile time? Should I use some compiler plugin? or should I write a simple gradle task that can invoke this annotation processor and make that task part of the compilation task? (I'm a beginner with gradle)

  • In the META-INF/services, added the entry for javax.annotation.processing.Processor specifying the custom annotation processor class.
ennovation
  • 366
  • 2
  • 9

2 Answers2

3

I know that this question is quite old but since it even got a favor and nobody answered it I want to give at least a little answer for future readers.

For this are multiple ways possible depending on the way your environment is set up.

For example you can use something like this in build.gradle or some other .gradle file that is used by all wanted projects:

compileJava{
    options.fork = false 
    options.forkOptions.executable = 'javac'
    options.compilerArgs.addAll(['-classpath','path/to/your/compiled/processor.jar'])
}

when you use the dependency system you could use this:

dependencies {
  compileClasspath group: 'com.company', name: 'AnnotationProcessor', version: 'your revision' 
  //or this
  compileClasspath 'com.company.AnnotationProcessor:revision'
}

but be sure to have the .Processor file in src/main/resources/META-INF/services for this method. Else you would have to add the compilerArg '-processor','full.package.name.with.class.name' <-- this could be wrong since I never tried that way.

Nico
  • 1,727
  • 1
  • 24
  • 42
0

The only way I could get annotations to work was via setting the -processorpath directly.

compileJava{

    options.compilerArgs.addAll(['-processorpath',"$rootDir/yoursubproject/annotation.jar"])
    options.compilerArgs.addAll(['-Acom.crd.whomever.processors.pass=Production'])
    options.compilerArgs.addAll(['-XprintRounds'])
    options.compilerArgs.addAll(['-XprintProcessorInfo'])
}
sagneta
  • 1,546
  • 18
  • 26