Welcome to Srini's blog

Thursday, May 13, 2010

Java compilation issue with ant script

How to compile java using ANT ? Everybody knows java compilation using ant scripts. I thought you are already familiar with ant build scripts to compile java .Yesterday i got a requirement like compile different packages folders at a time. Due to some inter dependencies in these folders I need to compile these dirs in one shot.A quick googling I got solution and its worked for me. Let me explain how it can be coded.

1. The basic ant script to compile java files in one folder is
<javac srcdir="${src}"
destdir="${build}"
classpath="xyz.jar"
debug="on" />


Above script compiles all .java files under the ${src} directory, and stores the .class files in the ${build} directory. If your code using any thirdparty jars, you need to specify in classpath and and compiling with debug information is on.

2. Ant script to compile java files with some extra logic is
<javac srcdir="${src}"
destdir="${build}"
includes="mypkg/p1/**,mypkg/p2/**"
excludes="mypkg/p1/testpkg/**"
classpath="xyz.jar"
debug="on"/>

Above script compiles .java files under the ${src} directory, and stores the .class files in the ${build} directory. The classpath used includes xyz.jar, and debug information is on. Only files under mypkg/p1 and mypkg/p2 are used. All files in and below the mypkg/p1/testpkg directory are excluded from compilation.

3. Ant script to compile multiple source directories at one shot is
<javac srcdir="${src}:${src2}"
destdir="${build}"
includes="mypkg/p1/**,mypkg/p2/**"
excludes="mypkg/p1/testpkg/**"
classpath="xyz.jar"
debug="on"/>


This can also be represented using nested as follows
<javac destdir="${build}"
classpath="xyz.jar"
debug="on">
<src path="${src}"/>
<src path="${src2}"/>
<include name="mypkg/p1/**"/>
<include name="mypkg/p2/**"/>
<exclude name="mypkg/p1/testpkg/**"/>
</javac>


No comments:

Post a Comment