Adding own AnnotationProcesser causes Circular dependency

I’ve developed in InteliJ in Java my AnnotationProcessor:

@AutoService(Processor.class)
@SupportedAnnotationTypes("MyAnnotation")
public class MyAnnotationProcessor extends AbstractProcessor {

     @Override
    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { 
      //some code
   }
}

When i’am adding this into my build.gradle like this:

dependencies {

   //some dependencies....
  annotationProcessor group: 'org.projectlombok', name: 'lombok', version: lombokVersion
  implementation group: 'com.google.auto.service', name: 'auto-service', version: '1.1.1'
  annotationProcessor project(':')

}

i can click on elephant-symbol i get:
BUILD SUCCESSFUL in 34s

But, when i’am starting my project, i get a error:

Circular dependency between the following tasks:
:classes
\--- :compileJava
     \--- :jar
          +--- :classes (*)
          \--- :compileJava (*)

How can i fix that?

Of course, that is a chicken-and-egg problem.
To compile the sources with the annotation processor you need the sources compiled, but to compile the sources you need the annotation processor that should be compiled.
You need to split your code, so that you develop the annotation processor in a separate project or separate source set and then depend on that to compile your main sources.

1 Like

It is my first AnnotationProcessor. How can can create separate source set and register it in gradle?

In Kotlin DSL val myAnnotationProcessor by sourceSets.registering.
In Groovy DSL sourceSets { myAnnotationProcessor }.


Btw. I strongly recommend switching to Kotlin DSL. By now it is the default DSL, you immediately get type-safe build scripts, actually helpful error messages if you mess up the syntax, and amazingly better IDE support if you use a good IDE like IntelliJ IDEA or Android Studio.

I have one project with sourceSet: src/main/java
in one of package is my AnnotationProcessor. In the other packages are or java classes for API, BusinessLogic etc.
I have just one build.gradle file, where i handle alle dependencies, plugins etc.
I set sourceSet as you said to: my.package.MyAnnotationProcessor, but now other sources can’t be found.
How should i organize sourceSet with annotationProzessor and other sources, where i’am using annotationProzessor?

I set sourceSet as you said to: my.package.MyAnnotationProcessor, but now other sources can’t be found.

Whatever you mean by that, that is not what I said.

How should i organize sourceSet with annotationProzessor and other sources, where i’am using annotationProzessor?

As I already said, you cannot have both in the same source set as that’s a chicken-and-egg problem. You either need a separate project or separate source set for the annotation processor.

i just tried out to extract my “MyAnnotationProcessor” from “src/main/java” to seperate module “src/processor/java”.
The problem is, that my “MyAnnotationProcessor” uses some json-files from “src/main/resources” and some classes from “src/main/java”.
So i have a dependency from “src/processor/java” to “src/main/java”.
Of cource i can add third module like “/src/common/java”, but it is to much effort just for one class “MyAnnotationProcessor”.
Is there no possibility to have a one module “src/main/java” and to define in gradle taks “compileJava”, that at first compiles “MyAnnotationProcessor” and after that rest of the code.
Maybe there some other ideas.

Of course not, that would also not make much sense.
One source set is given to the compiler which then compiles all the files in there.
You can also not “just compile MyAnnotationProcessor” because as you said, you need classes from the main source set.
So yes, you would probably need to split it to common, annotation processor, and main code.