How to read a file from src / main / resources using annotation processor?

I have a simple annotation processor that should read a configuration file from the same project as annotated classes. Structure example:

- myproject 
  - src
    - main
      - java
        - my.package.SourceFile
      - resources
        - config.json

In the annotation handler, I am trying to read a file:

FileObject resource = processingEnv.getFiler().getResource(StandardLocation.SOURCE_PATH, "", "config.json");

but he throws it away FileNotFoundException. I also tried other ways, such as ../resources/config.json, (which produces Invalid relative name: ../resources/config.json). And I tried to put the configuration file in src/main/java(and even src/main/java/my/package) instead, which I don't like, but it still throws FileNotFoundException.

That would already help if I could filer.getResource()tell me where he looks. To find out, I tried to create a file:

filer.createResource(StandardLocation.SOURCE_OUTPUT, "", "dummy");

myproject/build/classes/main/dummy. , SOURCE_PATH, .

+4
2

Project Lombok. - , ;)

, JavacFileManager, , StandardLocation.SOURCE_PATH to. , Gradle.

+1

, src/main/resources target/classes ( ). :

ProcessingEnvironment pe = ...;
FileObject fileObject = pe.getFiler()
    .getResource( StandardLocation.CLASS_OUTPUT, "", "config.json" );
InputStream jsonStream = fileObject.openInputStream();
+1

Source: https://habr.com/ru/post/1656234/


All Articles