Armin Naderi
Armin Naderi

Reputation: 33

Generating XML Resources into Classpath using Annotation Processors

I am currently working on a Gradle 3.3 project in Intellij 15.0.6.

I am using the Gradle APT plugin to add annotation processors to my classpath.

It works fine when generating Java class files, however I need to be able to generate XML sources within the resources directory equivalent in the build directory's generated directory.

Here is my build directory structure currently:

Project Build Directory Image

As you can see, it does not include a resources directory, which I suspect is what may be causing this problem.

The current exception I receive from running my annotation processor via ./gradlew assemble is: java.lang.IllegalArgumentException: Resource creation not supported in location CLASS_PATH

The code I am using within my annotation processor to generate the xml file:

FileObject source = processingEnv.getFiler() .createResource(StandardLocation.CLASS_PATH, "", "ap-test-2.html");

Note: I used an HTML extension just as a test, XML should produce the same results.

javax.tools.StandardLocation has other output locations as well:

The SOURCE_OUTPUT location worked to place the XML within the same package as the generated Java classes, within src/apt/main. This is not my desired behaviour however. I need them to reside within the classpath.

I have not found this exception discussed anywhere else after extensive research.

Any help is appreciated. Thank you for reading this question.

Upvotes: 0

Views: 1709

Answers (1)

Thomas Broyer
Thomas Broyer

Reputation: 64561

StandardLocation.CLASS_PATH is only for input, not output. The only output locations are SOURCE_OUPUT (the build/generated/source/apt/… folder), CLASS_OUTPUT (the standard Gradle build/classes/…), and NATIVE_HEADER_OUPUT. See https://docs.oracle.com/javase/8/docs/api/javax/tools/StandardLocation.html

JavaC has no notion of classes vs. resources outputs, but if you run your annotation processor during your compilation then CLASS_OUTPUT should work (Gradle should then copy everything into the final directory/JAR). See https://docs.oracle.com/javase/8/docs/technotes/tools/unix/javac.html

Upvotes: 2

Related Questions