Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to compile only changed source files using ANT

I am trying to write ant build for compiling source folder here is my script target for compiling.

 <target name="compile" depends="init">
        <javac srcdir="${src.dir}" destdir="${classes.dir}" debug="true">
            <classpath refid="master-classpath"/>
        </javac>

    </target>

In my project I have near about 1000 .java files. When ever a single .java file is changed above target tends to compile all .java files. Which make development very slow. I just want to know is there any way or code to change the behavior of task to compile only modified or changed .java file rather than all .java file.

Please help me.

like image 965
Rais Alam Avatar asked Feb 28 '13 06:02

Rais Alam


People also ask

What is build xml in ant?

Ant uses an xml file for its configuration. The default file name is build. xml . Ant builds are based on three blocks: tasks, targets and extension points. A task is a unit of work which should be performed and constitutes of small atomic steps, for example compile source code or create Javadoc.

What is Ant build script?

Ant is a Java-based build tool created as part of the Apache open-source project. You can think of it as a Java version of make. Ant scripts have a structure and are written in XML. Similar to make, Ant targets can depend on other targets.


2 Answers

As I understand, compiling only the modified java files is the default behavior of ant javac task. Ant uses the timestamp of a .java file and its corresponding .class file to determine if the a Java file needs to be recompiled. I have used it in many projects and never have issues.

Here are a few things you can check

  1. Is your source tree directory structure matching your Java package structure? Please see Why does ant always recompile all my Java files?. This is probably the number 1 reason since it is in the FAQ.

  2. I see your compile target depends on init target? What does the init target do? Does it depends on any clean target or does it cleanup the .class files?

  3. What is your ant version? Can you try a different ant version to see if you still have the same problem?

  4. If none of the above fixes your issue, after the compilation, can you manually check the .class file timestamp and compare it with the timestamp of the corresponding .java file?

like image 173
Lan Avatar answered Oct 07 '22 18:10

Lan


So if you are not deleting the classes directory in any dependent task say 'clean' the javac task inherently compiles only the changed java files. It is an out of the box feature that javac task provides. Hope it helps.

like image 36
Tushar Avatar answered Oct 07 '22 16:10

Tushar