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.
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.
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.
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
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.
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?
What is your ant version? Can you try a different ant version to see if you still have the same problem?
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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With