views:

86

answers:

2

I have a custom annotation and it's processor & processorFactory. How do I configure my Ant build file such that:

  1. The annotation processor is applied on annotated classes and generates source files inside "gen" folder

  2. The generated source files(from annotation processing) could be used by other source files in project.

A: 

you can take a look at the annotation processing tool , it automatically compiles the generated sourcefiles

//EDIT// In reply to your comment:

You can use apt in combination with the apt ant task

But as of jdk6 the javac tool provides direct support for annotation processing, so you should be able to use the javac ant task with the compiler attribute specified as "javac1.6"

davyM
I'm not looking for that. I'm looking for Ant build configuration instead (which is written very clearly in my question).
nabeelalimemon
A: 

This is not pretty, but it is what I do. (Sources javac ant task javac man page) Using the compilerarg attribute I can pass in the annotation processing related arguments that are not directly supported by the javac ant task.

<javac srcdir="${src}" destdir="${classes}" ... > 
     ....
     <compilerarg line="-processorpath ${processorpath}"/>
     <compilerarg line="-processor ${processor}"/>
     <compilerarg line="-s ${whereToPutGeneratedClassFiles}"/>
</javac>

I do not use the APT tool because the documentation states

Be advised that the Apt tool does appear to be an unstable part of the JDK framework, so may change radically in future versions. In particular it is likely to be obsolete in JDK 6, which can run annotation processors as part of javac.

If you really don't care for compiler args, you can jar your annotation processors like this

<jar destfile="${annotationprocessorjar}" ... >
     ...
     <service type="javax.annotation.processing.Processor" provider="${your.annotation.processor.fully.qualified.name}"/>
</jar>

Then you can do

 <javac ... make sure ${annotationprocessorjar} is in classpath>
 </javac>
emory
I tried both ways but each time ant target "completed in 0s" and it actually din't generate file or even print my debug statements. Interestingly, when I ran it from command line, I got the javac error Annotation Processor mypackage.MyProcessor not found. Am I missing something?
nabeelalimemon
Something is obviously wrong. What version of javac r u using? Does mypackage.MyProcessor extends deprecated 1.5 (http://download.oracle.com/javase/1.5.0/docs/guide/apt/mirror/com/sun/mirror/apt/AnnotationProcessor.html) or the current 1.6 (http://download-llnw.oracle.com/javase/6/docs/api/javax/annotation/processing/Processor.html)?
emory
nabeelalimemon
Instead of using "service" I created MyProcessor.jar having META-INF/services/javax.annotation.processing.Processor which contains mypackage.MyAnnotationProcessor. Now when i compile source files with MyProcessor.jar in classpath I get this error: "error: Bad service configuration file, or exception thrown while constructing Processor object: javax.annotation.processing.Processor: Provider mypackage.MyAnnotationProcessor not found"
nabeelalimemon
It turns out that your compiled package should go into root location of the jar file. Otherwise the annotation processor won't load. Previously I was copying my compiled package under "classes" folder inside the jar.
nabeelalimemon