简体   繁体   中英

How to generate xhtml files in webapp using javac annotation processor

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.

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 :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM