gradle-incap-helper
Helper library and annotation processor for building incremental annotation processors
Gradle 4.7 comes with some level incremental annotation processing support. Gradle 4.8 goes farther by making it possibly dynamic.
This library and annotation processor helps you generate the META-INF descriptor, and return the appropriate value from your processor's getSupportedOptions() if it's dynamic.
Usage
-
Add the
incaplibrary to your compile-time dependencies, and theincap-processorto your annotation processor path:with Gradle
dependencies { compileOnly("net.ltgt.gradle.incap:incap:${incap.version}") annotationProcessor("net.ltgt.gradle.incap:incap-processor:${incap.version}") }
with Maven
<dependencies> <dependency> <groupId>net.ltgt.gradle.incap</groupId> <artifactId>incap</artifactId> <version>${incap.version}</version> <scope>provided</scope> <optional>true</optional> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <annotationProcessorPaths> <path> <groupId>net.ltgt.gradle.incap</groupId> <artifactId>incap-processor</artifactId> <version>${incap.version}</version> </path> </annotationProcessorPaths> </configuration> </plugin> </plugins> </build>
Note: it's OK to use
compileOnlyin Gradle, or theprovidedscope in Maven, despite the annotation having class retention, because annotation processors aren't libraries that others compile against. -
Annotate your annotation processor with
@IncrementalAnnotationProcessor@IncrementalAnnotationProcessor(ISOLATING) public class MyProcessor extends AbstractProcessor {
-
If the choice of incrementality support is dynamic (i.e. you used the
DYNAMICvalue above), use theIncrementalAnnotationProcessorTypeenumeration'sgetProcessorOption()from yourgetSupportedOptions()to get the appropriate constant.@Override public Set<String> getSupportedOptions() { if (someCondition) { return Collections.singleton(ISOLATING.getProcessorOption()); } else { return Collections.emptySet(); } }
Acknowledgements
This processor works great with @AutoService, which also inspired some of the code here.
Shout-out Gradle, Inc. (and Groupon) who built this feature.