Java 6 annotation processing configuration with Ant
Asked Answered
S

4

18

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.

Swithin answered 4/9, 2010 at 21:28 Comment(0)
S
12

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>
Shirleneshirley answered 5/9, 2010 at 1:21 Comment(5)
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?Swithin
Something is obviously wrong. What version of javac r u using? Does mypackage.MyProcessor extends deprecated 1.5 (download.oracle.com/javase/1.5.0/docs/guide/apt/mirror/com/sun/…) or the current 1.6 (download-llnw.oracle.com/javase/6/docs/api/javax/annotation/…)?Shirleneshirley
Hey that was the missing link. I was using javac v.1.6 and jdk1.5's mirror API's. I found it written everywhere that apt tool is deprecated but din't find that mirror API was deprecated too. That's awkward. Anyways it only works when I compile the processor and then compile other sources providing -processor & -processorpath args. It still won't do anything if I jar the processor and use it in classpath (I've already given the javax.annotation.processing.Processor file having mypackage.MyProcessor in META-INF/services).Swithin
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"Swithin
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.Swithin
G
3

I found some of the other examples slightly confusing due to some of the key bits being unexplained variables. Here's what I ended up with:

to build the processor jar:

<target name="build-aux">
    <mkdir dir="${build.dir}" />
    <mkdir dir="${build.classes}" />
    <javac destdir="${build.classes}" source="1.6" target="1.6">
        <src path="${src.java}" />
        <include name="com/acme/cli/Program.java" />
        <include name="com/acme/cli/ProgramProcessor.java" />
    </javac>

    <jar jarfile="acme-aux.jar" update="true">
        <manifest>
            <attribute name="Main-Class" value="${main.class}" />
            <attribute name="Implementation-Title" value="acme-aux" />
            <attribute name="Implementation-Version" value="${version}" />
            <attribute name="Implementation-Vendor" value="ACME, Inc" />
            <attribute name="Built-By" value="${user.name}" />
            <attribute name="Build-Date" value="${TODAY}" />
        </manifest>
        <fileset dir="${build.classes}">
            <!-- the annotation -->
            <include name="com/acme/cli/Program.class" />
            <!-- the annotation processor -->
            <include name="com/acme/cli/ProgramProcessor.class" />
        </fileset>
        <service type="javax.annotation.processing.Processor"
            provider="com.acme.cli.ProgramProcessor" />
    </jar>
</target>

then to compile the code and run the processor:

<target name="compile" depends="generate,build-aux">
    <mkdir dir="${build.dir}" />
    <mkdir dir="${build.classes}" />
    <javac destdir="${build.classes}" source="1.6" target="1.6">
        <src path="${src.java}" />
        <include name="com/acme/**/*.java" />
        <!-- ensure that "acme-aux.jar" is in this classpath -->
        <classpath refid="compile.class.path"/>
         <!-- pass option to annotation processor -->
        <compilerarg value="-Aacme.version=${version}" />
    </javac>
</target>
Gothard answered 2/8, 2013 at 14:46 Comment(0)
N
1

Here's how I did it in eclipse/ant:

<javac destdir="bin"
  debug="true"
  debuglevel="${debuglevel}"
  compiler="javac1.6"
  srcdir="${src}">
       <include name="**/*.java"/> <!-- I just do it this way -->
 <classpath refid="classpath_ref_id"/>
 <compilerarg value="-processor" />
 <compilerarg value="${processor}" />
 <compilerarg value="-s" />
 <compilerarg value="${gen_src_target}" />
</javac>

Notes

  • The processor path is included in the *classpath_ref_id*
  • Run your processor before you compile the actual code (with or without the generated code).
Nettie answered 6/12, 2010 at 18:53 Comment(0)
E
0

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"

Escuage answered 4/9, 2010 at 22:54 Comment(1)
I'm not looking for that. I'm looking for Ant build configuration instead (which is written very clearly in my question).Swithin

© 2022 - 2024 — McMap. All rights reserved.