# Custom Gradle Task for Incremental Java Annotation Processing

**URL:** <https://discuss.gradle.org/t/custom-gradle-task-for-incremental-java-annotation-processing/39468>\
**Category:** Help/Discuss\
**Created:** [March 30, 2021, 7:07pm UTC](https://discuss.gradle.org/t/custom-gradle-task-for-incremental-java-annotation-processing/39468 "2021-03-30T19:07:31Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![nestedsoftware](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/nestedsoftware/32/8544_2.png) [@nestedsoftware](https://discuss.gradle.org/u/nestedsoftware)\
**Post date:** [March 30, 2021, 7:07pm UTC](https://discuss.gradle.org/t/custom-gradle-task-for-incremental-java-annotation-processing/39468/1 "2021-03-30T19:07:31Z")

</div>

I have my own annotation processor that I would like to use in a custom task rather than the default compileJava task. When I run annotation processing through the default compileJava task, incremental generation “just works”. The setup in build.gradle looks like the following:

```gradle
dependencies {
    annotationProcessor 'com.generators:interfacegenerator:0.0.1'
    compileOnly 'com.generators:interfacegenerator:0.0.1'
}

```

I can update a single source file, and the annotation processing is done only against that file:

```gradle
> Task :compileJava
Caching disabled for task ':compileJava' because:
  Build cache is disabled
Task ':compileJava' is not up-to-date because:
  Input property 'stableSources' file C:\blahblah\sourcegeneration\src\main\java\gradledeps\sourcegeneration\OtherClass.java has changed.
Created classpath snapshot for incremental compilation in 0.0 secs.
Class dependency analysis for incremental compilation took 0.005 secs.
Compiling with JDK Java compiler API.
Incremental compilation of 2 classes completed in 0.186 secs.
:compileJava (Thread[Daemon worker Thread 13,5,main]) completed. Took 0.246 secs.

```

I would like to do the generation only in its own task. The following is my attempt:

```gradle
task generateSources(type: JavaCompile) {
    options.compilerArgs = ['-proc:only']

    source = "src/main/java"
    classpath = compileJava.classpath
    destinationDir = compileJava.destinationDir
    options.annotationProcessorPath = compileJava.options.annotationProcessorPath
}

```

When I run this after changing a single input file, all of the files are re-procoessed.:

```gradle
..\gradlew.bat --info generateSources
> Task :generateSources
Caching disabled for task ':generateSources' because:
  Build cache is disabled
Task ':generateSources' is not up-to-date because:
  Input property 'stableSources' file C:\blahblah\sourcegeneration\src\main\java\gradledeps\sourcegeneration\OtherClass.java has changed.
Full recompilation is required because unable to get source-classes mapping relationship from last compilation. Analysis took 0.0 secs.

```

If anyone could help out with this, I’d greatly appreciate it!

For reference, the code is in this github project: [gradledeps/sourcegeneration at master · nestedsoftware/gradledeps · GitHub](https://github.com/nestedsoftware/gradledeps/tree/master/sourcegeneration)
