Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle - Executing custom annotation processor during compile time

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.
like image 488
ennovation Avatar asked Mar 11 '16 05:03

ennovation


People also ask

What is annotationprocessor in Gradle?

Annotation processors in Gradle with the annotationProcessor dependency configuration By Tom Gregory Posted on June 26, 2020 Annotation processing is a Java compilation option which has been around since Java 5. It enables the generation of additional files during compilation, such as classes or documentation.

Why doesn’t Gradle recompile ABI-compatible methods in my project?

This means that if project A depends on project B and a class in B is changed in an ABI-compatible way (typically, changing only the body of a method), then Gradle won’t recompile A. Since implementation details matter for annotation processors, they must be declared separately on the annotation processor path.

What is javac command in Gradle?

The javac command runs the Java compiler, and it’s called during any Gradle Java compilation task. Specifically for annotation processing, javac includes this option: Specifies where to find annotation processors. If this option isn’t used, then the class path is searched for processors.

Why do I need to declare a configuration file in Gradle?

If you are using an annotation processor that reads resources (e.g. a configuration file), you need to declare those resources as an input of the compile task. If a resource file is changed, Gradle will trigger a full recompilation.


1 Answers

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.

like image 89
Nico Avatar answered Nov 03 '22 15:11

Nico