0

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.

Armin Naderi
  • 33
  • 1
  • 7

1 Answers1

2

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

Thomas Broyer
  • 64,353
  • 7
  • 91
  • 164
  • Thanks for the reply. Interesting, Intellij separates the `build` classpath files into two directories: `classes` and `resources`. My processor added the HTML to the `classes` directory after changing the location to `CLASS_OUTPUT`. I will test this further and let you know what I find. – Armin Naderi Mar 08 '17 at 19:08
  • 1
    It seems Intellij merges those two directories together and adds them to the classpath, so setting the location to `CLASS_OUTPUT` should work theoretically. I'll try to get an MVP working with my processor to verify that this is the case. – Armin Naderi Mar 08 '17 at 19:19