how to compile only changed source files using ANT

10,267

Solution 1

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?

Solution 2

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.

Share:
10,267

Related videos on Youtube

Rais Alam
Author by

Rais Alam

I'm a Software Engineer done MCA with 5 years of experience. I'm specialized in Java and Agile (XP, Scrum and Lean). I like GNU/Linux, Ubuntu, open source philosophy, knowledge sharing, finding innovative solutions.

Updated on September 16, 2022

Comments

  • Rais Alam
    Rais Alam over 1 year

    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.