I have a project that is laid out as follows:
src/
java
generated
src/java contains jpa entities and query classes that use the jpa metamodel classes that are generated by the hibernate metamodel annotation processor.
What is the best way to incorporate annotation processing into the java plugin?
I currently have the following task defined, but it has a task dependency on compileJava which will fail because some of the code is dependent on the classes that are generated by the annotation processor.
task processAnnotations(type: Compile) {
genDir = new File("${projectDir}/src/generated")
genDir.mkdirs()
source = ['src/java']
classpath = sourceSets.test.compileClasspath
destinationDir = genDir
options.compilerArgs = ["-proc:only"]
}
The reason why
processAnnotationsdepends oncompileJavais that you put the test compile class path on the former task’s compile class path, and the test compile class path contains the compiled production code (i.e. output ofcompileJava).As to how to best solve the problem at hand, you shouldn’t need a separate compile task. The Java compiler can invoke annotation processors and compile their generated sources (along with the original sources) in one pass (see Annotation Processing). One thing you’ll need to do is to put the annotation processor on the compile class path:
(You don’t want to add the annotation processor to the
compileconfiguration because then it will be considered a dependency of the production code.)From what I can tell, this is all there is to it (assuming you are using JDK6 or higher).