Piotr Sobczyk
Piotr Sobczyk

Reputation: 6583

Ant: javac with proc:only from Ant?

Is there any way to enforce javac task to invoke only annotation processing, without compilation. -proc:only javac option is supposed to enforce such behaviour, according to javac documentation.

But the following snippet of ant buildfile:

<target name="apt">
    <javac srcdir="src" destdir="bin" includeantruntime="false">
        <compilerarg value="-proc:only"/>
    </javac>
</target>

Compiles project anyway. I experimented with other <compilerarg> tag versions (e.g. line instead of value) but nothing helped.

Upvotes: 3

Views: 1024

Answers (1)

lbulej
lbulej

Reputation: 21

Avoid specifying the "destdir" attribute of the task, or use an empty directory as a destination for class files. The Ant "javac" task will then look for the class files either in the base directory (if you leave "destdir" unset) or in the empty directory. Because it will not find the classs files there, it will not exclude the potentially up-to-date sources from the compilation and execute "javac" on the sources from the directory specified in the "srcdir" attribute.

Your code would therefore look like this:

<target name="apt">
    <javac srcdir="src" includeantruntime="false">
        <compilerarg value="-proc:only" />
    </javac>
</target>

or, if you use the empty directory approach, like this:

<target name="apt">
    <mkdir dir="empty" />
    <javac srcdir="src" destdir="empty" includeantruntime="false">
        <compilerarg value="-proc:only" />
    </javac>
</target>

The second approach is slightly more complex but a bit more clean. Usually your project will have an output directory where you put compiled classes and packaged jars, so adding an extra empty directory there would not hurt. As of Ant version 1.9.4, I did not find any other way to do annotation processing from Ant independently of compilation, even though a simple "force" attribute in the "javac" task could solve this problem.

Upvotes: 2

Related Questions