Search code examples
javagradleannotationsannotation-processing

annotationProcessors multi module project Java


I recently started learning java processors and tried using them with gradle, when I build the project I can see that the annotation processors jars(other modules) are printed(I added that in the app gradle file, you can see that below), but not executed. I don't know what I'm doing wrong...

I have a multi module project with the following structure

ROOT
|
|-app  # contains main classes
|-annotations
|-processors

the processors project gradle file:

plugins {
    id 'java'
}

group 'org.example'
version '1.0-beta'

repositories {
    mavenCentral()
}

dependencies {
    compileOnly project(":annotations")
    implementation(project(":annotations"))
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.0'
}

test {
    useJUnitPlatform()
}

the app gradle file

plugins {
    id 'java'
}

group 'org.example'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

tasks.withType(JavaCompile) {
    doFirst {
        println "AnnotationProcessorPath for $name is ${options.getAnnotationProcessorPath().getFiles()}"
    }
}
compileJava {
    options.annotationProcessorPath = configurations.annotationProcessor
}

dependencies {
    compileOnly project(":annotations")
    implementation project(":annotations")
    implementation project(":processors")
    annotationProcessor project(":processors")
//    annotationProcessor  "org.example:processors:1.0-beta"
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.0'
}

test {
    useJUnitPlatform()
}

the annotations one is the default one

I have a processor which logs and creates a File

@Override
public boolean process(Set<? extends TypeElement> set, RoundEnvironment roundEnvironment) {
    processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR,"Testa asdfasdf");
 ...

And in my app module I have a class anotated with the @Builder annotation declared in the annotions module, so the Processor should get executed

Output of app gradle file

7:59:42 PM: Executing task 'Car.main()'...

> Task :annotations:compileJava UP-TO-DATE
> Task :annotations:processResources NO-SOURCE
> Task :annotations:classes UP-TO-DATE
> Task :annotations:jar UP-TO-DATE
> Task :processors:compileJava UP-TO-DATE
> Task :processors:processResources UP-TO-DATE
> Task :processors:classes UP-TO-DATE
> Task :processors:jar UP-TO-DATE

> Task :app:compileJava
AnnotationProcessorPath for compileJava is [/annotations/processors/build/libs/processors-1.0-beta.jar, /annotations/annotations/build/libs/annotations-1.0-SNAPSHOT.jar]

> Task :app:processResources UP-TO-DATE
> Task :app:classes

> Task :app:Car.main()


BUILD SUCCESSFUL in 242ms
8 actionable tasks: 2 executed, 6 up-to-date
7:59:42 PM: Task execution finished 'Car.main()'.

Solution

  • After a couple hours it worked by creating the file javax.annotation.processing.Processor under the processors module

    ROOT
    |
    --app  # contains main classes
    --annotations
    --processors
      |--main
         |--java
         |--resources
            |--META-INF
               |--services
                   |--javax.annotation.processing.Processor
      |--test
    

    setting its content to the processor path, in my case com.example.processors.BuilderProcessor