Compile code with generated source by annotation processor

I extended the AbstractProcessor to create an annotation processor to generate a class in Java and it works fine.

The first issue is: the class only is generated for the second time I run ./gradlew build.

The second issue (that my be related to the first one) Gradle won’t compile if I have some code that is dependent of the generated class. It throws:

> Task :compileJava FAILED
/src/main/java/me/fouyer/Main.java:6: error: cannot find symbol

My build.gradle

plugins {
    id 'java'
}

group = 'me.fouyer'
version = '1.0.0'

repositories {
    mavenCentral()
}

dependencies {
    implementation 'com.google.auto.service:auto-service:1.1.1'
    annotationProcessor('me.fouyer:annotation:1.0.0')
}

I already tried:

I created a repository with a minimal, reproducible example where I have the annotation, the annotation processor and a Person class that uses the annotation. GitHub

You have a hen-and-egg problem.
You cannot use the annotation processor within the same build that builds the annotation processor.
That’s like when you try to use a Gradle plugin to build a project that builds that very Gradle plugin.
That just cannot work except if you use a pre-built version from somewhere like in your described procedure.

I don’t think that this is necessarily impossible. I mean when you don’t annotate the processor itself with the annotation, then the codes are in different translation units and you don’t have a hen-egg problem.

Compile annotation.java, compile source.java - remember that it has an unprocessed annotation, compile processor.java - see that it processes the annotation, run processor on source.class, generating generated.java, compile generated.java.

Having the processor in a different project only causes the processor.class to be already compiled and taken from an archive, instead of compiling it.

I’m sorry, I was unclear.

I did not mean within the same project, but within the same compile unit.

If you move the annotations and the annotation processor to a separate project (or even just to a separate source set) in the same build it works of course.

And if the annotations need to be used at runtime, it would indeed be cleaner to have the annotations in one additional project and the annotation processor in another additional project or source set, so that at runtime you can have the annotations available without needing to include the processor.

But in the shown MCVE all of the classes are in the same source set and that is where you have chicken-and-egg which cannot work.