0

I am generating some source files at compile time using annotation processors, it is a very powerful feature. But I want to generate also some facelets components. I don't know how to create non java files in the webapp folder. I know I can do this creating a maven plugin, but I want to do it with javac annotation processors. Is it possible? Any advice?

Clarifications:

The idea is to generate some xhtml files (facelets tags and components) right under the webapp folder of the sources (maven project) based on JPA entities. So I have created an AnnotationProcessor, it is fired automatically by javac at compile time and using the javax.annotation.processing API I can generate files under target/generated-sources only.

I have found a workaround, creating a dummy file under target/generated-sources and using its URI to resolve the src/main/webapp, but if are there any more elegant solution using the API it will be welcome.

mnesarco
  • 2,619
  • 23
  • 31

1 Answers1

0

I have found an ugly but working solution: Create a dummy file in SOURCE_OUTPUT and get the path from the FileObject URI, then navigate to the project root.

  FileObject f = processingEnv.getFiler().createResource(StandardLocation.SOURCE_OUTPUT, "", "DUMMY");
  Path p = Paths.get(f.toUri())
          .getParent()  // {PROJECT_ROOT}/target/generated-sources/annotations
          .getParent()  // {PROJECT_ROOT}/target/generated-sources
          .getParent()  // {PROJECT_ROOT}/target
          .getParent(); // {PROJECT_ROOT}
  FileWriter fw = new FileWriter(new File(p.toFile(), "src/main/webapp/generated.xhtml"));
  fw.append("some content...");
  fw.close();

I leave this answer until a better one arrives :)

mnesarco
  • 2,619
  • 23
  • 31
  • How, what, where, when do you use this? I fail to see it's place in the question and (although you generate facelets xhtml), no relation to JSF. Cheers (out of curiosity, what is your usecase for generating xhtml?) – Kukeltje Sep 26 '18 at 23:04
  • Hi @Hukeltje, I have created an AnnotationProcessor that generates JSF artifacts as FacesConverters, DeltaSpike Repositories, Finders, and many more reusable components based on JPA Entities... Now I am generating also facelets tags and components, so most of the repetitive tasks of my JSF apps are now automated. – mnesarco Sep 27 '18 at 00:02