Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java 6 annotation processing configuration with Ant

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.

like image 897
user18943 Avatar asked Sep 04 '10 21:09

user18943


People also ask

How do I enable JPS incremental annotation processing?

Navigate to Build, Execution, Deployment > Compiler > Annotation Processors. Make sure "Enable annotation processing".

What is Eclipse annotation processing?

A Java annotation processor is a compiler plug-in that can gather information about source code as it is being compiled, generate additional Java types or other resource files, and post warnings and errors.

What is Annotationprocessor in gradle?

An annotation processor is a class which extends AbstractProcessor (in the package javax. annotation. processing ) providing the functionality needed to generate whatever it needs to generate, such as classes in the case of mapstruct.


2 Answers

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>
like image 145
emory Avatar answered Oct 21 '22 13:10

emory


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>
like image 35
Brad Mace Avatar answered Oct 21 '22 14:10

Brad Mace