Hello, I have a question regarding gradle and annotation processors.
javac documentation says that if you don’t specify -processorpath then --class-path also will be used to search for annotation processors.
If I undestand it correctly, javac is searching for file META-INF/services/javax.annotation.processing.Processor where you specify processor class
--class-path path, -classpath path, or -cp path
Specifies where to find user class files and annotation processors. This class path overrides the user class path in the CLASSPATH environment variable.
If --class-path, -classpath, or -cp are not specified, then the user class path is the value of the CLASSPATH environment variable, if that is set, or else the current directory.
If not compiling code for modules, if the --source-path or -sourcepath` option is not specified, then the user class path is also searched for source files.
If the -processorpath option is not specified, then the class path is also searched for annotation processors.
I tried to create annotation processor (which just prints in console) by hand with javac and it was working good without specifying -processorpath just to check if javac documentation don’t lie.
javac com/annotationprocessor/BuilderProcessor.java com/annotationprocessor/BuilderProperty.java
javac --class-path "./resources:." com/annotationuser/Person.java com/annotationuser/PersonApp.java
After second command I got “Hello World” in console.
But when I use gradle as a build tool, my annotation processor works only when I use annotationProcessor dependency configuration. If I use compileOnly or implementation configuration, annotation processor just don’t work.
But I don’t understand why, because javac documentation says that if there are no -processpath
then -classpath will be used for searching for annotation processors
Here is example. This is not my project. I got this link by watching this video
link
This project uses mapstruct and mapstruct-processor dependencies.
And for mapstruct-processor dependency this project uses annotationProcessor configuration.
But if you search in mvnrepository there is this:
// https://mvnrepository.com/artifact/org.mapstruct/mapstruct-processor
implementation group: 'org.mapstruct', name: 'mapstruct-processor', version: '1.5.2.Final'
https://mvnrepository.com/artifact/org.mapstruct/mapstruct-processor/1.5.2.Final
Or if you search in maven central repository
implementation 'org.mapstruct:mapstruct-processor:1.5.2.Final'
But I don’t understand. As I understand this correctly, annotation processing will not be working if you use implementation. So I don’t understand why in two maven repositories there is this copy-paste coordinates with ‘implementation’, but not ‘annotationProcessor’
Also I don’t understand why ‘implementation’/‘complileOnly’ is not working. Javac documentation says that if you don’t specify -processorpath then annotation processors will be used from --class-path.
As I understand ‘annotationProcessor’ configuration in gradle adds dependency to -processorpath.
But I don’t understand, if I use ‘implementation’ which adds dependency to compilation class-path why this is not works?
Thank you for your help!