'How to get the Gradle build directory in a Java annotation processor

I am writing a custom annotation processor in Java which needs to create a file.

It seems to me the best location for that file would be in a new folder inside the Gradle's $buildDir.

For a project without modules, the environment property "user.dir" seems to hold a value I could use.

However, that environment property changes if the project has modules and the gradlew build command is executed either from inside or outside a module.

What is the best approach to actually get in Java the Gradle build directory of the module in which the annotation processor is declared ?

P.S.

I do not want to create that file in the "build/generated" folder (this is what processingEnv.getFiler().createSourceFile(..) does).



Solution 1:[1]

You should do it the other way around : choose a location and set it up in your build.gradle

tasks.withType(JavaCompile) {
  options.annotationProcessorGeneratedSourcesDirectory = file("your/custom/path")
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ToYonos