To register a service provider in a Java 11 the following "provides" module-info entry can be used:
module com.mycompany.mylib {
provides ServiceInterface with ServiceImpl;
}
With annotation processors this doesn't seem to work, however. (The special thing with annotation processors is that it's the compiler which should pick up the implementation during compile time rather than the application during runtime.)
To test this out, I've created some SSCCEs on GitHub.
There are 2 modules:
java11-sample-annotation-processor: provides the@Fooannotation and theFooApplicationProcessorjava11-sample-lib: provides 2 sample classes, both using the@Fooannotation
Expected output: in the java11-sample-lib JAR there should be a foo.txt file
The version in the master branch is the one that works:
- Java 11
- no
module-infoused - annotation processor registration in
src/main/resources/META-INF/services/javax.annotation.processing.Processor
In the following versions the foo.txt file is not generated:
In the registration-in-module-info branch I've changed the 2 libs to Java 11 modules with module-info. The annotation processor is here registered in the module-info:
import com.github.puce77.java11annotationprocessortest.annotation.impl.FooApplicationProcessor;
module com.github.puce77.java11annotationprocessortest.annotation {
exports com.github.puce77.java11annotationprocessortest.annotation;
provides javax.annotation.processing.Processor with FooApplicationProcessor;
requires java.compiler;
}
The registration-in-module-info-moduleAndPkg branch is a slight variation, where I specified the target module and target package (hardcoded) rather than the root package.
In the registration-meta-inf branch still both have a module-info but the annotation processor is registered in src/main/resources/META-INF/services/javax.annotation.processing.Processor rather than in the module-info (in case the compiler can not work with provides statements in the module-info!?)
In the only-one-module branch I've removed the module-info from the java11-sample-annotation-processor again. The annotation processor is registered in src/main/resources/META-INF/services/javax.annotation.processing.Processor.
So my question: how can I configure an annotation processor when working with Java 11 modules?